Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

/home/tommi/tntdb/include/tntdb/bits/value.h

00001 /*
00002  * Copyright (C) 2005,2010 Tommi Maekitalo
00003  * 
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  * 
00009  * As a special exception, you may use this file as part of a free
00010  * software library without restriction. Specifically, if other files
00011  * instantiate templates or use macros or inline functions from this
00012  * file, or you compile this file and link it with other files to
00013  * produce an executable, this file does not by itself cause the
00014  * resulting executable to be covered by the GNU General Public
00015  * License. This exception does not however invalidate any other
00016  * reasons why the executable file might be covered by the GNU Library
00017  * General Public License.
00018  * 
00019  * This library is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022  * Lesser General Public License for more details.
00023  * 
00024  * You should have received a copy of the GNU Lesser General Public
00025  * License along with this library; if not, write to the Free Software
00026  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00027  */
00028 
00029 #ifndef TNTDB_BITS_VALUE_H
00030 #define TNTDB_BITS_VALUE_H
00031 
00032 #include <tntdb/iface/ivalue.h>
00033 #include <tntdb/date.h>
00034 #include <tntdb/time.h>
00035 #include <tntdb/datetime.h>
00036 #include <tntdb/decimal.h>
00037 #include <tntdb/blob.h>
00038 #include <tntdb/config.h>
00039 #include <cxxtools/smartptr.h>
00040 
00041 namespace tntdb
00042 {
00046   class Value
00047   {
00048       cxxtools::SmartPtr<IValue> value;
00049 
00050     public:
00051       explicit Value(IValue* value = 0)
00052         : value(value)
00053         { }
00054 
00056 
00062 
00063       bool isNull() const                 { return !value || value->isNull(); }
00065       bool getBool() const                { return value->getBool(); }
00067       short getShort() const              { return value->getShort(); }
00069       int getInt() const                  { return value->getInt(); }
00071       long getLong() const                { return value->getLong(); }
00073       unsigned short getUnsignedShort() const { return value->getUnsignedShort(); }
00075       unsigned getUnsigned() const        { return value->getUnsigned(); }
00077       unsigned long getUnsignedLong() const { return value->getUnsignedLong(); }
00079       int32_t getInt32() const            { return value->getInt32(); }
00081       uint32_t getUnsigned32() const      { return value->getUnsigned32(); }
00083       int64_t getInt64() const            { return value->getInt64(); }
00085       uint64_t getUnsigned64() const      { return value->getUnsigned64(); }
00087       Decimal getDecimal() const          { return value->getDecimal(); }
00089       float getFloat() const              { return value->getFloat(); }
00091       double getDouble() const            { return value->getDouble(); }
00093       char getChar() const                { return value->getChar(); }
00095       std::string getString() const
00096         { std::string ret; value->getString(ret); return ret; }
00100       void getString(std::string& ret) const
00101         { value->getString(ret); }
00105       Blob getBlob() const
00106         { Blob ret; value->getBlob(ret); return ret; }
00108       void getBlob(Blob& blob) const
00109         { value->getBlob(blob); }
00111       Date getDate() const                { return value->getDate(); }
00113       Time getTime() const                { return value->getTime(); }
00115       Datetime getDatetime() const        { return value->getDatetime(); }
00117 
00133       template <typename T>
00134       bool getValue(T& ret) const
00135         { return *this >> ret; }
00136 
00138       template <typename T>
00139       bool get(T& ret) const
00140         { return *this >> ret; }
00141 
00143       bool operator!() const              { return !value; }
00145       const IValue* getImpl() const       { return &*value; }
00146   };
00147 
00149 
00152   inline bool operator>> (const Value& value, bool& out)
00153   {
00154     if (value.isNull())
00155       return false;
00156 
00157     out = value.getBool();
00158     return true;
00159   }
00160 
00161   inline bool operator>> (const Value& value, short& out)
00162   {
00163     if (value.isNull())
00164       return false;
00165 
00166     out = value.getShort();
00167     return true;
00168   }
00169 
00170   inline bool operator>> (const Value& value, int& out)
00171   {
00172     if (value.isNull())
00173       return false;
00174 
00175     out = value.getInt();
00176     return true;
00177   }
00178 
00179   inline bool operator>> (const Value& value, long& out)
00180   {
00181     if (value.isNull())
00182       return false;
00183 
00184     out = value.getLong();
00185     return true;
00186   }
00187 
00188   inline bool operator>> (const Value& value, unsigned short& out)
00189   {
00190     if (value.isNull())
00191       return false;
00192 
00193     out = value.getUnsignedShort();
00194     return true;
00195   }
00196 
00197   inline bool operator>> (const Value& value, unsigned& out)
00198   {
00199     if (value.isNull())
00200       return false;
00201 
00202     out = value.getUnsigned();
00203     return true;
00204   }
00205 
00206   inline bool operator>> (const Value& value, unsigned long& out)
00207   {
00208     if (value.isNull())
00209       return false;
00210 
00211     out = value.getUnsignedLong();
00212     return true;
00213   }
00214 
00215 #if INT_INT32_T_CONFLICT != 1
00216   inline bool operator>> (const Value& value, int32_t& out)
00217   {
00218     if (value.isNull())
00219       return false;
00220 
00221     out = value.getInt32();
00222     return true;
00223   }
00224 #endif
00225 
00226 #if UNSIGNED_UINT32_T_CONFLICT != 1
00227   inline bool operator>> (const Value& value, uint32_t& out)
00228   {
00229     if (value.isNull())
00230       return false;
00231 
00232     out = value.getUnsigned32();
00233     return true;
00234   }
00235 #endif
00236 
00237 #if INT_INT64_T_CONFLICT != 1
00238   inline bool operator>> (const Value& value, int64_t& out)
00239   {
00240     if (value.isNull())
00241       return false;
00242 
00243     out = value.getInt64();
00244     return true;
00245   }
00246 #endif
00247 
00248 #if UNSIGNED_UINT64_T_CONFLICT != 1
00249   inline bool operator>> (const Value& value, uint64_t& out)
00250   {
00251     if (value.isNull())
00252       return false;
00253 
00254     out = value.getUnsigned64();
00255     return true;
00256   }
00257 #endif
00258 
00259   inline bool operator>> (const Value& value, Decimal& out)
00260   {
00261     if (value.isNull())
00262       return false;
00263 
00264     out = value.getDecimal();
00265     return true;
00266   }
00267 
00268   inline bool operator>> (const Value& value, float& out)
00269   {
00270     if (value.isNull())
00271       return false;
00272 
00273     out = value.getFloat();
00274     return true;
00275   }
00276 
00277   inline bool operator>> (const Value& value, double& out)
00278   {
00279     if (value.isNull())
00280       return false;
00281 
00282     out = value.getDouble();
00283     return true;
00284   }
00285 
00286   inline bool operator>> (const Value& value, char& out)
00287   {
00288     if (value.isNull())
00289       return false;
00290 
00291     out = value.getChar();
00292     return true;
00293   }
00294 
00295   inline bool operator>> (const Value& value, std::string& out)
00296   {
00297     if (value.isNull())
00298       return false;
00299 
00300     value.getString(out);
00301     return true;
00302   }
00303 
00304   inline bool operator>> (const Value& value, Blob& out)
00305   {
00306     if (value.isNull())
00307       return false;
00308 
00309     value.getBlob(out);
00310     return true;
00311   }
00312 
00313   inline bool operator>> (const Value& value, Date& out)
00314   {
00315     if (value.isNull())
00316       return false;
00317 
00318     out = value.getDate();
00319     return true;
00320   }
00321 
00322   inline bool operator>> (const Value& value, Time& out)
00323   {
00324     if (value.isNull())
00325       return false;
00326 
00327     out = value.getTime();
00328     return true;
00329   }
00330 
00331   inline bool operator>> (const Value& value, Datetime& out)
00332   {
00333     if (value.isNull())
00334       return false;
00335 
00336     out = value.getDatetime();
00337     return true;
00338   }
00340 
00341 }
00342 
00343 #endif // TNTDB_BITS_VALUE_H
00344 
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6