Git Product home page Git Product logo

dynamicvar's Introduction

About DynamicVar

DynamicVar is a dynamic type for C++ that holds data in an internal std::string object.

I wrote this to use in some personal projects to facilitate conversions between types in UI, Databases and other services.

This type allows you to set its value as int, double, string, boolean and some others and get its value as well in various types.

The type contains some useful constructors and type overloading helping to use it with your application.

Note: Internally, the DynamicVar holds the valus as a std::string.

Input types

Allowed input types for values are 'int', 'int64_t', 'double', 'string', 'bool', 'char const *' and JsonMaker::JSON (see more at http...)

You can set the value by a simple assignment operation ('='), by the costructor or by using the 'set*' methods.

The set* methods are:

void setInt(int value);
void setInt64(int64_t value);
void setDouble(double value);
void setString(string value);
void setCStr(char* s);
void setBool(bool value);

If you don't want to use the 'JsonMaker::JSON' type, currently you must remove the #include<JSON.h> from the DynamicVar.h header.

Output types

You can get the value by a simple assignment operation ('=') or by the 'get*' methods.

The get* methods are:

int getInt(function<void()> onError = [](){});
int64_t getInt64(function<void()> onError = [](){});
double getDouble(function<void()> onError = [](){});
string getString();
bool getBool(function<void()> onError = [](){});
void setBool(bool value);

Excluding the 'getString' function, all other calls to get functions can result in internal conversion errors. For these cases, you can specify an anonymous function to be notified. Also, if an internal error occurs, the function will return a default value (0 for numbers, false for boolean and an empty JSON object for json).

Usage examples

a simple var

DynamicVar tmp;
tmp.setInt(10);
cout << "The value of temp is " << tmp.getInt() << endl;

setting as int ang getting as string

DynamicVar tmp;
tmp.setInt(10);
cout << "The value of temp is " << tmp.getString() << endl;

Setting value on constructor

DynamicVar tmp(10);
cout << "The value of temp is " << tmp.getString() << endl;

Using a DynamicVar var in a class constructor or class methods

class MyClass{
private:
    DynamicVar value;
public:
    MyClass(DynamicVar initialValue): value(initialValue){}
    DynamicVar getValue(){ return value }
};


MyClass tmp1(10);
MyClass tmp1("11");
MyClass tmp1(12.0);
cout << "The value of tmp1 is " << tmp1.getValue().getString() << endl;
cout << "The value of tmp2 is " << tmp2.getValue().getString() << endl;
cout << "The value of tmp3 is " << tmp3.getValue().getString() << endl;

Simple attribuitions

DynamicVar tmp1 = 10;
DynamicVar tmp2 = "11";
DynamicVar tmp3 = 12.0;

cout << "The value of tmp1 is " << tmp1.getString() << endl;
cout << "The value of tmp2 is " << tmp2.getString() << endl;
cout << "The value of tmp3 is " << tmp3.getString() << endl;

dynamicvar's People

Contributors

rafael-tonello avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.