X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=src%2Fjson%2Fjhash.cpp;h=25efc4583abd9e5488ba5a34a86feac7677d3fa2;hb=555fc14abec797f96cd4c03bf997ce3711984393;hp=7f4a446d5033e14a934d0e68de2b87379f32d016;hpb=5a9d7c67d1ec1e4410995eae4b50f929396935f6;p=ric-plt%2Fxapp-frame-cpp.git diff --git a/src/json/jhash.cpp b/src/json/jhash.cpp index 7f4a446..25efc45 100644 --- a/src/json/jhash.cpp +++ b/src/json/jhash.cpp @@ -34,6 +34,10 @@ #include "jwrapper.h" #include "jhash.hpp" + +namespace xapp { + + // ------------------------------------------------------------------------ @@ -44,18 +48,41 @@ after which the functions provided by the class can be used to suss out the values. */ -Jhash::Jhash( const char* jbuf ) : - master_st( NULL ), +xapp::Jhash::Jhash( const char* jbuf ) : st( jw_new( jbuf ) ) { /* empty body */ } -//Jhash::Jhash( Jhash&& soi ); // mover -//Jhash::Jhash& operator=( Jhash&& soi ); // move operator + +/* + Move constructor. +*/ +Jhash::Jhash( Jhash&& soi ) : + master_st( soi.master_st ), + st( soi.st ) +{ + soi.st = NULL; // prevent closing of RMR stuff on soi destroy + soi.master_st = NULL; +} + +/* + Move Operator. +*/ +Jhash& Jhash::operator=( Jhash&& soi ) { + if( this != &soi ) { // cannot do self assignment + master_st = soi.master_st; + st = soi.st; + + soi.st = NULL; // prevent closing of RMR stuff on soi destroy + soi.master_st = NULL; + } + + return *this; +} /* Blow it away. */ -Jhash::~Jhash() { +xapp::Jhash::~Jhash() { if( master_st != NULL ) { // revert blob set if needed st = master_st; } @@ -82,11 +109,11 @@ Jhash::~Jhash() { overlays with the named root; unset needs only to be called to return to the top level. */ -bool Jhash::Set_blob( const char* name ) { +bool xapp::Jhash::Set_blob( const char* name ) { void* bst; // blob symbol table if( master_st == NULL ) { // must capture master - master_st == st; + master_st = st; } if( (bst = jw_blob( st, name )) != NULL ) { @@ -100,15 +127,10 @@ bool Jhash::Set_blob( const char* name ) { /* Return the suss root (blob root) to the root of the symtab. */ -void Jhash::Unset_blob( ) { +void xapp::Jhash::Unset_blob( ) { if( master_st != NULL ) { st = master_st; } -/* - else { -fprintf( stderr, "bad unset pointer%p %p\n", master_st, st ); -} -*/ } // ---------------- debugging and sanity checks --------------------------------------- @@ -119,14 +141,14 @@ fprintf( stderr, "bad unset pointer%p %p\n", master_st, st ); Right now we don't have much to work with other than checking for a nil table. */ -bool Jhash::Parse_errors( ) { +const bool xapp::Jhash::Parse_errors( ) { return st == NULL; } /* Dump the selected blob as much as we can. */ -void Jhash::Dump() { +void xapp::Jhash::Dump() { jw_dump( st ); } @@ -136,19 +158,19 @@ void Jhash::Dump() { These funcitons return true if the named object in the current blob is the indicated type */ -bool Jhash::Is_value( const char* name ) { +bool xapp::Jhash::Is_value( const char* name ) { return jw_is_value( st, name ) == 1; } -bool Jhash::Is_bool( const char* name ) { +bool xapp::Jhash::Is_bool( const char* name ) { return jw_is_bool( st, name ) == 1; } -bool Jhash::Is_null( const char* name ) { +bool xapp::Jhash::Is_null( const char* name ) { return jw_is_null( st, name ) == 1; } -bool Jhash::Is_string( const char* name ) { +bool xapp::Jhash::Is_string( const char* name ) { return jw_is_string( st, name ) == 1; } @@ -156,19 +178,19 @@ bool Jhash::Is_string( const char* name ) { These functions return true if the indicated element in the array is the indicated type. */ -bool Jhash::Is_string_ele( const char* name, int eidx ) { +bool xapp::Jhash::Is_string_ele( const char* name, int eidx ) { return jw_is_string_ele( st, name, eidx ) == 1; } -bool Jhash::Is_value_ele( const char* name, int eidx ) { +bool xapp::Jhash::Is_value_ele( const char* name, int eidx ) { return jw_is_value_ele( st, name, eidx ) == 1; } -bool Jhash::Is_bool_ele( const char* name, int eidx ) { +bool xapp::Jhash::Is_bool_ele( const char* name, int eidx ) { return jw_is_bool_ele( st, name, eidx ) == 1; } -bool Jhash::Is_null_ele( const char* name, int eidx ) { +bool xapp::Jhash::Is_null_ele( const char* name, int eidx ) { return jw_is_null_ele( st, name, eidx ) == 1; } @@ -177,14 +199,14 @@ bool Jhash::Is_null_ele( const char* name, int eidx ) { /* Returns true if the named element is in the hash. */ -bool Jhash::Exists( const char* name ) { +bool xapp::Jhash::Exists( const char* name ) { return jw_exists( st, name ) == 1; } /* Returns true if the named element is not in the hash. */ -bool Jhash::Is_missing( const char* name ) { +bool xapp::Jhash::Is_missing( const char* name ) { return jw_missing( st, name ) == 1; } @@ -196,9 +218,10 @@ bool Jhash::Is_missing( const char* name ) { Symtab saves bool values as 1 for true and doesn't provide a bool fetch function. So, fetch the value and return true if it is 1. */ -bool Jhash::Bool( const char* name ) { +bool xapp::Jhash::Bool( const char* name ) { int v; v = (int) jw_value( st, name ); + return v == 1; } @@ -206,9 +229,9 @@ bool Jhash::Bool( const char* name ) { Returns a C++ string to the named object; If the element is not in the hash an empty string is returned. */ -std::string Jhash::String( const char* name ) { +std::string xapp::Jhash::String( const char* name ) { std::string rv = ""; - char* hashv; + const char* hashv; if( (hashv = jw_string( st, name )) != NULL ) { rv = std::string( hashv ); @@ -222,7 +245,7 @@ std::string Jhash::String( const char* name ) { Returns the value assocated with the named object; If the element is not in the hash 0 is returned. */ -double Jhash::Value( const char* name ) { +double xapp::Jhash::Value( const char* name ) { return jw_value( st, name ); } @@ -231,7 +254,7 @@ double Jhash::Value( const char* name ) { /* Return the length of the named array, or -1 if it doesn't exist. */ -int Jhash::Array_len( const char* name ) { +int xapp::Jhash::Array_len( const char* name ) { return jw_array_len( st, name ); } @@ -239,7 +262,7 @@ int Jhash::Array_len( const char* name ) { /* Sets the blob in the array [eidx] to the current reference blob. */ -bool Jhash::Set_blob_ele( const char* name, int eidx ) { +bool xapp::Jhash::Set_blob_ele( const char* name, int eidx ) { void* bst; if( (bst = jw_obj_ele( st, name, eidx )) != NULL ) { @@ -256,9 +279,9 @@ bool Jhash::Set_blob_ele( const char* name, int eidx ) { /* Return the string at index eidx in the array . */ -std::string Jhash::String_ele( const char* name, int eidx ) { +std::string xapp::Jhash::String_ele( const char* name, int eidx ) { std::string rv = ""; - char* hashv; + const char* hashv; if( (hashv = jw_string_ele( st, name, eidx )) != NULL ) { rv = std::string( hashv ); @@ -270,14 +293,17 @@ std::string Jhash::String_ele( const char* name, int eidx ) { /* Return the value at index eidx in the array . */ -double Jhash::Value_ele( const char* name, int eidx ) { +double xapp::Jhash::Value_ele( const char* name, int eidx ) { return jw_value_ele( st, name, eidx ); } /* Return the bool value at index eidx in the array . */ -bool Jhash::Bool_ele( const char* name, int eidx ) { +bool xapp::Jhash::Bool_ele( const char* name, int eidx ) { return jw_bool_ele( st, name, eidx ) == 1; } + + +} // namespace