# Svar **Repository Path**: VisionDeveloper/Svar ## Basic Information - **Project Name**: Svar - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2022-07-02 - **Last Updated**: 2024-12-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Svar, A Tiny Modern C++ Header Brings Unified Interface for Different Languages --- [![Build Status](https://travis-ci.org/zdzhaoyong/Svar.svg?branch=master)](https://travis-ci.org/zdzhaoyong/Svar) [![Build Status](https://circleci.com/gh/zdzhaoyong/Svar.svg?style=svg)](https://circleci.com/gh/zdzhaoyong/Svar) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/9f6efa9443de498fa91a4e99f632dbd2)](https://www.codacy.com/app/zdzhaoyong/Svar) [![License](https://img.shields.io/badge/license-BSD--2--Clause-blue.svg)](./LICENSE) [![Version](https://img.shields.io/github/release/zdzhaoyong/Svar.svg)](https://github.com/zdzhaoyong/Svar/releases) - [Why Svar](#why-svar) - [Compile and Install](#compile-and-install) - [Usages](#usages) - [Perform tests](#perform-tests) - [Use Svar like JSON](#use-svar-like-json) - [Use Svar for Argument Parsing](#use-svar-for-argument-parsing) - [Svar Holding Everything](#svar-holding-everything) - [Hold Class Instances](#hold-class-instances) - [Hold Functions](#hold-functions) - [Hold and Use a Class](#hold-and-use-a-class) - [Create and Use a Svar Module](#create-and-use-a-svar-module) - [Create a Svar Module](#create-a-svar-module) - [The Svar Module Documentation](#the-svar-module-documentation) - [Import and Use a Svar Module](#import-and-use-a-svar-module) - [Use Svar Module in Other Languages](#use-svar-module-in-other-languages) - [Supported Compilers](#supported-compilers) - [Contact and Donation](#contact-and-donation) - [License](#license) --- # Quick start
1. Bind C++ to hello.so 2. Import by C++ 3. Import by Python
#include <Svar.h>
 
void say(std::string v){
  std::cerr<<v<<std::endl;
}
REGISTER_SVAR_MODULE(hello)
{
  svar["say"] = say;
}
EXPORT_SVAR_INSTANCE
#include <Svar.h>
 
auto m=svar.import("hello");
int main(){
  m["say"]("hello world");
  return 0;
}
import svar
hello = svar.load("hello")
hello.say("hello world")
4. Import by Javascript 5. C++ import Python 6. Javascript import Python
svar = require('./svar')
hello =  svar('hello')
hello.say("hello world")
#include <Svar.h>
 
