value.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005,2010 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef TNTDB_BITS_VALUE_H
30 #define TNTDB_BITS_VALUE_H
31 
32 #include <tntdb/iface/ivalue.h>
33 #include <tntdb/date.h>
34 #include <tntdb/time.h>
35 #include <tntdb/datetime.h>
36 #include <tntdb/decimal.h>
37 #include <tntdb/blob.h>
38 #include <tntdb/config.h>
39 #include <cxxtools/smartptr.h>
40 
41 namespace tntdb
42 {
46  class Value
47  {
49 
50  public:
51  explicit Value(IValue* value = 0)
52  : value(value)
53  { }
54 
56 
62 
63  bool isNull() const { return !value || value->isNull(); }
65  bool getBool() const { return value->getBool(); }
67  short getShort() const { return value->getShort(); }
69  int getInt() const { return value->getInt(); }
71  long getLong() const { return value->getLong(); }
73  unsigned short getUnsignedShort() const { return value->getUnsignedShort(); }
75  unsigned getUnsigned() const { return value->getUnsigned(); }
77  unsigned long getUnsignedLong() const { return value->getUnsignedLong(); }
79  int32_t getInt32() const { return value->getInt32(); }
81  uint32_t getUnsigned32() const { return value->getUnsigned32(); }
83  int64_t getInt64() const { return value->getInt64(); }
85  uint64_t getUnsigned64() const { return value->getUnsigned64(); }
87  Decimal getDecimal() const { return value->getDecimal(); }
89  float getFloat() const { return value->getFloat(); }
91  double getDouble() const { return value->getDouble(); }
93  char getChar() const { return value->getChar(); }
95  std::string getString() const
96  { std::string ret; value->getString(ret); return ret; }
100  void getString(std::string& ret) const
101  { value->getString(ret); }
104  { cxxtools::String ret; value->getUString(ret); return ret; }
105  void getUString(cxxtools::String& ret) const
106  { value->getUString(ret); }
110  Blob getBlob() const
111  { Blob ret; value->getBlob(ret); return ret; }
113  void getBlob(Blob& blob) const
114  { value->getBlob(blob); }
116  Date getDate() const { return value->getDate(); }
118  Time getTime() const { return value->getTime(); }
120  Datetime getDatetime() const { return value->getDatetime(); }
122 
138  template <typename T>
139  bool getValue(T& ret) const
140  { return *this >> ret; }
141 
143  template <typename T>
144  bool get(T& ret) const
145  { return *this >> ret; }
146 
148  bool operator!() const { return !value; }
150  const IValue* getImpl() const { return &*value; }
151  };
152 
154 
157  inline bool operator>> (const Value& value, bool& out)
158  {
159  if (value.isNull())
160  return false;
161 
162  out = value.getBool();
163  return true;
164  }
165 
166  inline bool operator>> (const Value& value, short& out)
167  {
168  if (value.isNull())
169  return false;
170 
171  out = value.getShort();
172  return true;
173  }
174 
175  inline bool operator>> (const Value& value, int& out)
176  {
177  if (value.isNull())
178  return false;
179 
180  out = value.getInt();
181  return true;
182  }
183 
184  inline bool operator>> (const Value& value, long& out)
185  {
186  if (value.isNull())
187  return false;
188 
189  out = value.getLong();
190  return true;
191  }
192 
193  inline bool operator>> (const Value& value, unsigned short& out)
194  {
195  if (value.isNull())
196  return false;
197 
198  out = value.getUnsignedShort();
199  return true;
200  }
201 
202  inline bool operator>> (const Value& value, unsigned& out)
203  {
204  if (value.isNull())
205  return false;
206 
207  out = value.getUnsigned();
208  return true;
209  }
210 
211  inline bool operator>> (const Value& value, unsigned long& out)
212  {
213  if (value.isNull())
214  return false;
215 
216  out = value.getUnsignedLong();
217  return true;
218  }
219 
220 #if INT_INT32_T_CONFLICT != 1
221  inline bool operator>> (const Value& value, int32_t& out)
222  {
223  if (value.isNull())
224  return false;
225 
226  out = value.getInt32();
227  return true;
228  }
229 #endif
230 
231 #if UNSIGNED_UINT32_T_CONFLICT != 1
232  inline bool operator>> (const Value& value, uint32_t& out)
233  {
234  if (value.isNull())
235  return false;
236 
237  out = value.getUnsigned32();
238  return true;
239  }
240 #endif
241 
242 #if INT_INT64_T_CONFLICT != 1
243  inline bool operator>> (const Value& value, int64_t& out)
244  {
245  if (value.isNull())
246  return false;
247 
248  out = value.getInt64();
249  return true;
250  }
251 #endif
252 
253 #if UNSIGNED_UINT64_T_CONFLICT != 1
254  inline bool operator>> (const Value& value, uint64_t& out)
255  {
256  if (value.isNull())
257  return false;
258 
259  out = value.getUnsigned64();
260  return true;
261  }
262 #endif
263 
264  inline bool operator>> (const Value& value, Decimal& out)
265  {
266  if (value.isNull())
267  return false;
268 
269  out = value.getDecimal();
270  return true;
271  }
272 
273  inline bool operator>> (const Value& value, float& out)
274  {
275  if (value.isNull())
276  return false;
277 
278  out = value.getFloat();
279  return true;
280  }
281 
282  inline bool operator>> (const Value& value, double& out)
283  {
284  if (value.isNull())
285  return false;
286 
287  out = value.getDouble();
288  return true;
289  }
290 
291  inline bool operator>> (const Value& value, char& out)
292  {
293  if (value.isNull())
294  return false;
295 
296  out = value.getChar();
297  return true;
298  }
299 
300  inline bool operator>> (const Value& value, std::string& out)
301  {
302  if (value.isNull())
303  return false;
304 
305  value.getString(out);
306  return true;
307  }
308 
309  inline bool operator>> (const Value& value, cxxtools::String& out)
310  {
311  if (value.isNull())
312  return false;
313 
314  value.getUString(out);
315  return true;
316  }
317 
318  inline bool operator>> (const Value& value, Blob& out)
319  {
320  if (value.isNull())
321  return false;
322 
323  value.getBlob(out);
324  return true;
325  }
326 
327  inline bool operator>> (const Value& value, Date& out)
328  {
329  if (value.isNull())
330  return false;
331 
332  out = value.getDate();
333  return true;
334  }
335 
336  inline bool operator>> (const Value& value, Time& out)
337  {
338  if (value.isNull())
339  return false;
340 
341  out = value.getTime();
342  return true;
343  }
344 
345  inline bool operator>> (const Value& value, Datetime& out)
346  {
347  if (value.isNull())
348  return false;
349 
350  out = value.getDatetime();
351  return true;
352  }
354 
355 }
356 
357 #endif // TNTDB_BITS_VALUE_H
358