Skip to content

Commit

Permalink
Generic(2) clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jeog committed Apr 30, 2016
1 parent eab7fc5 commit 33965f8
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 125 deletions.
54 changes: 42 additions & 12 deletions prov/generic2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,35 @@ along with this program. If not, see http://www.gnu.org/licenses.

#include "generic2.hpp"


/* COPY CONSTRUCT */
TOSDB_Generic::TOSDB_Generic(const TOSDB_Generic& gen)
:
_type_val( gen._type_val )
{
if( gen.is_string() )
*(std::string**)this->_sub =
new std::string(**(std::string**)(gen._sub));
else
memcpy(this->_sub, gen._sub, SUB_SZ);
}


/* MOVE CONSTRUCT */
TOSDB_Generic::TOSDB_Generic(TOSDB_Generic&& gen)
:
_type_val( gen._type_val )
{
/* we are stealing the scalar value OR the string pointer */
memcpy(this->_sub, gen._sub, SUB_SZ);
/* keep gen's carcas from screwing us if it had a string ptr */
memset(gen._sub, 0, SUB_SZ);
/* and change its type to keep it's destructor from a bad free */
gen._type_val = TYVAL_VOID;
}


/* ASSIGNMENT */
TOSDB_Generic&
TOSDB_Generic::operator=(const TOSDB_Generic& gen)
{
Expand All @@ -27,25 +56,26 @@ TOSDB_Generic::operator=(const TOSDB_Generic& gen)
{
if( this->is_string() )
/* if we're also a string simply assign */
*((std::string*)this->_sub) = *((std::string*)gen._sub);
**(std::string**)(this->_sub) = **(std::string**)(gen._sub);
else
/* otherwise allocate new */
*(std::string**)this->_sub =
new std::string(*((std::string*)gen._sub));
new std::string(**(std::string**)(gen._sub));
}
else
{
if( this->is_string() )
delete *(std::string**)this->_sub;

memcpy(this->_sub, gen._sub, gen._type_bsz);
memcpy(this->_sub, gen._sub, SUB_SZ);
}

this->_type_val = gen._type_val;
this->_type_bsz = gen._type_bsz;
this->_type_val = gen._type_val;
return *this;
}


/* MOVE ASSIGNMENT */
TOSDB_Generic&
TOSDB_Generic::operator=(TOSDB_Generic&& gen)
{
Expand All @@ -55,16 +85,16 @@ TOSDB_Generic::operator=(TOSDB_Generic&& gen)
although, this way is a lot cleaner than 2 nested conditionals*/
if(this->_type_val == TYVAL_STRING)
delete *(std::string**)this->_sub;

this->_type_val = gen._type_val;

/* we are stealing the scalar value OR the string pointer */
memcpy(this->_sub, gen._sub, gen._type_bsz);
memcpy(this->_sub, gen._sub, SUB_SZ);
/* keep gen's carcas from screwing us if it had a string ptr */
memset(gen._sub, 0, 8);
memset(gen._sub, 0, SUB_SZ);
/* and change its type to keep it's destructor from doing the same */
gen._type_val = TYVAL_VOID;

this->_type_val = gen._type_val;
this->_type_bsz = gen._type_bsz;

return *this;
}

Expand All @@ -80,7 +110,7 @@ TOSDB_Generic::as_string() const
case(TYVAL_DOUBLE):
return std::to_string(*((double*)this->_sub));
case(TYVAL_STRING):
return *((std::string*)(this->_sub));
return **(std::string**)(this->_sub);
default:
return std::string();
};
Expand All @@ -94,7 +124,7 @@ TOSDB_Generic::size() const
case(TYVAL_LONG_LONG): return sizeof(long long);
case(TYVAL_FLOAT): return sizeof(float);
case(TYVAL_DOUBLE): return sizeof(double);
case(TYVAL_STRING): return ((std::string*)(this->_sub))->size();
case(TYVAL_STRING): return (*(std::string**)(this->_sub))->size();
default: return 0;
};
}
Expand Down
Loading

0 comments on commit 33965f8

Please sign in to comment.