Created
March 1, 2014 19:42
-
-
Save Newlifer/9295965 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <assert.h> | |
#include <vector> | |
#include <map> | |
#include <string> | |
#include <memory> | |
using namespace std; | |
template< typename T > | |
struct ValueNum { | |
T value; | |
T upperLimit; | |
T downLimit; | |
ValueNum() {} | |
ValueNum( const T& val, const T& upLim, const T& dwnLim ) | |
: value( val ), upperLimit( upLim ), downLimit( dwnLim ) | |
{} | |
ValueNum( const ValueNum< T >& obj ) | |
{ | |
value = obj.value; | |
upperLimit = obj.upperLimit; | |
downLimit = obj.downLimit; | |
} | |
ValueNum< T >& operator= ( const ValueNum< T >& val ) | |
{ | |
if( this != &val ) { | |
value = val.value; | |
upperLimit = val.upperLimit; | |
downLimit = val.downLimit; | |
} | |
return *this; | |
} | |
}; | |
enum class ValueType { Integer = 0, | |
Double = 1, | |
VectorInt = 2, | |
VectorDouble = 3, | |
VectorString = 4 }; | |
struct Value { | |
public: | |
Value( const std::vector< ValueNum< int > >& vec ) | |
{ | |
myType = ValueType::VectorInt; | |
value.data.vectorInt = new std::vector< ValueNum< int > >(); | |
value.data.vectorInt->resize( vec.size() ); | |
std::copy( vec.begin(), vec.end(), value.data.vectorInt->begin() ); | |
} | |
Value( const std::vector< ValueNum< double > >& vec ) | |
{ | |
myType = ValueType::VectorDouble; | |
value.data.vectorDouble = new std::vector< ValueNum< double > >(); | |
value.data.vectorDouble->resize( vec.size() ); | |
std::copy( vec.begin(), vec.end(), value.data.vectorDouble->begin() ); | |
} | |
Value( const std::vector< ValueNum< std::string > >& vec ) | |
{ | |
myType = ValueType::VectorString; | |
value.data.vectorString = new std::vector< ValueNum< std::string > >(); | |
value.data.vectorString->resize( vec.size() ); | |
std::copy( vec.begin(), vec.end(), value.data.vectorString->begin() ); | |
} | |
Value( const ValueNum< int >& arg ) | |
{ | |
myType = ValueType::Integer; | |
value.data.valueInt = new ValueNum< int >( arg ); | |
} | |
Value( const ValueNum< double >& arg ) | |
{ | |
myType = ValueType::Double; | |
value.data.valueDouble = new ValueNum< double >( arg ); | |
} | |
~Value() | |
{ | |
switch( myType ) { | |
case ValueType::VectorInt: { | |
delete value.data.vectorInt; | |
break; | |
} | |
case ValueType::VectorDouble: { | |
delete value.data.vectorDouble; | |
break; | |
} | |
case ValueType::VectorString: { | |
delete value.data.vectorString; | |
break; | |
} | |
case ValueType::Integer: { | |
delete value.data.valueInt; | |
break; | |
} | |
case ValueType::Double: { | |
delete value.data.valueDouble; | |
break; | |
} | |
default:break; | |
}; | |
} | |
ValueNum< double >& valueDouble() | |
{ | |
assert( myType == ValueType::Double ); | |
return *value.data.valueDouble; | |
} | |
ValueNum< int >& valueInt() | |
{ | |
assert( myType == ValueType::Integer ); | |
return *value.data.valueInt; | |
} | |
std::vector< ValueNum< int > >& vectorInt() const | |
{ | |
assert( myType == ValueType::VectorInt ); | |
return *value.data.vectorInt; | |
} | |
std::vector< ValueNum< double > >& vectorDouble() | |
{ | |
assert( myType == ValueType::VectorDouble ); | |
return *value.data.vectorDouble; | |
} | |
std::vector< ValueNum< std::string > >& vectorString() | |
{ | |
assert( myType == ValueType::VectorString ); | |
return *value.data.vectorString; | |
} | |
ValueType type() const | |
{ | |
return myType; | |
} | |
protected: | |
ValueType myType; | |
private: | |
struct Private { | |
public: | |
Private() {} | |
Private( const Private& obj ) | |
{ | |
// data = obj.data; | |
} | |
~Private() {} | |
union Data { | |
Data() {} | |
~Data() {} | |
ValueNum< double >* valueDouble; | |
ValueNum< int >* valueInt; | |
std::vector< ValueNum< int > >* vectorInt; | |
std::vector< ValueNum< double > >* vectorDouble; | |
std::vector< ValueNum< std::string > >* vectorString; | |
}; | |
Data data; | |
}; | |
// * Data fields | |
Private value; | |
}; | |
class IToolBase { | |
public: | |
protected: | |
std::map< ValueType, Value > fields; | |
}; | |
int main() { | |
std::vector< ValueNum< int > > test = { { 1, 2, 3 } }; | |
Value val( test ); | |
cout << val.vectorInt()[0].value; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment