00001 #ifndef __INSTANTIO_BASICFIELDACCESSOR_H
00002 #define __INSTANTIO_BASICFIELDACCESSOR_H
00003
00004 #ifdef _MSC_VER
00005 # pragma once
00006 #endif
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "InstantIODef.h"
00027 #include "InvalidValueException.h"
00028 #include <string>
00029 #include <sstream>
00030
00031
00032 namespace InstantIO
00033 {
00034
00035 #define INSTANTIO_SETTER(type, function_name, variable_name) \
00036 void set##function_name(const type &temp) \
00037 { \
00038 variable_name = temp; \
00039 }
00040
00041 #define INSTANTIO_GETTER(type, function_name, variable_name) \
00042 type get##function_name() const \
00043 { \
00044 return variable_name; \
00045 }
00046
00047 #define INSTANTIO_SETTER_GETTER(type, function_name, variable_name) \
00048 INSTANTIO_GETTER(type, function_name, variable_name) \
00049 INSTANTIO_SETTER(type, function_name, variable_name)
00050
00051 class Node;
00052
00053 #define STRING_DELIMITER " "
00054
00055
00062 class INSTANTIO_DLLMAPPING BasicFieldAccessor
00063 {
00064 public:
00065
00067 virtual ~BasicFieldAccessor();
00068
00074 virtual void set(Node &node, const std::string &value) const = 0;
00075
00081 virtual std::string get(const Node &node) const = 0;
00082
00090 virtual bool sameValue(const Node &node, const std::string &value) const = 0;
00091
00096 virtual BasicFieldAccessor *clone() const = 0;
00097
00098 protected:
00099
00101 inline BasicFieldAccessor() {}
00102
00108 inline BasicFieldAccessor(const BasicFieldAccessor &other) {}
00109
00115 template <class T> static inline const std::string value2String(const T &value)
00116 {
00117
00118 std::ostringstream os;
00119 os << value;
00120 return os.str();
00121 }
00122
00128 template <class C> static inline const std::string valueContainer2String(const C &value)
00129 {
00130 std::ostringstream os;
00131
00132
00133 for(typename C::const_iterator iter = value.begin(); iter != value.end(); ++iter)
00134 os << *iter << STRING_DELIMITER;
00135
00136 return os.str();
00137 }
00138
00144 static const std::string &value2String(bool value);
00145
00151
00152
00153 static inline const std::string value2String(const std::string &value)
00154 { return value; }
00155
00162 template <class T> static inline void string2Value(const std::string &strValue, T &value)
00163 {
00164
00165 std::istringstream is(strValue);
00166 is >> value;
00167 if (is.fail())
00168 throw InvalidValueException(std::string("Cannot convert \"") + strValue + std::string("\" to a value"));
00169
00170 is.get();
00171 if (!is.eof())
00172 throw InvalidValueException(std::string("Cannot convert \"") + strValue + std::string("\" to a value"));
00173 }
00174
00181 template <class T, class C> static inline void string2ValueContainer(const std::string &strValue, C &value)
00182 {
00183 unsigned offset = 0;
00184 while(offset < strValue.size())
00185 {
00186 const unsigned begin = strValue.find_first_not_of(STRING_DELIMITER, offset);
00187 const unsigned end = strValue.find_first_of(STRING_DELIMITER, begin);
00188 const int length = (end == -1) ? -1 : end - begin;
00189
00190 T result;
00191 string2Value(strValue.substr(begin, length), result);
00192
00193 value.push_back(result);
00194 offset = end;
00195 }
00196 }
00197
00204 static void string2Value(const std::string &strValue, bool &value);
00205
00211 static inline void string2Value(const std::string &strValue, std::string &value)
00212 { value = strValue; }
00213
00214 private:
00215
00217 const BasicFieldAccessor &operator=(const BasicFieldAccessor &);
00218 };
00219
00220
00221 }
00222
00223
00224 #endif // __INSTANTIO_BASICFIELDACCESSOR_H