comparison osx/include/capnp/dynamic.h @ 147:45360b968bf4

Cap'n Proto v0.6 + build for OSX
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 22 May 2017 10:01:37 +0100
parents 41e769c91eca
children
comparison
equal deleted inserted replaced
146:206f0eb279b8 147:45360b968bf4
212 StructSchema schema; 212 StructSchema schema;
213 _::StructReader reader; 213 _::StructReader reader;
214 214
215 inline Reader(StructSchema schema, _::StructReader reader) 215 inline Reader(StructSchema schema, _::StructReader reader)
216 : schema(schema), reader(reader) {} 216 : schema(schema), reader(reader) {}
217 Reader(StructSchema schema, const _::OrphanBuilder& orphan);
217 218
218 bool isSetInUnion(StructSchema::Field field) const; 219 bool isSetInUnion(StructSchema::Field field) const;
219 void verifySetInUnion(StructSchema::Field field) const; 220 void verifySetInUnion(StructSchema::Field field) const;
220 static DynamicValue::Reader getImpl(_::StructReader reader, StructSchema::Field field); 221 static DynamicValue::Reader getImpl(_::StructReader reader, StructSchema::Field field);
221 222
303 StructSchema schema; 304 StructSchema schema;
304 _::StructBuilder builder; 305 _::StructBuilder builder;
305 306
306 inline Builder(StructSchema schema, _::StructBuilder builder) 307 inline Builder(StructSchema schema, _::StructBuilder builder)
307 : schema(schema), builder(builder) {} 308 : schema(schema), builder(builder) {}
309 Builder(StructSchema schema, _::OrphanBuilder& orphan);
308 310
309 bool isSetInUnion(StructSchema::Field field); 311 bool isSetInUnion(StructSchema::Field field);
310 void verifySetInUnion(StructSchema::Field field); 312 void verifySetInUnion(StructSchema::Field field);
311 void setInUnion(StructSchema::Field field); 313 void setInUnion(StructSchema::Field field);
312 314
367 // Try to convert to any List<T>, Data, or Text. Throws an exception if the underlying data 369 // Try to convert to any List<T>, Data, or Text. Throws an exception if the underlying data
368 // can't possibly represent the requested type. 370 // can't possibly represent the requested type.
369 371
370 inline ListSchema getSchema() const { return schema; } 372 inline ListSchema getSchema() const { return schema; }
371 373
372 inline uint size() const { return reader.size() / ELEMENTS; } 374 inline uint size() const { return unbound(reader.size() / ELEMENTS); }
373 DynamicValue::Reader operator[](uint index) const; 375 DynamicValue::Reader operator[](uint index) const;
374 376
375 typedef _::IndexingIterator<const Reader, DynamicValue::Reader> Iterator; 377 typedef _::IndexingIterator<const Reader, DynamicValue::Reader> Iterator;
376 inline Iterator begin() const { return Iterator(this, 0); } 378 inline Iterator begin() const { return Iterator(this, 0); }
377 inline Iterator end() const { return Iterator(this, size()); } 379 inline Iterator end() const { return Iterator(this, size()); }
379 private: 381 private:
380 ListSchema schema; 382 ListSchema schema;
381 _::ListReader reader; 383 _::ListReader reader;
382 384
383 Reader(ListSchema schema, _::ListReader reader): schema(schema), reader(reader) {} 385 Reader(ListSchema schema, _::ListReader reader): schema(schema), reader(reader) {}
386 Reader(ListSchema schema, const _::OrphanBuilder& orphan);
384 387
385 template <typename T, Kind k> 388 template <typename T, Kind k>
386 friend struct _::PointerHelpers; 389 friend struct _::PointerHelpers;
387 friend struct DynamicStruct; 390 friend struct DynamicStruct;
388 friend class DynamicList::Builder; 391 friend class DynamicList::Builder;
409 // Try to convert to any List<T>, Data, or Text. Throws an exception if the underlying data 412 // Try to convert to any List<T>, Data, or Text. Throws an exception if the underlying data
410 // can't possibly represent the requested type. 413 // can't possibly represent the requested type.
411 414
412 inline ListSchema getSchema() const { return schema; } 415 inline ListSchema getSchema() const { return schema; }
413 416
414 inline uint size() const { return builder.size() / ELEMENTS; } 417 inline uint size() const { return unbound(builder.size() / ELEMENTS); }
415 DynamicValue::Builder operator[](uint index); 418 DynamicValue::Builder operator[](uint index);
416 void set(uint index, const DynamicValue::Reader& value); 419 void set(uint index, const DynamicValue::Reader& value);
417 DynamicValue::Builder init(uint index, uint size); 420 DynamicValue::Builder init(uint index, uint size);
418 void adopt(uint index, Orphan<DynamicValue>&& orphan); 421 void adopt(uint index, Orphan<DynamicValue>&& orphan);
419 Orphan<DynamicValue> disown(uint index); 422 Orphan<DynamicValue> disown(uint index);
429 private: 432 private:
430 ListSchema schema; 433 ListSchema schema;
431 _::ListBuilder builder; 434 _::ListBuilder builder;
432 435
433 Builder(ListSchema schema, _::ListBuilder builder): schema(schema), builder(builder) {} 436 Builder(ListSchema schema, _::ListBuilder builder): schema(schema), builder(builder) {}
437 Builder(ListSchema schema, _::OrphanBuilder& orphan);
434 438
435 template <typename T, Kind k> 439 template <typename T, Kind k>
436 friend struct _::PointerHelpers; 440 friend struct _::PointerHelpers;
437 friend struct DynamicStruct; 441 friend struct DynamicStruct;
438 template <typename T, ::capnp::Kind k> 442 template <typename T, ::capnp::Kind k>
632 // - Text, Data, AnyPointer, any struct type: Returns the corresponding Reader. 636 // - Text, Data, AnyPointer, any struct type: Returns the corresponding Reader.
633 // - List<T> for any T listed above: Returns List<T>::Reader. 637 // - List<T> for any T listed above: Returns List<T>::Reader.
634 // - DynamicEnum: Returns the corresponding type. 638 // - DynamicEnum: Returns the corresponding type.
635 // - DynamicStruct, DynamicList: Returns the corresponding Reader. 639 // - DynamicStruct, DynamicList: Returns the corresponding Reader.
636 // - Any capability type, including DynamicCapability: Returns the corresponding Client. 640 // - Any capability type, including DynamicCapability: Returns the corresponding Client.
641 // - DynamicValue: Returns an identical Reader. Useful to avoid special-casing in generic code.
637 // (TODO(perf): On GCC 4.8 / Clang 3.3, provide rvalue-qualified version that avoids 642 // (TODO(perf): On GCC 4.8 / Clang 3.3, provide rvalue-qualified version that avoids
638 // refcounting.) 643 // refcounting.)
639 // 644 //
640 // DynamicValue allows various implicit conversions, mostly just to make the interface friendlier. 645 // DynamicValue allows various implicit conversions, mostly just to make the interface friendlier.
641 // - Any integer can be converted to any other integer type so long as the actual value is within 646 // - Any integer can be converted to any other integer type so long as the actual value is within
1183 template <typename T> 1188 template <typename T>
1184 inline Orphan<T> AnyPointer::Builder::disownAs(InterfaceSchema schema) { 1189 inline Orphan<T> AnyPointer::Builder::disownAs(InterfaceSchema schema) {
1185 return _::PointerHelpers<T>::disown(builder, schema); 1190 return _::PointerHelpers<T>::disown(builder, schema);
1186 } 1191 }
1187 1192
1188 template <> 1193 // We have to declare the methods below inline because Clang and GCC disagree about how to mangle
1189 DynamicStruct::Builder Orphan<AnyPointer>::getAs<DynamicStruct>(StructSchema schema); 1194 // their symbol names.
1190 template <> 1195 template <>
1191 DynamicList::Builder Orphan<AnyPointer>::getAs<DynamicList>(ListSchema schema); 1196 inline DynamicStruct::Builder Orphan<AnyPointer>::getAs<DynamicStruct>(StructSchema schema) {
1192 template <> 1197 return DynamicStruct::Builder(schema, builder);
1193 DynamicCapability::Client Orphan<AnyPointer>::getAs<DynamicCapability>(InterfaceSchema schema); 1198 }
1194 template <> 1199 template <>
1195 DynamicStruct::Reader Orphan<AnyPointer>::getAsReader<DynamicStruct>(StructSchema schema) const; 1200 inline DynamicStruct::Reader Orphan<AnyPointer>::getAsReader<DynamicStruct>(
1196 template <> 1201 StructSchema schema) const {
1197 DynamicList::Reader Orphan<AnyPointer>::getAsReader<DynamicList>(ListSchema schema) const; 1202 return DynamicStruct::Reader(schema, builder);
1198 template <> 1203 }
1199 DynamicCapability::Client Orphan<AnyPointer>::getAsReader<DynamicCapability>( 1204 template <>
1200 InterfaceSchema schema) const; 1205 inline Orphan<DynamicStruct> Orphan<AnyPointer>::releaseAs<DynamicStruct>(StructSchema schema) {
1201 template <> 1206 return Orphan<DynamicStruct>(schema, kj::mv(builder));
1202 Orphan<DynamicStruct> Orphan<AnyPointer>::releaseAs<DynamicStruct>(StructSchema schema); 1207 }
1203 template <> 1208 template <>
1204 Orphan<DynamicList> Orphan<AnyPointer>::releaseAs<DynamicList>(ListSchema schema); 1209 inline DynamicList::Builder Orphan<AnyPointer>::getAs<DynamicList>(ListSchema schema) {
1205 template <> 1210 return DynamicList::Builder(schema, builder);
1206 Orphan<DynamicCapability> Orphan<AnyPointer>::releaseAs<DynamicCapability>( 1211 }
1207 InterfaceSchema schema); 1212 template <>
1213 inline DynamicList::Reader Orphan<AnyPointer>::getAsReader<DynamicList>(ListSchema schema) const {
1214 return DynamicList::Reader(schema, builder);
1215 }
1216 template <>
1217 inline Orphan<DynamicList> Orphan<AnyPointer>::releaseAs<DynamicList>(ListSchema schema) {
1218 return Orphan<DynamicList>(schema, kj::mv(builder));
1219 }
1220 template <>
1221 inline DynamicCapability::Client Orphan<AnyPointer>::getAs<DynamicCapability>(
1222 InterfaceSchema schema) {
1223 return DynamicCapability::Client(schema, builder.asCapability());
1224 }
1225 template <>
1226 inline DynamicCapability::Client Orphan<AnyPointer>::getAsReader<DynamicCapability>(
1227 InterfaceSchema schema) const {
1228 return DynamicCapability::Client(schema, builder.asCapability());
1229 }
1230 template <>
1231 inline Orphan<DynamicCapability> Orphan<AnyPointer>::releaseAs<DynamicCapability>(
1232 InterfaceSchema schema) {
1233 return Orphan<DynamicCapability>(schema, kj::mv(builder));
1234 }
1208 1235
1209 // ======================================================================================= 1236 // =======================================================================================
1210 // Inline implementation details. 1237 // Inline implementation details.
1211 1238
1212 template <typename T> 1239 template <typename T>
1406 static typename T::Client apply(Builder& builder) { 1433 static typename T::Client apply(Builder& builder) {
1407 return builder.as<DynamicCapability>().as<T>(); 1434 return builder.as<DynamicCapability>().as<T>();
1408 } 1435 }
1409 }; 1436 };
1410 1437
1438 template <>
1439 struct DynamicValue::Reader::AsImpl<DynamicValue> {
1440 static DynamicValue::Reader apply(const Reader& reader) {
1441 return reader;
1442 }
1443 };
1444 template <>
1445 struct DynamicValue::Builder::AsImpl<DynamicValue> {
1446 static DynamicValue::Builder apply(Builder& builder) {
1447 return builder;
1448 }
1449 };
1450
1411 inline DynamicValue::Pipeline::Pipeline(std::nullptr_t n): type(UNKNOWN) {} 1451 inline DynamicValue::Pipeline::Pipeline(std::nullptr_t n): type(UNKNOWN) {}
1412 inline DynamicValue::Pipeline::Pipeline(DynamicStruct::Pipeline&& value) 1452 inline DynamicValue::Pipeline::Pipeline(DynamicStruct::Pipeline&& value)
1413 : type(STRUCT), structValue(kj::mv(value)) {} 1453 : type(STRUCT), structValue(kj::mv(value)) {}
1414 inline DynamicValue::Pipeline::Pipeline(DynamicCapability::Client&& value) 1454 inline DynamicValue::Pipeline::Pipeline(DynamicCapability::Client&& value)
1415 : type(CAPABILITY), capabilityValue(kj::mv(value)) {} 1455 : type(CAPABILITY), capabilityValue(kj::mv(value)) {}
1506 return *this; 1546 return *this;
1507 } 1547 }
1508 template <> 1548 template <>
1509 inline DynamicList::Builder DynamicList::Builder::as<DynamicList>() { 1549 inline DynamicList::Builder DynamicList::Builder::as<DynamicList>() {
1510 return *this; 1550 return *this;
1551 }
1552
1553 template <>
1554 inline AnyList::Reader DynamicList::Reader::as<AnyList>() const {
1555 return AnyList::Reader(reader);
1556 }
1557
1558 template <>
1559 inline AnyList::Builder DynamicList::Builder::as<AnyList>() {
1560 return AnyList::Builder(builder);
1511 } 1561 }
1512 1562
1513 // ------------------------------------------------------------------- 1563 // -------------------------------------------------------------------
1514 1564
1515 template <typename T, typename> 1565 template <typename T, typename>