Tech/LxEngine/lxvar
From Athile
It may contain inaccurate, incorrect information. Use at your own risk.
The lxvar class is intended as a simple, convenient class for dealing with arbitrary JSON-like data with reasonable efficiency. In addition to supporting the majority of standard Javascript 'var' functionality, the class also supports various features to aid in intermixing with native C++ types as well as persisting, validating, and responding to value changes.
Contents |
Example Usage
Primitive types
Support for basic types: bool, int, float, double, and std::string.
lxvar v; // default value is "undefined" // Can store the following basic types v = true; v = 1; v = 1.0f; v = 1.0; v = "one"; // Explicit conversion bool b = v.as<bool>(); int i = v.as<int>(); float f = v.as<float>(); std::string s = v.as<std::string>(); // Implicit type conversion int i = v; // same as v.as<int>() float f = v; // same as v.as<float>()
1 Implicit conversion is usually frowned upon as a C++ coding technique, as it "hides" what is happening from the reader; however, for a convenience class like lxvar, implicit conversions were deemed a good fit.
Arrays, Maps
to be completed
Map Specializations
In addition to a standard map implementation, other specialized map implementations are available. These provide the same map interface but also provide additional functionality:
-
lxvar::orderedmap() -
lxvar::decoratedmap()
orderedmap
The orderermap implementation behaves like a standard map with one exception: the iteration order is equivalent to the insertion order of the keys. If a value is overwritten for a key, the key retains its original order.
The ordermap requires slightly more memory and is slightly less efficient than a standard map.
decoratedmap
to be completed
Objects
The wrap and unwrap2 functions can be used to store native C++ types in an lxvar.
lxvar data; data["position"] = lxvar::wrap(glgeom::point3f(1, 2, 3)); auto& position = data["position"].unwrap2<glgeom::point3f>();
Interface
Class
Functions
query( string path ) -> lxvar
Searches the lxvar for a given property (using JSON syntax) and returns the value of that property. If that property, or any part of the property path, does not exist, then undefined is returned.
- Using this method alone, there is no way to distinguish between a property that exists and is set to undefined and a property that does not exist and thus returns undefined.
query( string path, type default_value ) -> lxvar
The same as the alternate version of query but returns the provided default if the property does not exist or is of an incompatible type.