Advertisement
Guest User

Untitled

a guest
Feb 14th, 2011
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include "Defines.h"
  2. #include <list>
  3. #include <string>
  4.  
  5. class Value
  6. {
  7. public:
  8.     Value() {}
  9.     Value(int32 i)  { m_data.push_back(new Data(i));}
  10.     Value(uint32 i) { m_data.push_back(new Data(i)); }
  11.     Value(char c)   { m_data.push_back(new Data(c)); }
  12.     Value(const std::string& text) { m_data.push_back(new Data(text)); }
  13.     Value(const char* text) { m_data.push_back(new Data(std::string(text))); }
  14.     ~Value();
  15.  
  16.     enum DataType {
  17.         TYPE_INT,
  18.         TYPE_CHAR,
  19.         TYPE_UINT,
  20.         TYPE_STRING,
  21.         TYPE_INVALID
  22.     };
  23.  
  24.  
  25.     inline int16 GetType() const { return m_data.size() ? m_data.front()->m_type : -1; }
  26.     void Clear();
  27.     inline uint16 size() const { return (uint16)m_data.size(); }
  28.  
  29.     operator int32()const;
  30.     operator uint32() const;   
  31.     operator std::string() const;
  32.     operator char() const;
  33.  
  34.     bool operator==(const Value& value)
  35.     {
  36.         if(value.size() != this->size())
  37.             return false;
  38.         if(value.GetType() != this->GetType())
  39.             return false;
  40.         switch(this->GetType())
  41.         {
  42.         case TYPE_INT:      return int32(*this) == int32(value);
  43.         case TYPE_STRING:   return std::string(*this) == std::string(value);
  44.         case TYPE_UINT:     return uint32(*this) == uint32(value);
  45.         case TYPE_CHAR:     return char(*this) == char(value);
  46.         default: return false;
  47.         }
  48.     }
  49.     bool operator!=(const Value& value) { return !(*this == value); }
  50.     bool operator>(const Value& value)  
  51.     {
  52.         if(value.size() != this->size())
  53.             return false;
  54.         if(value.GetType() != this->GetType())
  55.             return false;
  56.         switch(this->GetType())
  57.         {
  58.         case TYPE_INT:      return int32(*this) > int32(value);
  59.         case TYPE_STRING:   return std::string(*this) > std::string(value);
  60.         case TYPE_UINT:     return uint32(*this) > uint32(value);
  61.         case TYPE_CHAR:     return char(*this) > char(value);
  62.         default: return false;
  63.         }
  64.     }
  65.     bool operator<(const Value& value) { return !(*this > value); }
  66.  
  67. private:
  68.     struct Data {
  69.         Data(int32 i)   { m_data = new int32(i);   m_type = TYPE_INT; }
  70.         Data(uint32 ui) { m_data = new uint32(ui); m_type = TYPE_UINT; }
  71.         Data(const std::string& text) { m_data = new std::string(text); m_type = TYPE_STRING; }
  72.         Data(char c) { m_data = new char(c); m_type = TYPE_CHAR; }
  73.         ~Data();
  74.  
  75.         void* m_data;
  76.         int m_type;
  77.  
  78.     private:
  79.         Data();
  80.     };
  81.  
  82.     std::list<Data*> m_data;
  83.     static std::string error;
  84.  
  85.     template<class T>
  86.     friend Value& operator<<(Value& v, T type);
  87.  
  88.     template<class T>
  89.     friend Value& operator>>(Value& v, T& type);
  90.  
  91.     friend std::ostream& operator<<(std::ostream&, const Value&);
  92.  
  93. };
  94.  
  95.  
  96. template<class T>
  97. Value& operator<<( Value& v, T type )
  98. {
  99.     v.m_data.push_back(new Value::Data(type));
  100.     return v;
  101. }
  102. template<class T>
  103. Value& operator>>( Value& v, T& type )
  104. {
  105.     if(!v.m_data.size()) return v;
  106.     Value::Data* temp = v.m_data.front();
  107.     type = v;               //converts to the type if possible
  108.     delete temp;            //free memory
  109.     v.m_data.pop_front();   //pop the last element
  110.     return v;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement