00001 #ifndef __INSTANTIO_FIELDACCESSOR_H
00002 #define __INSTANTIO_FIELDACCESSOR_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 "BasicFieldAccessor.h"
00028 #include <string>
00029
00030
00031 namespace InstantIO
00032 {
00033
00034
00035 class Node;
00036
00043 template <class ClassType, class ValueType>
00044 class FieldAccessor: public BasicFieldAccessor
00045 {
00046 public:
00047
00049 typedef void (ClassType::*SetMethod)(const ValueType &);
00050
00052 typedef ValueType (ClassType::*GetMethod)() const;
00053
00061 inline FieldAccessor(SetMethod setMethod, GetMethod getMethod)
00062 : BasicFieldAccessor(), setMethod_(setMethod), getMethod_(getMethod)
00063 {}
00064
00070 inline FieldAccessor(const FieldAccessor<ClassType, ValueType> &other)
00071 : BasicFieldAccessor(other), setMethod_(other.setMethod_), getMethod_(other.getMethod_)
00072 {}
00073
00075 virtual ~FieldAccessor() {};
00076
00082 virtual void set(Node &node, const std::string &value) const
00083 {
00084 ValueType result;
00085 string2Value(value, result);
00086 ClassType &typedNode = dynamic_cast<ClassType&>(node);
00087 (typedNode.*setMethod_)(result);
00088 };
00089
00095 virtual std::string get(const Node &node) const
00096 {
00097 const ClassType &typedNode = dynamic_cast<const ClassType&>(node);
00098 return value2String((typedNode.*getMethod_)());
00099 };
00100
00108 virtual bool sameValue(const Node &node, const std::string &value) const
00109 {
00110 ValueType v;
00111 string2Value(value, v);
00112 const ClassType &typedNode = dynamic_cast<const ClassType&>(node);
00113 return v == (typedNode.*getMethod_)();
00114 };
00115
00120 virtual BasicFieldAccessor *clone() const
00121 {
00122 return new FieldAccessor<ClassType, ValueType>(*this);
00123 };
00124
00125 private:
00126
00128 const FieldAccessor<ClassType, ValueType> &operator=(const FieldAccessor<ClassType, ValueType> &);
00129
00131 SetMethod setMethod_;
00132
00134 GetMethod getMethod_;
00135 };
00136
00137
00138 }
00139
00140
00141 #endif // __INSTANTIO_FIELDACCESSOR_H