jvar
JS inspired Variant and Json
jvar::Variant Class Reference

Detailed Description

Variant class is inspired by Javascript's Var semantics. It maintains data of different kinds such as ints, doubles, strings. It also stores objects with properties, arrays of variants, and function objects.

Examples:
arrays.cpp, basics.cpp, func.cpp, jsonparse.cpp, objs.cpp, and printv.cpp.

#include <var.h>

+ Collaboration diagram for jvar::Variant:

Classes

struct  VarData

Public Types

enum  Type {
  V_EMPTY, V_NULL, V_INT, V_BOOL,
  V_DOUBLE, V_STRING, V_ARRAY, V_OBJECT,
  V_FUNCTION, V_POINTER
}
typedef int(* Compare )(const Variant *, const Variant *)

Public Member Functions

 Variant ()
 Variant (longint i)
 Variant (int i)
 Variant (double d)
 Variant (std::string s)
 Variant (const char *s)
 Variant (Variant const &src)
 ~Variant ()
Variantoperator= (const Variant &src)
Variantoperator= (const Variant *src)
Variantoperator= (const char *src)
Variantoperator= (const std::string &src)
Variantoperator= (bool src)
Variantoperator= (longint src)
Variantoperator= (int src)
Variantoperator= (double src)
Variantoperator= (float src)
Variantoperator+= (const Variant &rhs)
Variantoperator+= (longint rhs)
Variantoperator+= (int rhs)
Variantoperator+= (const char *rhs)
Variantoperator+= (const std::string &rhs)
Variantoperator+= (double rhs)
Variantoperator++ ()
Variant operator++ (int)
longint toInt () const
double toDouble () const
bool toBool () const
 operator longint () const
 operator double () const
 operator bool () const
 operator const char * ()
const char * c_str () const
std::string & s ()
std::string toString () const
std::string toFixed (int digs=0) const
std::string toJsonString () const
bool parseJson (const char *jsontxt)
void format (const char *fmt,...)
Type type () const
const char * typeName () const
bool isNull () const
bool isEmpty () const
bool isObject () const
bool isArray () const
bool isPointer () const
bool isNaN ()
int length () const
Variantoperator[] (int i)
const Variantoperator[] (int i) const
Variantoperator[] (const char *key)
const Variantoperator[] (const char *key) const
Variantpath (const char *pathkey)
Variantpath (const std::string &pathkey)
void createArray (const char *initvalue=NULL)
Variantappend (const Variant &elem=VEMPTY)
void push (const Variant &elem)
Variant pop ()
Variant shift ()
void sort (Compare comp)
void createObject (const char *initvalue=NULL)
VariantaddProperty (const char *key, const Variant &value=vEmpty)
bool hasProperty (const char *key)
const char * getKey (int n)
void createFunction (Variant(*func)(Variant &env, Variant &arg))
bool addEnv (const char *varname, const Variant &value=vEmpty)
Variant operator() ()
Variant operator() (const Variant &value1)
Variant operator() (const Variant &value1, const Variant &value2)
Variant operator() (const Variant &value1, const Variant &value2, const Variant &value3)
Variant operator() (const Variant &value1, const Variant &value2, const Variant &value3, const Variant &value4)
bool forEach (Iter< Variant > &iter)
void clear ()
void setModified ()
void clearModified ()
bool isModified ()

Static Public Attributes

static Variant vEmpty
static Variant vNull

Constructor & Destructor Documentation

jvar::Variant::Variant ( ) [inline]

Default constructor-- an Empty object

jvar::Variant::Variant ( longint  i) [inline]

Constructs an Integer object with a longint

jvar::Variant::Variant ( int  i) [inline]

Constructs an Integer object with an int

jvar::Variant::Variant ( double  d) [inline]

Constructs an double object

jvar::Variant::Variant ( std::string  s) [inline]

Constructs an string object using a std::string

jvar::Variant::Variant ( const char *  s) [inline]

Constructs an string object

jvar::Variant::Variant ( Variant const &  src) [inline]

Copy constructor

Destructor deletes all data in the object


Member Function Documentation

bool jvar::Variant::addEnv ( const char *  varname,
const Variant value = vEmpty 
)

Adds a variable to the enviroment of the function object

