Skip to content

Pointers

Felix Gündling edited this page Oct 9, 2019 · 1 revision

cista::ptr<T> is just a typedef to T* in cista::raw but a special cista::offset_ptr<T> in cista::offset. In both cases, it can only point to null or to a value stored in the serialized buffer. Pointing to a value within the serialized buffer requires that the offset it was written at is known at serialization time.

There are three ways to index an address in order to serialize a pointer to it:

  • cista::unique_ptr<T>: Every cista::unique_ptr<T> will be indexed. Thus, pointing to values held by a cista::unique_ptr<T> is possible.
  • cista::indexed_vector<T>: Within a cista::indexed_vector<T>, every value can be referenced. This is more efficient than a cista::vector<cista::unique_ptr<T>>. However, cista::vector<T> and cista::indexed_vector<T> do not provide pointer stability after non-const operations such as resize, or emplace_back.
  • cista::indexed<T>: To be able to point to the value of member variables, it is possible to use cista::indexed<T>. cista::indexed<T> inherits from T and thus can be used just like a T.

An example using cista::indexed_vector<T> and cista::indexed<T>:

namespace data = cista::offset;

struct node;

struct edge {
  data::ptr<node> from_;
  data::ptr<node> to_;
};

struct node {
  uint32_tid_{0};
  data::vector<data::ptr<edge>> edges_;
  cista::indexed<data::string> name_;
};

struct graph {
  data::indexed_vector<node> nodes_;
  data::indexed_vector<edge> edges_;
  data::vector<data::ptr<data::string>> node_names_;
};