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