Parameters:
varnameKey name for the variable
valueOptional value for the variable
Returns:
Success
Examples:
func.cpp.
Variant & jvar::Variant::addProperty ( const char *  key,
const Variant value = vEmpty 
)

Adds a new property to the object

Parameters:
keyKey name for the property
valueOptional value
Returns:
Reference to the value in object
Examples:
objs.cpp.

References setModified(), and type().

Variant * jvar::Variant::append ( const Variant elem = VEMPTY)

Adds a variant at the end of an array

Parameters:
elemVariant to add
Returns:
Pointer to the newly added variant

References setModified(), and type().

Referenced by push().

const char* jvar::Variant::c_str ( ) const [inline]

Returns a pointer to the string. Returns NULL if not string type.

Referenced by operator const char *().

void jvar::Variant::clear ( ) [inline]

Clears the object by deleting all data

void jvar::Variant::clearModified ( ) [inline]

Clears the modified flag

void jvar::Variant::createArray ( const char *  initvalue = NULL)

Creates an array

Parameters:
initvalueOptional JSON to initialize the array
Examples:
arrays.cpp, and objs.cpp.

References jvar::JsonParser::FLAG_ARRAYONLY, and jvar::JsonParser::FLAG_FLEXQUOTES.

void jvar::Variant::createFunction ( Variant(*)(Variant &env, Variant &arg)  func)

Creates a function object]

Parameters:
funcFunction pointer
Examples:
func.cpp.
void jvar::Variant::createObject ( const char *  initvalue = NULL)

Creates an objects with optional initial value

Parameters:
initvalueJson style initial value
Examples:
objs.cpp, and printv.cpp.

References jvar::JsonParser::FLAG_FLEXQUOTES, and jvar::JsonParser::FLAG_OBJECTONLY.

bool jvar::Variant::forEach ( Iter< Variant > &  iter) [inline]

Returns an iterator to go over all elements in an array or object

Parameters:
iterIterator
Returns:
Success
Examples:
arrays.cpp, and objs.cpp.
void jvar::Variant::format ( const char *  fmt,
  ... 
)

Formats a new string using printf style formatting and assigns it to the variant

Examples:
basics.cpp.

Referenced by toFixed().

const char * jvar::Variant::getKey ( int  n)

Returns the key name at a given index

Parameters:
nIndex position
Returns:
A pointer to property key name
bool jvar::Variant::hasProperty ( const char *  key)

Determines if the object has a property

Parameters:
keyKey name for the property
Returns:
True if found, false otherwise
bool jvar::Variant::isModified ( ) [inline]

Checks modified flag

int jvar::Variant::length ( ) const

Returns the length. If an array, returns array length. If an object, returns the number of properties. Otherwise, returns 1.

Examples:
arrays.cpp.
jvar::Variant::operator bool ( ) const [inline]

Typecast the value as an bool

jvar::Variant::operator const char * ( ) [inline]

Returns a pointer to the string. Returns NULL if not string type.

References c_str().

jvar::Variant::operator double ( ) const [inline]

Typecast the value as an double

jvar::Variant::operator longint ( ) const [inline]

Typecast the value as an int

Variant& jvar::Variant::operator++ ( ) [inline]

Prefix (++v) Adding Variants

Variant jvar::Variant::operator++ ( int  ) [inline]

Postfix (v++) Adding Variants

Variant& jvar::Variant::operator+= ( const Variant rhs) [inline]
Variant& jvar::Variant::operator+= ( longint  rhs) [inline]
Variant& jvar::Variant::operator+= ( int  rhs) [inline]
Variant& jvar::Variant::operator+= ( const char *  rhs) [inline]
Variant& jvar::Variant::operator+= ( const std::string &  rhs) [inline]
Variant& jvar::Variant::operator+= ( double  rhs) [inline]
Variant& jvar::Variant::operator= ( const Variant src) [inline]

Assignms a variant by copying it

Variant& jvar::Variant::operator= ( const Variant src) [inline]

Assignms a variant by copying it

Variant& jvar::Variant::operator= ( const char *  src) [inline]

Assignms a string (changes type if needed)

Variant& jvar::Variant::operator= ( const std::string &  src) [inline]

Assignms a string (changes type if needed)

