Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(361)

Unified Diff: mojo/public/bindings/bindings.h

Issue 23913008: C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/public/bindings/bindings.h
diff --git a/mojo/public/bindings/bindings.h b/mojo/public/bindings/bindings.h
new file mode 100644
index 0000000000000000000000000000000000000000..9be6d83a6f6675a67c54bec19d08a663dd0f0a06
--- /dev/null
+++ b/mojo/public/bindings/bindings.h
@@ -0,0 +1,211 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MOJO_PUBLIC_BINDINGS_BINDINGS_H_
+#define MOJO_PUBLIC_BINDINGS_BINDINGS_H_
+
+#include <assert.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <new>
+#include <vector>
+
+#include "mojo/public/system/core.h"
+
+namespace mojo {
+
+template <typename T> class Array;
+class String;
+
+namespace internal {
+
+struct MessageHeader {
+ uint32_t num_bytes;
+ uint32_t name;
+};
+
+struct StructHeader {
+ uint32_t num_bytes;
+ uint32_t num_fields;
+};
+
+struct ArrayHeader {
+ uint32_t num_bytes;
+ uint32_t num_elements;
+};
+
+template <typename T>
+union StructPointer {
+ uint64_t offset;
+ T* ptr;
+};
+
+template <typename T>
+union ArrayPointer {
+ uint64_t offset;
+ Array<T>* ptr;
+};
+
+union StringPointer {
+ uint64_t offset;
+ String* ptr;
+};
+
+template <typename T>
+struct ArrayTraits {
+ typedef T StorageType;
+
+ static T& ToRef(StorageType& e) { return e; }
+ static T const& ToConstRef(const StorageType& e) { return e; }
+};
+
+template <typename P>
+struct ArrayTraits<P*> {
+ typedef StructPointer<P> StorageType;
+
+ static P*& ToRef(StorageType& e) { return e.ptr; }
+ static P* const& ToConstRef(const StorageType& e) { return e.ptr; }
+};
+
+template <typename T> class ObjectTraits {};
+
+} // namespace internal
+
+//-----------------------------------------------------------------------------
+
+template <typename T>
+class Array {
+ public:
+ explicit Array(size_t num_elements) {
+ // TODO: bools should get packed to a single bit?
+ header_.num_bytes =
+ sizeof(*this) +
+ sizeof(typename internal::ArrayTraits<T>::StorageType) *
+ (num_elements - 1);
+ header_.num_elements = num_elements;
+ }
+
+ size_t size() const { return header_.num_elements; }
+
+ T* data() { return elements_; }
+ const T* data() const { return elements_; }
+
+ T& at(size_t offset) {
+ return internal::ArrayTraits<T>::ToRef(elements_[offset]);
+ }
+
+ const T& at(size_t offset) const {
+ return internal::ArrayTraits<T>::ToConstRef(elements_[offset]);
+ }
+
+ T& operator[](size_t offset) {
+ return at(offset);
+ }
+
+ const T& operator[](size_t offset) const {
+ return at(offset);
+ }
+
+ private:
+ friend class internal::ObjectTraits<Array<T> >;
+ friend class internal::ObjectTraits<String>;
+
+ internal::ArrayHeader header_;
+
+ // Extra elements follow.
+ typename internal::ArrayTraits<T>::StorageType elements_[1];
+};
+
+//-----------------------------------------------------------------------------
+
+// UTF-8 encoded
+class String : public Array<char> {
+ public:
+ explicit String(size_t num_chars)
+ : Array<char>(num_chars) {
+ }
+
+ char& operator[](size_t offset) {
+ return at(offset);
+ }
+
+ const char& operator[](size_t offset) const {
+ return at(offset);
+ }
+};
+
+//-----------------------------------------------------------------------------
+
+struct Message {
+ uint32_t name;
+ uint8_t* data_start;
+ uint8_t* data_end;
+ //XXX MessageBuffer data;
+ std::vector<Handle> handles;
+};
+
+class MessageSender {
+ public:
+ virtual void Send(const Message& message) = 0;
+};
+
+//-----------------------------------------------------------------------------
+
+// The following is a cheezy arena allocator.
+class MessageBuffer {
+ public:
+ MessageBuffer() : ptr_(NULL), size_(0) {
+ }
+ ~MessageBuffer() { free(ptr_); }
+
+ const uint8_t* data() const { return ptr_; }
+ uint8_t* data() { return ptr_; }
+
+ size_t size() const { return size_; }
+
+ template <typename T>
+ T* New() {
+ size_t size = sizeof(T);
+ return new (Grow(size)) T();
+ }
+
+ template <typename T>
+ Array<T>* NewArray(size_t num_elements) {
+ // (count - 1) because Array<T> has reserved space for the first element.
+ size_t size = sizeof(Array<T>) + sizeof(T) * (num_elements - 1);
+ return new (Grow(size)) Array<T>(num_elements);
+ }
+
+ String* NewString(size_t num_chars) {
+ // (num_chars - 1) because String has reserved space for the first element.
+ size_t size = sizeof(String) + (num_chars - 1);
+ return new (Grow(size)) String(num_chars);
+ }
+
+ private:
+ uint8_t* Grow(size_t delta) {
+ // TODO: Align allocations
+ size_t old_size = size_;
+ size_t new_size = old_size + delta;
+ ptr_ = static_cast<uint8_t*>(realloc(ptr_, old_size + delta));
+ size_ = new_size;
+ uint8_t* result = ptr_ + old_size;
+ memset(result, 0, delta);
+ return result;
+ }
+
+ uint8_t* ptr_;
+ size_t size_;
+
+ // NOT IMPLEMENTED
+ MessageBuffer(const MessageBuffer&);
+ void operator=(const MessageBuffer&);
+};
+
+} // namespace mojo
+
+#endif // MOJO_PUBLIC_BINDINGS_BINDINGS_H_

Powered by Google App Engine
This is Rietveld 408576698