Chris@64
|
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
|
Chris@64
|
2 // Licensed under the MIT License:
|
Chris@64
|
3 //
|
Chris@64
|
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
Chris@64
|
5 // of this software and associated documentation files (the "Software"), to deal
|
Chris@64
|
6 // in the Software without restriction, including without limitation the rights
|
Chris@64
|
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
Chris@64
|
8 // copies of the Software, and to permit persons to whom the Software is
|
Chris@64
|
9 // furnished to do so, subject to the following conditions:
|
Chris@64
|
10 //
|
Chris@64
|
11 // The above copyright notice and this permission notice shall be included in
|
Chris@64
|
12 // all copies or substantial portions of the Software.
|
Chris@64
|
13 //
|
Chris@64
|
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
Chris@64
|
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
Chris@64
|
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
Chris@64
|
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
Chris@64
|
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
Chris@64
|
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
Chris@64
|
20 // THE SOFTWARE.
|
Chris@64
|
21
|
Chris@64
|
22 #ifndef CAPNP_POINTER_HELPERS_H_
|
Chris@64
|
23 #define CAPNP_POINTER_HELPERS_H_
|
Chris@64
|
24
|
Chris@64
|
25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
|
Chris@64
|
26 #pragma GCC system_header
|
Chris@64
|
27 #endif
|
Chris@64
|
28
|
Chris@64
|
29 #include "layout.h"
|
Chris@64
|
30 #include "list.h"
|
Chris@64
|
31
|
Chris@64
|
32 namespace capnp {
|
Chris@64
|
33 namespace _ { // private
|
Chris@64
|
34
|
Chris@64
|
35 // PointerHelpers is a template class that assists in wrapping/unwrapping the low-level types in
|
Chris@64
|
36 // layout.h with the high-level public API and generated types. This way, the code generator
|
Chris@64
|
37 // and other templates do not have to specialize on each kind of pointer.
|
Chris@64
|
38
|
Chris@64
|
39 template <typename T>
|
Chris@64
|
40 struct PointerHelpers<T, Kind::STRUCT> {
|
Chris@64
|
41 static inline typename T::Reader get(PointerReader reader, const word* defaultValue = nullptr) {
|
Chris@64
|
42 return typename T::Reader(reader.getStruct(defaultValue));
|
Chris@64
|
43 }
|
Chris@64
|
44 static inline typename T::Builder get(PointerBuilder builder,
|
Chris@64
|
45 const word* defaultValue = nullptr) {
|
Chris@64
|
46 return typename T::Builder(builder.getStruct(structSize<T>(), defaultValue));
|
Chris@64
|
47 }
|
Chris@64
|
48 static inline void set(PointerBuilder builder, typename T::Reader value) {
|
Chris@64
|
49 builder.setStruct(value._reader);
|
Chris@64
|
50 }
|
Chris@64
|
51 static inline void setCanonical(PointerBuilder builder, typename T::Reader value) {
|
Chris@64
|
52 builder.setStruct(value._reader, true);
|
Chris@64
|
53 }
|
Chris@64
|
54 static inline typename T::Builder init(PointerBuilder builder) {
|
Chris@64
|
55 return typename T::Builder(builder.initStruct(structSize<T>()));
|
Chris@64
|
56 }
|
Chris@64
|
57 static inline void adopt(PointerBuilder builder, Orphan<T>&& value) {
|
Chris@64
|
58 builder.adopt(kj::mv(value.builder));
|
Chris@64
|
59 }
|
Chris@64
|
60 static inline Orphan<T> disown(PointerBuilder builder) {
|
Chris@64
|
61 return Orphan<T>(builder.disown());
|
Chris@64
|
62 }
|
Chris@64
|
63 static inline _::StructReader getInternalReader(const typename T::Reader& reader) {
|
Chris@64
|
64 return reader._reader;
|
Chris@64
|
65 }
|
Chris@64
|
66 static inline _::StructBuilder getInternalBuilder(typename T::Builder&& builder) {
|
Chris@64
|
67 return builder._builder;
|
Chris@64
|
68 }
|
Chris@64
|
69 };
|
Chris@64
|
70
|
Chris@64
|
71 template <typename T>
|
Chris@64
|
72 struct PointerHelpers<List<T>, Kind::LIST> {
|
Chris@64
|
73 static inline typename List<T>::Reader get(PointerReader reader,
|
Chris@64
|
74 const word* defaultValue = nullptr) {
|
Chris@64
|
75 return typename List<T>::Reader(List<T>::getFromPointer(reader, defaultValue));
|
Chris@64
|
76 }
|
Chris@64
|
77 static inline typename List<T>::Builder get(PointerBuilder builder,
|
Chris@64
|
78 const word* defaultValue = nullptr) {
|
Chris@64
|
79 return typename List<T>::Builder(List<T>::getFromPointer(builder, defaultValue));
|
Chris@64
|
80 }
|
Chris@64
|
81 static inline void set(PointerBuilder builder, typename List<T>::Reader value) {
|
Chris@64
|
82 builder.setList(value.reader);
|
Chris@64
|
83 }
|
Chris@64
|
84 static inline void setCanonical(PointerBuilder builder, typename List<T>::Reader value) {
|
Chris@64
|
85 builder.setList(value.reader, true);
|
Chris@64
|
86 }
|
Chris@64
|
87 static void set(PointerBuilder builder, kj::ArrayPtr<const ReaderFor<T>> value) {
|
Chris@64
|
88 auto l = init(builder, value.size());
|
Chris@64
|
89 uint i = 0;
|
Chris@64
|
90 for (auto& element: value) {
|
Chris@64
|
91 l.set(i++, element);
|
Chris@64
|
92 }
|
Chris@64
|
93 }
|
Chris@64
|
94 static inline typename List<T>::Builder init(PointerBuilder builder, uint size) {
|
Chris@64
|
95 return typename List<T>::Builder(List<T>::initPointer(builder, size));
|
Chris@64
|
96 }
|
Chris@64
|
97 static inline void adopt(PointerBuilder builder, Orphan<List<T>>&& value) {
|
Chris@64
|
98 builder.adopt(kj::mv(value.builder));
|
Chris@64
|
99 }
|
Chris@64
|
100 static inline Orphan<List<T>> disown(PointerBuilder builder) {
|
Chris@64
|
101 return Orphan<List<T>>(builder.disown());
|
Chris@64
|
102 }
|
Chris@64
|
103 static inline _::ListReader getInternalReader(const typename List<T>::Reader& reader) {
|
Chris@64
|
104 return reader.reader;
|
Chris@64
|
105 }
|
Chris@64
|
106 static inline _::ListBuilder getInternalBuilder(typename List<T>::Builder&& builder) {
|
Chris@64
|
107 return builder.builder;
|
Chris@64
|
108 }
|
Chris@64
|
109 };
|
Chris@64
|
110
|
Chris@64
|
111 template <typename T>
|
Chris@64
|
112 struct PointerHelpers<T, Kind::BLOB> {
|
Chris@64
|
113 static inline typename T::Reader get(PointerReader reader,
|
Chris@64
|
114 const void* defaultValue = nullptr,
|
Chris@64
|
115 uint defaultBytes = 0) {
|
Chris@64
|
116 return reader.getBlob<T>(defaultValue, bounded(defaultBytes) * BYTES);
|
Chris@64
|
117 }
|
Chris@64
|
118 static inline typename T::Builder get(PointerBuilder builder,
|
Chris@64
|
119 const void* defaultValue = nullptr,
|
Chris@64
|
120 uint defaultBytes = 0) {
|
Chris@64
|
121 return builder.getBlob<T>(defaultValue, bounded(defaultBytes) * BYTES);
|
Chris@64
|
122 }
|
Chris@64
|
123 static inline void set(PointerBuilder builder, typename T::Reader value) {
|
Chris@64
|
124 builder.setBlob<T>(value);
|
Chris@64
|
125 }
|
Chris@64
|
126 static inline void setCanonical(PointerBuilder builder, typename T::Reader value) {
|
Chris@64
|
127 builder.setBlob<T>(value);
|
Chris@64
|
128 }
|
Chris@64
|
129 static inline typename T::Builder init(PointerBuilder builder, uint size) {
|
Chris@64
|
130 return builder.initBlob<T>(bounded(size) * BYTES);
|
Chris@64
|
131 }
|
Chris@64
|
132 static inline void adopt(PointerBuilder builder, Orphan<T>&& value) {
|
Chris@64
|
133 builder.adopt(kj::mv(value.builder));
|
Chris@64
|
134 }
|
Chris@64
|
135 static inline Orphan<T> disown(PointerBuilder builder) {
|
Chris@64
|
136 return Orphan<T>(builder.disown());
|
Chris@64
|
137 }
|
Chris@64
|
138 };
|
Chris@64
|
139
|
Chris@64
|
140 struct UncheckedMessage {
|
Chris@64
|
141 typedef const word* Reader;
|
Chris@64
|
142 };
|
Chris@64
|
143
|
Chris@64
|
144 template <> struct Kind_<UncheckedMessage> { static constexpr Kind kind = Kind::OTHER; };
|
Chris@64
|
145
|
Chris@64
|
146 template <>
|
Chris@64
|
147 struct PointerHelpers<UncheckedMessage> {
|
Chris@64
|
148 // Reads an AnyPointer field as an unchecked message pointer. Requires that the containing
|
Chris@64
|
149 // message is itself unchecked. This hack is currently private. It is used to locate default
|
Chris@64
|
150 // values within encoded schemas.
|
Chris@64
|
151
|
Chris@64
|
152 static inline const word* get(PointerReader reader) {
|
Chris@64
|
153 return reader.getUnchecked();
|
Chris@64
|
154 }
|
Chris@64
|
155 };
|
Chris@64
|
156
|
Chris@64
|
157 } // namespace _ (private)
|
Chris@64
|
158 } // namespace capnp
|
Chris@64
|
159
|
Chris@64
|
160 #endif // CAPNP_POINTER_HELPERS_H_
|