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

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

Issue 23913008: C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Windows build. Created 7 years, 2 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/lib/bindings.h
diff --git a/mojo/public/bindings/lib/bindings.h b/mojo/public/bindings/lib/bindings.h
new file mode 100644
index 0000000000000000000000000000000000000000..0f1effbb590b28a90ebea0ef25f2e1db311280c8
--- /dev/null
+++ b/mojo/public/bindings/lib/bindings.h
@@ -0,0 +1,142 @@
+// 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_LIB_BINDINGS_H_
+#define MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_H_
+
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+
+#include <new>
+
+#include "mojo/public/bindings/lib/buffer.h"
+#include "mojo/public/system/core.h"
+
+namespace mojo {
+
+//-----------------------------------------------------------------------------
+
+template <typename T> class Array;
+
+// UTF-8 encoded
+typedef Array<char> String;
+
+namespace internal {
+
+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:
+ static Array<T>* New(Buffer* buf, size_t num_elements) {
+ // The first array element is included in sizeof(*this).
+ size_t num_bytes =
+ sizeof(Array<T>) +
+ sizeof(typename internal::ArrayTraits<T>::StorageType) *
+ (num_elements - 1);
viettrungluu 2013/10/09 03:44:06 Can num_elements be 0?
+ return new (buf->Allocate(num_bytes)) Array<T>(num_bytes, num_elements);
+ }
+
+ template <typename U>
+ static Array<T>* NewCopyOf(Buffer* buf, const U& u) {
+ Array<T>* result = Array<T>::New(buf, u.size());
+ memcpy(result->data(), u.data(), u.size() * sizeof(T));
+ return result;
+ }
+
+ 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);
+ }
+
+ template <typename U>
+ U To() const {
+ return U(data(), data() + size());
+ }
+
+ protected:
+ Array(size_t num_bytes, size_t num_elements) {
+ header_.num_bytes = static_cast<uint32_t>(num_bytes);
+ header_.num_elements = static_cast<uint32_t>(num_elements);
+ }
+ ~Array() {
+ }
+
+ private:
+ friend class internal::ObjectTraits<Array<T> >;
+
+ internal::ArrayHeader header_;
+
+ // Extra elements follow.
+ // TODO: bools should get packed to a single bit?
+ typename internal::ArrayTraits<T>::StorageType elements_[1];
+};
+
+} // namespace mojo
+
+#endif // MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_H_

Powered by Google App Engine
This is Rietveld 408576698