rapidyaml  0.16.0
parseandemitYAML,anddoitfast
Serialize container types

Serialization definitions used in sample_user_container_types() More...

Classes

struct  my_seq_type< T >
 example user container type: seq-like More...
 
struct  my_map_type< K, V >
 example user container type: map-like More...
 
struct  my_type
 example user container type with nested user types. More...
 

Functions

template<class T >
void write (ryml::Tree *tree, ryml::id_type id, my_seq_type< T > const &seq)
 
void write (ryml::Tree *tree, ryml::id_type id, my_seq_type< std::string > const &seq)
 
template<class K , class V >
void write (ryml::Tree *tree, ryml::id_type id, my_map_type< K, V > const &map)
 
void write (ryml::Tree *tree, ryml::id_type id, my_type const &val)
 
template<class T >
ryml::ReadResult read (ryml::Tree const *tree, ryml::id_type id, my_seq_type< T > *seq)
 
template<class K , class V >
ryml::ReadResult read (ryml::Tree const *tree, ryml::id_type id, my_map_type< K, V > *map)
 
ryml::ReadResult read (ryml::ConstNodeRef const &n, my_type *val)
 
ryml::ReadResult read (ryml::Tree const *tree, ryml::id_type id, my_type *val)
 

Detailed Description

Serialization definitions used in sample_user_container_types()

Function Documentation

template<class T >
void write ( ryml::Tree *  tree,
ryml::id_type  id,
my_seq_type< T > const &  seq 
)

Definition at line 4245 of file quickstart.cpp.

4246 {
4247  tree->set_seq(id);
4248  for(T const& v : seq.seq_member)
4249  {
4250  // inside write(), prefer using .set_serialized() instead of .save()
4251  tree->set_serialized(tree->append_child(id), v);
4252  }
4253 }
std::vector< T > seq_member
id_type append_child(id_type parent)
create and insert a node as the last child of parent
Definition: tree.hpp:1180
void set_serialized(id_type node, T const &val) RYML_NOEXCEPT
Definition: tree.hpp:823
void set_seq(id_type node) RYML_NOEXCEPT
Definition: tree.hpp:720
void write ( ryml::Tree *  tree,
ryml::id_type  id,
my_seq_type< std::string > const &  seq 
)

Definition at line 4257 of file quickstart.cpp.

4258 {
4259  tree->set_seq(id);
4260  for(std::string const& v : seq.seq_member)
4261  {
4262  // now the tree is pointing at seq's strings. using .set_val()
4263  // does not serialize, and this avoids the string copy to the
4264  // tree's arena
4265  tree->set_val(tree->append_child(id), ryml::to_csubstr(v));
4266  }
4267 }
void set_val(id_type node, csubstr val) RYML_NOEXCEPT
Definition: tree.hpp:688
std::vector< T > seq_member
id_type append_child(id_type parent)
create and insert a node as the last child of parent
Definition: tree.hpp:1180
csubstr to_csubstr(const char(&s)[N]) noexcept
Definition: substr.hpp:2380
void set_seq(id_type node) RYML_NOEXCEPT
Definition: tree.hpp:720
template<class K , class V >
void write ( ryml::Tree *  tree,
ryml::id_type  id,
my_map_type< K, V > const &  map 
)

Definition at line 4271 of file quickstart.cpp.

4272 {
4273  tree->set_map(id);
4274  for(auto const& v : map.map_member)
4275  {
4276  // inside write(), prefer using .set_serialized() instead of .save()
4277  ryml::id_type child_id = tree->append_child(id);
4278  tree->set_key_serialized(child_id, v.first); // we're serializing the key!
4279  tree->set_serialized(child_id, v.second);
4280  }
4281 }
void set_map(id_type node) RYML_NOEXCEPT
Definition: tree.hpp:731
std::map< K, V > map_member
void set_key_serialized(id_type node, T const &key) RYML_NOEXCEPT
Definition: tree.hpp:837
id_type append_child(id_type parent)
create and insert a node as the last child of parent
Definition: tree.hpp:1180
RYML_ID_TYPE id_type
The type of a node id in the YAML tree; to override the default type, define the macro RYML_ID_TYPE t...
Definition: common.hpp:124
void set_serialized(id_type node, T const &val) RYML_NOEXCEPT
Definition: tree.hpp:823
void write ( ryml::Tree *  tree,
ryml::id_type  id,
my_type const &  val 
)

Definition at line 4283 of file quickstart.cpp.

4284 {
4285  tree->set_map(id);
4286  // inside write(), prefer using .set_serialized() instead of .save()
4287  //
4288  ryml::id_type ch;
4289  // these are leaf nodes:
4290  ch = tree->append_child(id); tree->set_key(ch, "v2"); tree->set_serialized(ch, val.v2);
4291  ch = tree->append_child(id); tree->set_key(ch, "v3"); tree->set_serialized(ch, val.v3);
4292  ch = tree->append_child(id); tree->set_key(ch, "v4"); tree->set_serialized(ch, val.v4);
4293  // these are container nodes (note how the call is equal):
4294  ch = tree->append_child(id); tree->set_key(ch, "seq"); tree->set_serialized(ch, val.seq);
4295  ch = tree->append_child(id); tree->set_key(ch, "map"); tree->set_serialized(ch, val.map);
4296  // Note above that we're NOT serializing the keys. That works and
4297  // is correct here because the keys themselves are fixed, and are
4298  // static strings located in the executable. But if the keys came
4299  // from the data, they too would have to be serialized with
4300  // .set_key_serialized().
4301 }
void set_map(id_type node) RYML_NOEXCEPT
Definition: tree.hpp:731
id_type append_child(id_type parent)
create and insert a node as the last child of parent
Definition: tree.hpp:1180
RYML_ID_TYPE id_type
The type of a node id in the YAML tree; to override the default type, define the macro RYML_ID_TYPE t...
Definition: common.hpp:124
void set_serialized(id_type node, T const &val) RYML_NOEXCEPT
Definition: tree.hpp:823
void set_key(id_type node, csubstr key) RYML_NOEXCEPT
Definition: tree.hpp:705
template<class T >
ryml::ReadResult read ( ryml::Tree const *  tree,
ryml::id_type  id,
my_seq_type< T > *  seq 
)

