//  // //  #### # # # -= SQLiteX =-  // // # # ## ## Query.cpp  // //  ## # ###  // //  ## # ### SQL transaction  // //  # # ## ##  // // #### ##### # # R1 (C)2005 Markus Ewald -> License.txt  // //  // #include "SQLiteX/Variant.h" #include "SQLiteX/Errors.h" #include using namespace SQLiteX; namespace { // ############################################################################################# // // # lexical_cast() # // // ############################################################################################# // /// Performs a lexical cast to another data type /** A lexical cast forms a string from the provided source data and tries to interpret it as the target data type @param from Value to be converted @return The converted value */ template inline TargetType lexical_cast(SourceType from) { std::stringstream s; s << from; TargetType to; s >> to; return to; } } // anonymous namespace // ############################################################################################# // // # SQLiteX::Variant::Variant() # // // ############################################################################################# // /** Creates a new instance of an empty Variant */ Variant::Variant() : m_eType(T_NULL) {} // ############################################################################################# // // # SQLiteX::Variant::Variant() # // // ############################################################################################# // /** Creates a new instance as copy of an existing instance. @param Value Instance to copy */ Variant::Variant(const Variant &Value) : m_eType(T_NULL) { operator =(Value); } // ############################################################################################# // // # SQLiteX::Variant::Variant() # // // ############################################################################################# // /** Creates a new instance of Variant initialized with an integer @param nValue Initial integer value */ Variant::Variant(int nValue) : m_eType(T_INT), m_nValue(nValue) {} // ############################################################################################# // // # SQLiteX::Variant::Variant() # // // ############################################################################################# // /** Creates a new instance of Variant initialized with a double @param dValue Initial double value */ Variant::Variant(double dValue) : m_eType(T_FLOAT), m_dValue(dValue) {} // ############################################################################################# // // # SQLiteX::Variant::Variant() # // // ############################################################################################# // /** Creates a new instance of Variant initialized with a c string @param pszString Initial string value */ Variant::Variant(const char *pszString) : m_eType(T_TEXT), m_psString(new std::string(pszString)) {} // ############################################################################################# // // # SQLiteX::Variant::Variant() # // // ############################################################################################# // /** Creates a new instance of Variant initialized with a string @param sString Initial string value */ Variant::Variant(const std::string &sString) : m_eType(T_TEXT), m_psString(new std::string(sString)) {} // ############################################################################################# // // # SQLiteX::Variant::Variant() # // // ############################################################################################# // /** Creates a new instance of Variant initialized with a blob @param Data Initial blob contents */ Variant::Variant(const std::vector &Data) : m_eType(T_BLOB), m_pData(new std::vector(Data)) {} // ############################################################################################# // // # SQLiteX::Variant::Variant() # // // ############################################################################################# // /** Creates a new instance of Variant initialized with a blob @param Data Initial blob contents */ Variant::Variant(std::vector &Data, bool bDestructive) : m_eType(T_BLOB), m_pData(new std::vector()) { if(bDestructive) m_pData->swap(Data); else (*m_pData) = Data; } // ############################################################################################# // // # SQLiteX::Variant::~Variant() # // // ############################################################################################# // /** Destroys an instance of Variant */ Variant::~Variant() { reset(); } // ############################################################################################# // // # SQLiteX::Variant::operator =() # // // ############################################################################################# // /** Assigns a value to the Variant @param Value Value to assign @return The assigned value */ Variant &Variant::operator =(const Variant &Value) { reset(); m_eType = Value.m_eType; switch(m_eType) { case T_NULL: { break; } case T_INT: { m_nValue = Value.m_nValue; break; } case T_FLOAT: { m_dValue = Value.m_dValue; break; } case T_TEXT: { m_psString = new std::string(*Value.m_psString); break; } case T_BLOB: { m_pData = new std::vector(Value); break; } } return *this; } // ############################################################################################# // // # SQLiteX::Variant::operator =() # // // ############################################################################################# // /** Assigns an integer value to the Variant @param nValue Integer value @return The assigned value */ Variant &Variant::operator =(int nValue) { reset(); m_eType = T_INT; m_nValue = nValue; return *this; } // ############################################################################################# // // # SQLiteX::Variant::operator =() # // // ############################################################################################# // /** Assigns a double value to the Variant @param dValue Double value @return The assigned value */ Variant &Variant::operator =(double dValue) { reset(); m_eType = T_FLOAT; m_dValue = dValue; return *this; } // ############################################################################################# // // # SQLiteX::Variant::operator =() # // // ############################################################################################# // /** Assigns a string value to the Variant @param sString String value @return The assigned value */ Variant &Variant::operator =(const std::string &sString) { reset(); m_eType = T_TEXT; m_psString = new std::string(sString); return *this; } // ############################################################################################# // // # SQLiteX::Variant::operator =() # // // ############################################################################################# // /** Assigns a string value to the Variant @param sString String value @return The assigned value */ Variant &Variant::operator =(const std::vector &Data) { reset(); m_eType = T_BLOB; m_pData = new std::vector(Data); return *this; } // ############################################################################################# // // # SQLiteX::Variant::operator int() # // // ############################################################################################# // /** Returns the value of the object as integer @return Integer value of the object */ Variant::operator int() const { switch(m_eType) { case T_NULL: { return 0; } case T_INT: { return m_nValue; } case T_FLOAT: { return static_cast(m_dValue); } case T_TEXT: { return lexical_cast(*m_psString); } case T_BLOB: { int nValue; std::memcpy(&nValue, &m_pData[0], std::min(sizeof(nValue), m_pData->size())); return nValue; } default: throw std::logic_error("Variant has invalid type"); } } // ############################################################################################# // // # SQLiteX::Variant::operator double() # // // ############################################################################################# // /** Returns the value of the object as double @return Double value of the object */ Variant::operator double() const { switch(m_eType) { case T_NULL: { return 0.0; } case T_INT: { return static_cast(m_nValue); } case T_FLOAT: { return m_dValue; } case T_TEXT: { return lexical_cast(*m_psString); } case T_BLOB: { double dValue; std::memcpy(&dValue, &m_pData[0], std::min(sizeof(dValue), m_pData->size())); return dValue; } default: throw std::logic_error("Variant has invalid type"); } } // ############################################################################################# // // # SQLiteX::Variant::operator std::string() # // // ############################################################################################# // /** Returns the value of the object as string @return String value of the object */ Variant::operator std::string() const { switch(m_eType) { case T_NULL: { return ""; } case T_INT: { return lexical_cast(m_nValue); } case T_FLOAT: { return lexical_cast(m_dValue); } case T_TEXT: { return *m_psString; } case T_BLOB: { return std::string(m_pData->begin(), m_pData->end()); } default: throw std::logic_error("Variant has invalid type"); } } // ############################################################################################# // // # SQLiteX::Variant::operator std::vector() # // // ############################################################################################# // /** Returns the value of the object as string @return String value of the object */ Variant::operator std::vector() const { switch(m_eType) { case T_NULL: { return std::vector(); } case T_INT: { std::vector Blob(sizeof(m_nValue)); std::memcpy(&Blob[0], &m_nValue, sizeof(m_nValue)); return Blob; } case T_FLOAT: { std::vector Blob(sizeof(m_dValue)); std::memcpy(&Blob[0], &m_dValue, sizeof(m_dValue)); return Blob; } case T_TEXT: { return std::vector (m_psString->begin(), m_psString->end()); } case T_BLOB: { return *m_pData; } default: throw std::logic_error("Variant has invalid type"); } } // ############################################################################################# // // # SQLiteX::Variant::reset() # // // ############################################################################################# // /** Clears the contents of the Variant instance. If the Value contains an Objects or a Blob, it will be Release()d. After the method returns, the Value is in an empty state again. */ void Variant::reset() { switch(m_eType) { case T_TEXT: { delete m_psString; break; } case T_BLOB: { delete m_pData; break; } } m_eType = T_NULL; }