auto py=svar.import("svarpy");
int main(){
  auto os = py["import]("os");
  std::cout<<"Pid is:"
  <<os["getpid"]();
  return 0;
}
svar=require("./svar")
python=svar("svarpy")
os=python.import("os")
print("Pid is", os.getpid())
# Why Svar? Svar is a powerful tiny modern c++ header implemented the following functionals: - JSON with buffer: Svar natually support JSON, but more than JSON, where JSON is only a subset of Svar data structure. - Argument parsing: Svar manages parameter in tree style, so that configuration in JSON format can be supported. - More than std::any after c++17: Svar is able to hold everything, including variables, functions, and classes. They can be used directly without declare header, just like writing Python or JavaScript! - A general plugin form. The released library comes with documentation, making *.h header file interface description unnecessary. - Auto multi-languages api. By Using Svar, your C++ library can be called easily with different languages like C++, Java, Python and Javascript. # Compile and Install Obtain source code from github ``` git clone https://github.com/zdzhaoyong/Svar ``` Compile the source code with cmake: ``` cd Svar mkdir build cd build cmake .. -DCMAKE_BUILD_TYPE=Release make sudo make install ``` # Usages ## Perform tests Usage help: ``` svar --help ``` ``` svar tests ``` ## Use Svar like JSON Svar natually support JSON and here is some basic usage demo: ``` Svar null=nullptr; Svar b=false; Svar i=1; Svar d=2.1; Svar s="hello world"; Svar v={1,2,3}; Svar m={{"b",false},{"s","hello world"},{"n",nullptr},{"u",Svar()}}; Svar obj; obj["m"]=m; obj["pi"]=3.14159; std::cout<()) // use is to check type std::cout<<"raw string is "<()<();// use castAs, this may throw SvarException for(auto it:v) std::cout< it:m) std::cout<("i",0,"This is a demo int parameter"); bool bv=svar.arg("b.dump",false,"Svar supports tree item assign"); Svar m =svar.arg("obj",Svar(),"Svar support json parameter"); if(svar.get("help",false)){ svar.help(); return 0; } if(svar["b"]["dump"].as()) std::cerr< sample_use --help Usage: sample_use [--help] [-conf configure_file] [-arg_name arg_value]... Using Svar supported argument parsing. The following table listed several argume nt introductions. Argument Type(default->setted) Introduction -------------------------------------------------------------------------------- -i int(0) This is a demo int parameter -b.dump bool(false) Svar supports tree item assign -obj svar(undefined) Svar support json parameter -conf str("Default.cfg") The default configure file going to parse. -help bool(false->true) Show the help information. ``` "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: - "-arg value": two '-' such as "--arg value" is the same - "-arg=value": two "--" such as "--arg=value" is the same - "arg=value" - "-arg" : this is the same with "arg=true", but the next argument should not be a value Svar supports to use brief Json instead of strict Json: ``` sample_use -b.dump -obj '{a:1,b:false}' ``` ## Svar Holding Everything ### Hold Class Instances Svar support to hold different values: ``` struct A{int a,b,c;}; // define a struct A a={1,2,3}; Svar avar=a; // support directly value copy assign Svar aptrvar=&a; // support pointer value assign Svar uptrvar=std::unique_ptr(new A({2,3,4})); Svar sptrvar=std::shared_ptr(new A({2,3,4})); // support shared_ptr lvalue std::cout<().a<().a<().a<().a<()->b<()->b<()->b<()->b<>()->b<>()->b<()<("Person","The base class") .construct() .def("intro",&Person::intro) .def_static("all",&Person::all_person) .def("age",&Person::age) .def_readonly("name",&Person::_name,"The name of a person"); Class("Student","The derived class") .construct() .inherit() .def("intro",&Student::intro) .def("setSchool",&Student::setSchool) .def("getSchool",[](Student& self){return self._school;}) .def_readwrite("school",&Student::_school,"The school of a student"); // use the class with svar Svar Person=svar["Person"]; Svar Student=svar["Student"]; std::cout<(); std::cout<(); Svar father=Person(40,"father"); Svar mother=Person(39,"mother"); Svar sister=Student(15,"sister","high"); Svar me =Student(13,"me","juniar"); me.call("setSchool","school1"); std::cout<<"all:"<()<()<("school","school2"); std::cout<("school","")<("Person","The base class") .construct() .def("intro",&Person::intro) .def_static("all",&Person::all_person) .def("age",&Person::age) .def_readonly("name",&Person::_name,"The name of a person"); Class("Student","The derived class") .construct() .inherit() .def("intro",&Student::intro) .def("setSchool",&Student::setSchool) .def("getSchool",[](Student& self){return self._school;}) .def_readwrite("school",&Student::_school,"The school of a student"); } EXPORT_SVAR_INSTANCE // export the symbol of Svar::instance ``` We compile the above code to a shared library "libsample_module.so" or "sample_module.dll". ### The Svar Module Documentation Show the context of the sample plugin module: ``` # Show the structure of a plugin svar doc -plugin libsample_module.so # Show the documentation of a named class svar doc -plugin libsample_module.so -key Person # Show the documentation of a named function svar doc -plugin libsample_module.so -key add ``` ### Import and Use a Svar Module For the Svar module, we can import and use them in different languages! Here we show how to use it in c++, source code is available in file "src/sample/main.cpp". ``` Svar sampleModule=Registry::load("sample_module"); Svar Person=sampleModule["Person"]; Svar Student=sampleModule["Student"]; std::cout<(); std::cout<(); Svar father=Person(40,"father"); Svar mother=Person(39,"mother"); Svar sister=Student(15,"sister","high"); Svar me =Student(13,"me","juniar"); me.call("setSchool","school1"); std::cout<<"all:"<()<()<("school","school2"); std::cout<("school","")< | | [![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=48AGPL9FJX2D6) | # License ``` Copyright (c) 2018 Northwestern Polytechnical University, Yong Zhao. All rights reserved. This software was developed by the Yong Zhao at Northwestern Polytechnical University. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed by Northwestern Polytechnical University and its contributors. 4. Neither the name of the University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ```