To map custom C++ data types to Jinja2 template values, you can implement interfaces that follow the patterns established by the IndexedListItemAccessorImpl, IndexedListAccessorImpl, and MapAccessorImpl templates. These adapters allow the Jinja2 engine to interact with your native C++ collections (like lists or maps) as if they were native Jinja2 types.
Key Adapter Patterns
1. Indexed Item Access (Single Element Access)
For types that allow accessing an element by an index (e.g., a single object that behaves like a container), implement the logic required by IndexedListItemAccessorImpl. You must provide:
GetItem(idx): Returns the item at the specified index.ItemsCountImpl(): Returns the total number of items.GetIndexer(): Returns a pointer to the indexer (usually this).
2. Indexed List Access (Collection Access)
For types representing a collection of items, implement IndexedListAccessorImpl. This provides both index-based access and iteration capabilities. You must provide:
GetItem(idx): Returns the item at the specified index.ItemsCountImpl(): Returns the total number of items.CreateListAccessorEnumerator(): Returns an enumerator for iterating over the collection.
3. Map Access (Key-Value Access)
For types representing key-value pairs (like a dictionary or map), implement MapAccessorImpl. You must provide:
GetItem(name): Returns the value associated with the string key name.
Enumerator Lifecycle
When implementing iterators for these collections, the engine uses an Enumerator pattern. The enumerator supports:
MoveNext(): Advances the iterator to the next item.GetCurrent(): Returns the current value in the iteration.Reset(): Resets the iterator to the beginning.Clone() / Move(): Allows for duplicating or transferring the state of the iterator.