00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00035 #ifndef KEYVALUE_MNGT_BUILDER_BUILDERFROM_H_
00036 #define KEYVALUE_MNGT_BUILDER_BUILDERFROM_H_
00037
00038 #include "keyvalue/extern/SharedPtr.h"
00039 #include "keyvalue/frontend/LexicalToolKit.h"
00040 #include "keyvalue/sys/exception/Exception.h"
00041 #include "keyvalue/value/Variant.h"
00042
00043 namespace keyvalue {
00044
00058 template<typename ObjectType>
00059 class BuilderFromVariant {
00060
00061 public:
00062
00091 shared_ptr<ObjectType>
00092 getObject(const value::Variant& data) const;
00093
00094 private:
00095
00096 virtual const char*
00097 getName() const = 0;
00098
00112 template<typename InputType>
00113 shared_ptr<ObjectType>
00114 build(const value::Variant& data) const;
00115
00116 };
00117
00134 template<typename ObjectType, typename InputType>
00135 class BuilderFrom {
00136
00137 public:
00138
00149 virtual shared_ptr<ObjectType>
00150 getObject(const InputType& data) const = 0;
00151
00152 };
00153
00154
00155
00156
00157
00158 template<typename ObjectType>
00159 shared_ptr<ObjectType> BuilderFromVariant<ObjectType>::getObject(
00160 const value::Variant& data) const {
00161
00162 using frontend::LexicalToolKit;
00163
00164 if (data.is<bool> (LexicalToolKit::input))
00165 return build<bool> (data);
00166
00167 if (data.is<double> (LexicalToolKit::input))
00168 return build<double> (data);
00169
00170 if (data.is<ptime> (LexicalToolKit::input))
00171 return build<ptime> (data);
00172
00173 if (data.is<string> (LexicalToolKit::input))
00174 return build<string> (data);
00175
00176 throw ::keyvalue::LogicError() & "Unknown keyvalue::Variant content!";
00177 }
00178
00179
00180
00181
00182
00183 template<typename ObjectType>
00184 template<typename InputType>
00185 shared_ptr<ObjectType> BuilderFromVariant<ObjectType>::build(
00186 const value::Variant& data) const {
00187
00188 const BuilderFrom<ObjectType, InputType>* ptr(
00189 dynamic_cast<const BuilderFrom<ObjectType, InputType>*> (this));
00190
00191 using frontend::LexicalToolKit;
00192 if (ptr)
00193 return ptr->getObject(data.get<InputType> (LexicalToolKit::input));
00194
00195 throw RuntimeError() & "Builder '" & getName() & "'cannot build from "
00196 "input " & data & "'!";
00197 }
00198
00199 }
00200
00201 #endif // KEYVALUE_MNGT_BUILDER_BUILDERFROM_H_