Variant.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #ifndef KEYVALUE_VALUE_VARIANT_H_
00029 #define KEYVALUE_VALUE_VARIANT_H_
00030
00031 #include <iosfwd>
00032
00033 #include <boost/variant.hpp>
00034
00035 #include "keyvalue/extern/String.h"
00036 #include "keyvalue/extern/Ptime.h"
00037 #include "keyvalue/frontend/LexicalToolKit.h"
00038 #include "keyvalue/value/Nothing.h"
00039
00040 namespace keyvalue {
00041 namespace value {
00042
00043
00044 class Single;
00045
00059 class Variant {
00060
00061 public:
00062
00063 typedef boost::variant<Nothing, bool, double, ptime, string> DataType_;
00064
00068 Variant();
00069
00077 Variant(const DataType_& data);
00078
00086 template <typename Rhs>
00087 Variant(const Rhs& rhs);
00088
00096 Variant(const Single& data);
00097
00107 template <typename Rhs>
00108 Variant&
00109 operator=(const Rhs& rhs);
00110
00141 template <typename ElementType>
00142 bool
00143 is(frontend::LexicalToolKit::IO io) const;
00144
00179 template <typename ElementType>
00180 ElementType
00181 get(frontend::LexicalToolKit::IO io) const;
00182
00183 friend std::ostream&
00184 operator<<(std::ostream& os, const Variant& variant);
00185
00186 private:
00187
00199 template <typename ElementType>
00200 const ElementType*
00201 get() const;
00202
00203 DataType_ data_;
00204
00205 };
00206
00212 std::ostream&
00213 operator<<(std::ostream& os, const Variant& rhs);
00214
00235 template <typename Rhs>
00236 struct Parent;
00237
00238 template <>
00239 struct Parent<Variant::DataType_> {
00240 typedef Variant Type_;
00241 };
00242
00243 template <>
00244 struct Parent<Nothing> {
00245 typedef Variant::DataType_ Type_;
00246 };
00247
00248 template <>
00249 struct Parent<bool> {
00250 typedef Variant::DataType_ Type_;
00251 };
00252
00253 template <>
00254 struct Parent<double> {
00255 typedef Variant::DataType_ Type_;
00256 };
00257
00258 template <>
00259 struct Parent<unsigned int> {
00260 typedef double Type_;
00261 };
00262
00263 template <>
00264 struct Parent<long unsigned int> {
00265 typedef double Type_;
00266 };
00267
00268 template <>
00269 struct Parent<string> {
00270 typedef Variant::DataType_ Type_;
00271 };
00272
00273 template <>
00274 struct Parent<char[]> {
00275 typedef string Type_;
00276 };
00277
00278 template <size_t N>
00279 struct Parent<char[N]> {
00280 typedef string Type_;
00281 };
00282
00283 template <size_t N>
00284 struct Parent<const char[N]> {
00285 typedef string Type_;
00286 };
00287
00288 template <>
00289 struct Parent<char *> {
00290 typedef string Type_;
00291 };
00292
00293 template <>
00294 struct Parent<ptime> {
00295 typedef Variant::DataType_ Type_;
00296 };
00297
00298
00299
00300
00301
00302 template <typename Rhs>
00303 Variant::Variant(const Rhs& rhs) :
00304 data_(Variant(typename Parent<Rhs>::Type_(rhs)).data_) {
00309 }
00310
00311
00312
00313
00314
00315 template <typename Rhs>
00316 Variant&
00317 Variant::operator=(const Rhs& rhs) {
00322 return *this = typename Parent<Rhs>::Type_(rhs);
00323 }
00324
00325 }
00326 }
00327
00328 #endif // KEYVALUE_VALUE_VARIANT_H_