Variant& jvar::Variant::operator= ( bool  src) [inline]

Assignms a bool (changes type if needed)

Variant& jvar::Variant::operator= ( longint  src) [inline]

Assignms a longint (changes type if needed)

Variant& jvar::Variant::operator= ( int  src) [inline]

Assignms a int (changes type if needed)

Variant& jvar::Variant::operator= ( double  src) [inline]

Assignms a double (changes type if needed)

Variant& jvar::Variant::operator= ( float  src) [inline]

Assignms a float (changes type if needed)

Variant & jvar::Variant::operator[] ( int  i)

Returns a referene to the variant in an array

const Variant & jvar::Variant::operator[] ( int  i) const

Returns a const referene to the variant in an array

Variant & jvar::Variant::operator[] ( const char *  key)

Returns a referene to the variant inside an object or a fuction using key

const Variant & jvar::Variant::operator[] ( const char *  key) const

Returns a const referene to the variant inside an object or a fuction using key

bool jvar::Variant::parseJson ( const char *  jsontxt)

Parses json text and loads the data structure into the variant

Parameters:
jsontxtJson text string
Returns:
Success
Examples:
jsonparse.cpp.

References jvar::Parser::failed(), and setModified().

Variant & jvar::Variant::path ( const char *  pathkey)

Returns a refernece to a variant by parsing properties and indexes using a path syntax (ex: "obj.propA.2.name")

Parameters:
pathkey[description]
Returns:
[description]

References jvar::Parser::advance(), jvar::Parser::eof(), jvar::Parser::token(), and jvar::Parser::tokenEquals().

Referenced by path().

Variant& jvar::Variant::path ( const std::string &  pathkey) [inline]

Returns a refernece to a variant by parsing properties and indexes using a path syntax (ex: "obj.propA.2.name")

Parameters:
pathkey[description]
Returns:
[description]

References path().

Removes the last item from the array and returns it

Returns:
Copy of the last element
Examples:
arrays.cpp.
void jvar::Variant::push ( const Variant elem) [inline]

Adds an variant at the end of an array

Examples:
arrays.cpp.

References append().

std::string & jvar::Variant::s ( )

Returns a *reference* to the string object. If not string type, the type is automatically *changed* to string by this function.

NOTE 1: Modifies the type of the object to string NOTE 2: Should not be called with type==V_NULL, or VNULL or VEMPTY.

Returns:
Reference to the string object.

References setModified().

Referenced by toFixed(), toJsonString(), and toString().

void jvar::Variant::setModified ( ) [inline]

Sets the modified flag

Referenced by addProperty(), append(), parseJson(), and s().

Removes the first item from the array and returns it

Returns:
Copy of first item
void jvar::Variant::sort ( Compare  comp)

Sorts an array

Parameters:
compOptional compare function
bool jvar::Variant::toBool ( ) const [inline]

Return the value as an bool

double jvar::Variant::toDouble ( ) const [inline]

Return the value as an double

std::string jvar::Variant::toFixed ( int  digs = 0) const

Returns as a formatted string for a double, otherwise returns a copy of string performing any conversions

Parameters:
digsNumber of digits to show in double
Returns:
Copy of string
Examples:
func.cpp.

References format(), s(), and toString().

longint jvar::Variant::toInt ( ) const [inline]

Return the value as an int

std::string jvar::Variant::toJsonString ( ) const

Returns a json text representing the object.

Returns:
String with Json
Examples:
jsonparse.cpp.

References s().

std::string jvar::Variant::toString ( ) const

Returns a *copy* of the string performing any conversions. Format is similar to json but does not do any escaping (for proper Json strings, use toJsonString()

Returns:
Copy of the string
Examples:
arrays.cpp, basics.cpp, func.cpp, jsonparse.cpp, objs.cpp, and printv.cpp.

References s().

Referenced by toFixed().

Type jvar::Variant::type ( ) const [inline]

Returns the variant type V_xxx for the object

Returns:
Variant type

Referenced by addProperty(), and append().

const char * jvar::Variant::typeName ( ) const

Returns the variant type of the object as a string

Returns:
String containing type name

References jvar::KeywordArray::toKeyword().


The documentation for this class was generated from the following files:
 All Classes Functions Variables Enumerator