GSLAM
3.0.0
|
The Svar class, A Tiny Modern C++ Header Brings Unified Interface for Different Languages. More...
Public Member Functions | |
Svar () | |
Default is Undefined. | |
Svar (SvarValue *v) | |
Construct from SvarValue pointer. | |
template<typename T > | |
Svar (const T &var) | |
Wrap any thing with template caster for user customize. | |
template<typename T > | |
Svar (std::unique_ptr< T > &&v) | |
Wrap an pointer use unique_ptr. | |
Svar (bool b) | |
Wrap bool to static instances. | |
Svar (int i) | |
Wrap a int. | |
Svar (double d) | |
Wrap a double. | |
Svar (const std::string &str) | |
Wrap a string. | |
Svar (const std::vector< Svar > &vec) | |
Construct an Array. | |
Svar (const std::map< std::string, Svar > &m) | |
Construct an Object. | |
Svar (const std::map< Svar, Svar > &m) | |
Construct a Dict. | |
Svar (const std::initializer_list< Svar > &init) | |
Construct from initializer. | |
template<typename Return , typename... Args, typename... Extra> | |
Svar (Return(*f)(Args...), const Extra &...extra) | |
Construct a cpp_function from a vanilla function pointer. | |
template<typename Return , typename Class , typename... arg, typename... Extra> | |
Svar (Return(Class::*f)(arg...), const Extra &...extra) | |
Construct a cpp_function from a class method (non-const) | |
template<typename Return , typename Class , typename... arg, typename... Extra> | |
Svar (Return(Class::*f)(arg...) const, const Extra &...extra) | |
Construct a cpp_function from a class method (const) | |
template<typename T > | |
bool | is () const |
Is holding a type T value? | |
bool | is (const std::type_index &typeId) const |
bool | is (const std::string &typeStr) const |
template<typename T > | |
const T & | as () const |
Should always check type with "is<T>()" first. | |
template<typename T > | |
T & | as () |
Return the value as type T, TAKE CARE when using this. The Svar should be hold during operations of the value. | |
template<typename T > | |
Svar | cast () const |
Success: return a Svar holding T instance, Failed: return Undefined. | |
Svar | cast (const std::string &typeStr) const |
template<typename T > | |
detail::enable_if_t< std::is_pointer< T >::value, T > | castAs () |
No cast is performed, directly return the c++ pointer, throw SvarException when failed. | |
template<typename T > | |
detail::enable_if_t< std::is_reference< T >::value, T & > | castAs () |
No cast is performed, directly return the c++ reference, throw SvarException when failed. | |
template<typename T > | |
detail::enable_if_t<!std::is_reference< T >::value &&!std::is_pointer< T >::value, T > | castAs () const |
Cast to c++ type T and return, throw SvarException when failed. | |
bool | isUndefined () const |
bool | isNull () const |
bool | isFunction () const |
bool | isClass () const |
bool | isException () const |
bool | isObject () const |
bool | isArray () const |
bool | isDict () const |
Svar | clone (int depth=0) const |
std::string | typeName () const |
Return the value typename. | |
std::type_index | cpptype () const |
Return the value typeid. | |
const Svar & | classObject () const |
Return the class singleton, return Undefined when failed. | |
SvarClass * | classPtr () const |
Return the class singleton pointer, return nullptr when failed. | |
size_t | length () const |
Return the item numbers when it is an array, object or dict. | |
size_t | size () const |
template<typename T > | |
T & | get (const std::string &name, T def, bool parse_dot=false) |
Force to return the children as type T, cast is performed, otherwise the old value will be droped and set to the value "def". | |
Svar | get (const std::string &name, Svar def, bool parse_dot=false) |
template<typename T > | |
void | set (const std::string &name, const T &def, bool parse_dot=false) |
Set the child "name" to "create<T>(def)". | |
bool | exist (const Svar &id) const |
For Object: dot compute and find the member For Array, Dict and Class, check if the item is not Undefined without dot compute. | |
void | erase (const Svar &id) |
For Object: dot compute and call delitem Others : directly call delitem | |
void | push_back (const Svar &rh) |
Append item when this is an array. | |
template<typename... Args> | |
Svar | call (const std::string function, Args...args) const |
Call function or class with name and arguments. | |
template<typename... Args> | |
Svar | operator() (Args...args) const |
Call this as function or class. | |
Svar & | def (const std::string &name, Svar funcOrClass) |
Define a class or function with name. | |
template<typename Func > | |
Svar & | def (const std::string &name, Func &&f) |
Define a lambda function with name. | |
std::vector< std::string > | parseMain (int argc, char **argv) |
Parse the "main(int argc,char** argv)" arguments. | |
bool | parseFile (const std::string &file_path) |
template<typename T > | |
T | arg (const std::string &name, T def, const std::string &help) |
Register default required arguments. | |
std::string | helpInfo () |
Format print version, usages and arguments as string. | |
int | help () |
Format print version, usages and arguments. | |
Svar | operator- () const |
Svar | operator+ (const Svar &rh) const |
Svar | operator- (const Svar &rh) const |
Svar | operator* (const Svar &rh) const |
Svar | operator/ (const Svar &rh) const |
Svar | operator% (const Svar &rh) const |
Svar | operator^ (const Svar &rh) const |
Svar | operator| (const Svar &rh) const |
Svar | operator& (const Svar &rh) const |
bool | operator== (const Svar &rh) const |
bool | operator< (const Svar &rh) const |
bool | operator!= (const Svar &rh) const |
bool | operator> (const Svar &rh) const |
bool | operator<= (const Svar &rh) const |
bool | operator>= (const Svar &rh) const |
Svar | operator[] (const Svar &i) const |
Svar & | operator[] (const Svar &name) |
template<typename T > | |
detail::enable_if_t< std::is_copy_assignable< T >::value, Svar & > | operator= (const T &v) |
template<typename T > | |
detail::enable_if_t<!std::is_copy_assignable< T >::value, Svar & > | operator= (const T &v) |
operator std::string () | |
const std::shared_ptr< SvarValue > & | value () const |
Return the raw holder. | |
Svar & | getOrCreate (const std::string &name, bool parse_dot=false) |
This is dangerous since the returned Svar may be free by other threads, TAKE CARE! | |
template<typename T > | |
T | Arg (const std::string &name, T def, const std::string &help) |
std::vector< std::string > | ParseMain (int argc, char **argv) |
bool | ParseFile (const std::string &file_path) |
template<typename T > | |
T & | Get (const std::string &name, T def=T()) |
Svar | Get (const std::string &name, Svar def=Svar()) |
int & | GetInt (const std::string &name, int def=0) |
double & | GetDouble (const std::string &name, double def=0) |
std::string & | GetString (const std::string &name, std::string def="") |
void *& | GetPointer (const std::string &name, void *def=nullptr) |
template<typename T > | |
void | Set (const std::string &name, const T &def) |
template<typename T > | |
void | Set (const std::string &name, const T &def, bool overwrite) |
Static Public Member Functions | |
template<typename Func , typename... Extra> | |
static Svar | lambda (Func &&f, const Extra &...extra) |
Construct a cpp_function from a lambda function (possibly with internal state) | |
template<class T > | |
static Svar | create (const T &t) |
Create any other c++ type instance, T need to be a copyable type. | |
template<class T > | |
static Svar | create (T &&t) |
static Svar | object (const std::map< std::string, Svar > &m={}) |
Create an Object instance. | |
static Svar | array (const std::vector< Svar > &vec={}) |
Create an Array instance. | |
static Svar | dict (const std::map< Svar, Svar > &m={}) |
Create a Dict instance. | |
static Svar | json (const std::string &str) |
Create from Json String. | |
static std::string | printTable (std::vector< std::pair< int, std::string >> line) |
Format print strings as table. | |
static const Svar & | Undefined () |
Undefined is the default value when Svar is not assigned a valid value It corrosponding to the c++ void and means no return. | |
static const Svar & | Null () |
Null is corrosponding to the c++ nullptr. | |
static const Svar & | True () |
static const Svar & | False () |
static Svar & | instance () |
static Svar | loadFile (const std::string &file_path) |
template<typename T > | |
static std::string | type_id () |
template<typename T > | |
static std::string | toString (const T &v) |
template<typename T > | |
static detail::enable_if_t< detail::has_loading_support< std::istream, T >::value, T > | fromString (const std::string &str, const T &def) |
template<typename T > | |
static detail::enable_if_t<!detail::has_loading_support< std::istream, T >::value, T > | fromString (const std::string &str, const T &def) |
static std::string | getFileName (const std::string &path) |
Public Attributes | |
std::shared_ptr< SvarValue > | _obj |
Friends | |
std::ostream & | operator<< (std::ostream &ost, const Svar &self) |
Dump this as Json style outputs. | |
std::istream & | operator>> (std::istream &ist, Svar &self) |
The Svar class, A Tiny Modern C++ Header Brings Unified Interface for Different Languages.
By Using Svar, your C++ library can be called easily with different languages like C++, Java, Python and Javascript.
Svar brings the following features:
Svar natually support JSON and here is some basic usage demo:
Svar provides argument parsing functional with JSON configuration loading. Here is a small demo shows how to use Svar for argument parsing:
When you use "--help" or "-help", the terminal should show the below introduction:
"help" and "conf" is two default parameters and users can use "conf" to load JSON file for configuration loading.
Svar supports the following parsing styles:
Svar can not only hold JSON types, but also any other c++ elements including functions, classes and objects.
Svar does not do magic, so users need to bind functions mannually like boost or pybind11 did. Fortunately, Svar provided utils to bind those functions very conveniencely.
As used in last section, there are three types of functions: constructor, member function and static member function. And Svar also support lambda expression of functions so that users able to extend functions more easily.
Svar supports to call functions with operators, such as for int, we got some math operators and we want to use it directly with Svar:
The above operation is available because Svar builtin defined as following:
If you want your own class support these operators, you need to define you own implementations. The following table listed some operators that often used.
Operator | Function Name |
---|---|
+ | "__add__" |
- | "__sub__" |
* | "__mul__" |
/ | "__div__" |
% | "__mod__" |
& | "__and__" |
| | "__or__" |
^ | "__xor__" |
= | "__eq__" |
!= | "__nq__" |
< | "__lt__" |
> | "__gt__" |
<= | "__le__" |
>= | "__ge__" |
[] | "__getitem__" |
One of the most important design target of Svar is to export a shared library that can be used by different languages.
The only thing needs to do is to put what you want to the singleton of Svar: Svar::instance(), which is defined as 'svar', and expose the symbol with macro EXPORT_SVAR_INSTANCE.
Compile this file to a shared library "libsample.so" or "sample.dll", and we are going to access this module with Svar later.
A Svar module is designed to be self maintained, which means the module can be called without header or documentation. Since SvarFunction auto generated function signatures, so that users are able to know how to call Svar functions.
One can use the 'svar' application from https://github.com/zdzhaoyong/Svar, or the "gslam" application of https://github.com/zdzhaoyong/GSLAM to access the context.
We are able to load the Svar instance exported by marco with EXPORT_SVAR_INSTANCE using Registry::load().