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

Unified Diff: mojo/public/bindings/bindings_internal.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_internal.h
diff --git a/mojo/public/bindings/bindings_internal.h b/mojo/public/bindings/bindings_internal.h
new file mode 100644
index 0000000000000000000000000000000000000000..7150153843b56e1eb9d509057f52de9ec44d26c2
--- /dev/null
+++ b/mojo/public/bindings/bindings_internal.h
@@ -0,0 +1,150 @@
+// 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_INTERNAL_H_
+#define MOJO_PUBLIC_BINDINGS_BINDINGS_INTERNAL_H_
+
+#include <string.h>
+
+#include <vector>
+
+#include "mojo/public/bindings/bindings.h"
+
+namespace mojo {
+namespace internal {
+
+inline void EncodePointer(void* address, uint64_t* offset) {
+ if (!address) {
+ *offset = 0;
+ return;
+ }
+ uint8_t* p_obj = reinterpret_cast<uint8_t*>(address);
+ uint8_t* p_slot = reinterpret_cast<uint8_t*>(offset);
+ assert(p_obj > p_slot);
+ *offset = p_obj - p_slot;
+}
+
+template <typename T>
+inline void DecodePointer(uint64_t* offset, T** ptr) {
+ if (!*offset) {
+ *ptr = NULL;
+ return;
+ }
+ uint8_t* p_slot = reinterpret_cast<uint8_t*>(offset);
+ *ptr = reinterpret_cast<T*>(p_slot + *offset);
+}
+
+template <typename T>
+inline T* Clone(const T* obj, MessageBuffer* buf) {
+ return Traits<T>::Clone(obj, buf);
+}
+
+template <typename T>
+inline void EncodePointersAndHandles(T* obj,
+ std::vector<Handle>* handles) {
+ Traits<T>::EncodePointersAndHandles(obj, handles);
+}
+
+template <typename T>
+inline bool DecodePointersAndHandles(T* obj, const Message& message) {
+ return Traits<T>::DecodePointersAndHandles(obj, message);
+}
+
+template <typename T>
+inline void Encode(T* obj, std::vector<Handle>* handles) {
+ if (obj->ptr)
+ EncodePointersAndHandles(obj->ptr, handles);
+ EncodePointer(obj->ptr, &obj->offset);
+}
+
+template <typename T>
+inline bool Decode(T* obj, const Message& message) {
+ DecodePointer(&obj->offset, &obj->ptr);
+ if (obj->ptr) {
+ if (!DecodePointersAndHandles(obj->ptr, message))
+ return false;
+ }
+ // TODO: Validate!
+ return true;
+}
+
+template <typename T>
+struct ArrayHelper {
+ typedef T ElementType;
+
+ static void CloneElements(MessageBuffer* buf,
+ ArrayHeader* header,
+ ElementType* elements) {
+ }
+
+ static void EncodePointersAndHandles(ArrayHeader* header,
+ ElementType* elements,
+ std::vector<Handle>* handles) {
+ }
+ static bool DecodePointersAndHandles(ArrayHeader* header,
+ ElementType* elements,
+ const Message& message) {
+ return true;
+ }
+};
+
+template <typename P>
+struct ArrayHelper<P*> {
+ typedef StructPointer<P> ElementType;
+
+ static void CloneElements(MessageBuffer* buf,
+ ArrayHeader* header,
+ ElementType* elements) {
+ for (uint32_t i = 0; i < header->num_elements; ++i) {
+ if (elements[i].ptr)
+ elements[i].ptr = Clone(elements[i].ptr, buf);
+ }
+ }
+
+ static void EncodePointersAndHandles(ArrayHeader* header,
+ ElementType* elements,
+ std::vector<Handle>* handles) {
+ for (uint32_t i = 0; i < header->num_elements; ++i)
+ Encode(&elements[i], handles);
+ }
+ static bool DecodePointersAndHandles(ArrayHeader* header,
+ ElementType* elements,
+ const Message& message) {
+ for (uint32_t i = 0; i < header->num_elements; ++i) {
+ if (!Decode(&elements[i], message))
+ return false;
+ }
+ return true;
+ }
+};
+
+template <typename T>
+class Traits<Array<T> > {
+ public:
+ static Array<T>* Clone(const Array<T>* array, MessageBuffer* buf) {
+ Array<T>* clone = buf->AllocArray<T>(array->header_.num_elements);
+ memcpy(clone, array, array->header_.num_bytes);
+
+ ArrayHelper<T>::CloneElements(buf, &clone->header_, clone->elements_);
+ return clone;
+ }
+
+ static void EncodePointersAndHandles(Array<T>* array,
+ std::vector<Handle>* handles) {
+ ArrayHelper<T>::EncodePointersAndHandles(&array->header_, array->elements_,
+ handles);
+ }
+
+ static bool DecodePointersAndHandles(Array<T>* array,
+ const Message& message) {
+ return ArrayHelper<T>::DecodePointersAndHandles(&array->header_,
+ array->elements_,
+ message);
+ }
+};
+
+} // namespace internal
+} // namespace mojo
+
+#endif // MOJO_PUBLIC_BINDINGS_BINDINGS_INTERNAL_H_

Powered by Google App Engine
This is Rietveld 408576698