6#include <bond/core/config.h>
8#include "detail/once.h"
9#include "detail/tags.h"
10#include "reflection.h"
11#include "runtime_schema.h"
13#include <boost/bind/bind.hpp>
14#include <boost/make_shared.hpp>
25 : schema(schema.schema),
27 instance(schema.instance)
36 : schema(schema.get()),
42 : schema(schema.schema),
44 instance(schema.instance)
48 : schema(schema.schema),
50 instance(schema.instance)
53inline bool RuntimeSchema::HasBase()
const
55 return !GetStruct().base_def.empty();
63inline const StructDef& RuntimeSchema::GetStruct()
const
65 BOOST_ASSERT(type->id == BT_STRUCT);
66 return schema->structs[type->struct_def];
69inline BondDataType RuntimeSchema::GetTypeId()
const
75template <
typename Protocols = BuiltInProtocols,
typename Writer>
78 Apply<Protocols>(SerializeTo<Protocols>(output), schema.GetSchema());
82template <
typename Protocols = BuiltInProtocols,
typename Writer>
85 Apply<Protocols>(MarshalTo<Protocols>(output), schema.GetSchema());
92 inline uint16_t schema_depth(
const RuntimeSchema& schema)
97 depth += schema_depth(schema.GetBaseSchema());
102 template <
typename T,
typename Unused =
void>
106 static const SchemaDef& Get()
116 call_once(flag, boost::bind(&AppendStructDef, &schema));
120 static void AppendStructDef(SchemaDef* s);
123 static SchemaDef schema;
124 static once_flag flag;
131 template <
typename Unused>
132 class SchemaCache<Unknown, Unused>
135 static const SchemaDef& Get()
140 static SchemaDef NewSchemaDef()
144 s.root.id = BT_STRUCT;
150 static const SchemaDef schema;
153 template <
typename T,
typename Unused>
154 SchemaDef SchemaCache<T, Unused>::schema;
156 template <
typename Unused>
157 const SchemaDef SchemaCache<Unknown, Unused>::schema
158 = SchemaCache<Unknown>::NewSchemaDef();
160 template <
typename T,
typename Unused>
161 once_flag SchemaCache<T, Unused>::flag;
167 using Parser = StaticParser<SchemaReader&>;
170 bool CanReadArray(uint32_t )
const
177template <
typename Unused>
struct
178uses_marshaled_bonded<SchemaReader&, Unused> : std::false_type
189 InitSchemaDef(SchemaDef& schema)
191 _struct_def(schema.structs.size())
193 _schema.structs.push_back(StructDef());
197 void Begin(
const Metadata& metadata)
const
199 This().metadata = metadata;
206 template <
typename T>
207 bool Base(
const T& )
const
209 TypeDef type = GetTypeDef<typename remove_bonded<T>::type>();
210 This().base_def.set(type);
215 template <
typename T>
216 bool Field(uint16_t
id,
const Metadata& metadata,
const T&)
const
221 field.metadata = metadata;
222 field.type = GetTypeDef<typename remove_bonded_value<T>::type>();
224 This().fields.push_back(field);
229 template <
typename T>
230 typename boost::enable_if<is_basic_type<T>, TypeDef>::type
235 type.id = get_type_id<T>::value;
241 template <
typename T>
242 typename boost::enable_if_c<is_container<T>::value && !is_map_container<T>::value, TypeDef>::type
247 type.id = get_type_id<T>::value;
248 type.element.set() = GetTypeDef<typename element_type<T>::type>();
254 template <
typename T>
255 typename boost::enable_if<is_map_container<T>, TypeDef>::type
260 type.id = get_type_id<T>::value;
261 type.key.set() = GetTypeDef<typename element_type<T>::type::first_type>();
262 type.element.set() = GetTypeDef<typename element_type<T>::type::second_type>();
268 template <
typename T>
269 typename boost::enable_if<is_bond_type<T>, TypeDef>::type
274 type.id = get_type_id<T>::value;
275 type.struct_def = GetStructDef<typename remove_bonded<T>::type>();
276 type.bonded_type = is_bonded<T>::value;
282 template <
typename T>
283 uint16_t GetStructDef()
const
285 const auto& structs = _schema.structs;
287 BOOST_ASSERT(structs.size() <= (std::numeric_limits<uint16_t>::max)());
289 auto it = std::find_if(
292 [](
const StructDef& def)
294 return def.metadata.qualified_name == schema<T>::type::metadata.qualified_name;
297 const auto index =
static_cast<uint16_t
>(std::distance(std::begin(structs), it));
299 if (it == std::end(structs))
301 detail::SchemaCache<T>::AppendStructDef(&_schema);
312 StructDef& This()
const
314 BOOST_ASSERT(_schema.structs.size() > _struct_def);
315 return _schema.structs[_struct_def];
320 const size_t _struct_def;
326template <
typename T,
typename Unused>
327void SchemaCache<T, Unused>::AppendStructDef(SchemaDef* s)
329 Apply<T>(InitSchemaDef{ *s });
344 return RuntimeSchema(detail::SchemaCache<T>::Get());
350 if (!schema.GetType().element.empty())
351 return RuntimeSchema(schema, schema.GetType().element.value());
359 if (!schema.GetType().key.empty())
367template <
typename T,
typename Map = std::map<T, std::
string> >
370 return GetValueToNameMap(T{}, detail::mpl::identity<Map>{});
375template <
typename T,
typename Map = std::map<std::
string, T> >
378 return GetNameToValueMap(T{}, detail::mpl::identity<Map>{});
Represents runtime schema See User's Manual
Definition runtime_schema.h:26
RuntimeSchema()
Default constructor.
Definition runtime_schema.h:29
namespace bond
Definition apply.h:17
const Map & GetEnumNames()
Returns a const reference to a map of names for a user defined enum.
Definition schema.h:376
RuntimeSchema GetRuntimeSchema()
Returns an instance of RuntimeSchema for a user defined struct.
Definition schema.h:336
void Serialize(const T &obj, Writer &output)
Serialize an object using a protocol writer.
Definition bond.h:20
void Marshal(const T &obj, Writer &output)
Marshal an object using a protocol writer.
Definition bond.h:64
const Map & GetEnumValues()
Returns a const reference to a map of values for a user defined enum.
Definition schema.h:368