omni::structuredlog::JsonTreeSizeCalculator

Defined in omni/structuredlog/JsonTree.h

class JsonTreeSizeCalculator

Class for determining the allocation size required to build a JSON tree in a single block of memory.

To use this, you simply track all of the items that you will store in your tree, then you can retrieve the size that is required to store this tree. This final size can be used to allocate a block for a BlockAllocator to allocate a tree of the exact correct size.

Note

This size calculator rounds up all sizes to the nearest alignment so that allocator can always return properly aligned allocations. Because of this, ordering of the track calls does not have to exactly match the ordering of the setNode() calls.

Public Functions

inline size_t getSize()

Get the size required for the tree.

Returns

The size in bytes.

inline void trackRoot()

Track the root node in the tree.

Remark

Call this if you’re planning on allocating the root node of the tree, rather than keeping it as a local variable or something like that.

inline void trackObject(size_t propertyCount)

Track the size of a JSON object node.

Parameters

propertyCount[in] The number of properties that JSON node has. For example {"a": 0, "b": 2} is an object with 2 properties.

inline void trackObjectArray(size_t propertyCount, size_t len)

Track the size for a JSON array of objects.

Parameters
  • propertyCount[in] The number of properties that each object element has. This implies that each element will be an object with the same layout. For an array of objects with varying layouts, trackObject() would need to be called for each element.

  • len[in] The length of the object array. (e.g. [{}, {}] is length 2).

inline void trackName(const char *name, uint16_t nameLen)

Track the size occupied by the node name.

Parameters
  • name[in] The node name.

  • nameLen[in] The length of name including the null terminator.

inline void trackName(const char *name)

Track the size occupied by the node name.

Parameters

name[in] The node name.

Returns

The number of bytes required to encode name.

inline void track()

Track the size of a node without any associated data.

Remark

This is useful when using the JsonNode structure for defining a schema, so each node may not store a value, just type information and a name.

template<typename T>
inline void track(T value)

Track the size of an arithmetic type node.

Parameters

value[in] The value that will be encoded in the future.

inline void track(const char *const *str, uint16_t len)

track the size of a string array node.

Parameters
  • str[in] The array of strings that will be encoded.

  • len[in] The length of array b.

template<typename T>
inline void track(const T *value, uint16_t len)

Track the size of an array node with the string length pre-calculated.

Parameters
  • value[in] The array that will be used for the node.

  • len[in] The length of value. If value is a string, this includes the null terminator.

inline void track(const void *value, uint16_t len)

Track the size of a binary blob node.

Parameters
  • value[in] The array of binary data that will be used for the node.

  • len[in] The length of value.

inline void track(void *value, uint16_t len)

Track the size of a binary blob node.

Parameters
  • value[in] The array of binary data that will be used for the node.

  • len[in] The length of value.

inline void track(const char *str)

track the size of a string node.

Parameters

str[in] The string to be encoded. This string must be less than 64KiB.

inline void track(char *str)

track the size of a string node.

Parameters

str[in] The string to be encoded.

inline void track(const JsonNode *node)

Track the size required for a deep copy of a node.

Note

This includes the size required for the root node.

Parameters

node[in] The node to calculate the size for.

inline void track(JsonNode *node)

Track the size required for a deep copy of a node.

Note

This includes the size required for the root node.

Parameters

node[in] The node to calculate the size for.