Definition at line 4313 of file quickstart.cpp.

4314 {
4315  if(!tree->is_seq(id)) return ryml::ReadResult(id); // id must be a seq
4316  seq->seq_member.clear(); // we'll overwrite the vector
4317  for(ryml::id_type child = tree->first_child(id);
4318  child != ryml::NONE;
4319  child = tree->next_sibling(child))
4320  {
4321  // create a new entry
4322  seq->seq_member.emplace_back();
4323  // inside read() you SHOULD NOT use .load() because of its
4324  // exceptional flow. Instead, you should use .deserialize() to
4325  // play nice with .deserialize() callers calling this function.
4326  // Read more at the doxygen page linked above.
4327  ryml::ReadResult r = tree->deserialize(child, &seq->seq_member.back());
4328  if(!r) return r; // return the inner-most result
4329  }
4330  return ryml::ReadResult(); // all good
4331 }
an index to none
Definition: common.hpp:131
A lightweight truthy type, used to enable reporting the offending node when a deserializing error hap...
Definition: common.hpp:161
std::vector< T > seq_member
RYML_ID_TYPE id_type
The type of a node id in the YAML tree; to override the default type, define the macro RYML_ID_TYPE t...
Definition: common.hpp:124
template<class K , class V >
ryml::ReadResult read ( ryml::Tree const *  tree,
ryml::id_type  id,
my_map_type< K, V > *  map 
)

Definition at line 4334 of file quickstart.cpp.

4335 {
4336  if(!tree->is_map(id)) return ryml::ReadResult(id); // id must be a seq
4337  for(ryml::id_type child = tree->first_child(id);
4338  child != ryml::NONE;
4339  child = tree->next_sibling(child))
4340  {
4341  K k{};
4342  // again, we're using .deserialize() instead of .load()
4343  // because (1) we should gracefully return a ReadResult and
4344  // anyway (2) we're certain the node is readable (because
4345  // we're inside the loop).
4346  ryml::ReadResult r = tree->deserialize_key(child, &k);
4347  if(r) r = tree->deserialize(child, &map->map_member[std::move(k)]);
4348  if(!r) return r; // return the inner-most result
4349  }
4350  return ryml::ReadResult(); // all good.
4351 }
an index to none
Definition: common.hpp:131
A lightweight truthy type, used to enable reporting the offending node when a deserializing error hap...
Definition: common.hpp:161
std::map< K, V > map_member
RYML_ID_TYPE id_type
The type of a node id in the YAML tree; to override the default type, define the macro RYML_ID_TYPE t...
Definition: common.hpp:124
ryml::ReadResult read ( ryml::ConstNodeRef const &  n,
my_type *  val 
)

Definition at line 4369 of file quickstart.cpp.

4370 {
4371  ryml::ReadResult r(n.is_map(), n.id()); // node must be a map
4372  if(r) r = n.deserialize_child("v2", &val->v2);
4373  if(r) r = n.deserialize_child("v3", &val->v3);
4374  if(r) r = n.deserialize_child("v4", &val->v4);
4375  if(r) r = n.deserialize_child("seq", &val->seq);
4376  if(r) r = n.deserialize_child("map", &val->map);
4377  // hint: you can also add a default argument for when no such child exists
4378  // hint: you can also use indices instead of keys
4379  return r;
4380 }
my_map_type< int, int > map
A lightweight truthy type, used to enable reporting the offending node when a deserializing error hap...
Definition: common.hpp:161
vec3< int > v3
vec4< int > v4
my_seq_type< int > seq
vec2< int > v2
ryml::ReadResult read ( ryml::Tree const *  tree,
ryml::id_type  id,
my_type *  val 
)

Definition at line 4387 of file quickstart.cpp.

4388 {
4389  ryml::ReadResult r(tree->is_map(id), id); // node must be a map
4390  if(r) r = tree->deserialize_child(id, "v2", &val->v2);
4391  if(r) r = tree->deserialize_child(id, "v3", &val->v3);
4392  if(r) r = tree->deserialize_child(id, "v4", &val->v4);
4393  if(r) r = tree->deserialize_child(id, "seq", &val->seq);
4394  if(r) r = tree->deserialize_child(id, "map", &val->map);
4395  // hint: you can also add a default argument for when no such child exists
4396  // hint: you can also use indices instead of keys
4397  return r;
4398 }
my_map_type< int, int > map
A lightweight truthy type, used to enable reporting the offending node when a deserializing error hap...
Definition: common.hpp:161
vec3< int > v3
vec4< int > v4
my_seq_type< int > seq
vec2< int > v2