* Add newlines at the end of files.
[matthijs/ABM2.git] / ABM2 / Engine / dator.h
1 #ifndef DATOR_H_INCLUDED\r
2 #define DATOR_H_INCLUDED\r
3 \r
4 #include "mmanager.h"\r
5 \r
6 class BaseDator : public IMMObject\r
7 {\r
8 protected:\r
9         BaseDator(){}\r
10         BaseDator(BaseDator &b){(*this)=b;}\r
11 public:\r
12         virtual BaseDator &operator =(std::string &s)=0;\r
13         virtual BaseDator &operator +=(std::string &s)=0;\r
14         virtual BaseDator &operator -=(std::string &s)=0;\r
15         virtual bool operator ==(std::string &s)=0;\r
16         virtual bool operator !=(std::string &s)=0;\r
17 \r
18         virtual bool hasMultipleValues()=0;\r
19 \r
20         virtual operator std::string()=0;\r
21 };\r
22 \r
23 template<class T>\r
24 class Dator : public BaseDator\r
25 {\r
26 protected:\r
27         T& target;\r
28         T toVal(std::string &s)\r
29         {\r
30                 std::stringstream str;\r
31                 str.unsetf(std::ios::skipws);\r
32                 str<<s;\r
33                 T res;\r
34                 str>>res;\r
35                 return res;\r
36         }\r
37         std::string toString(T &val)\r
38         {\r
39                 std::stringstream str;\r
40                 str.unsetf(std::ios::skipws);\r
41                 str<<val;\r
42                 std::string res;\r
43                 str>>res;\r
44                 return res;\r
45         }\r
46 public:\r
47         Dator(T& t) : target(t) {}\r
48         BaseDator &operator =(std::string &s) { target=toVal(s); return *this; }\r
49         BaseDator &operator +=(std::string &s) { target+=toVal(s); return *this; }\r
50         BaseDator &operator -=(std::string &s) { target-=toVal(s); return *this; }\r
51         bool operator ==(std::string &s) { return (s==(std::string)(*this)); }\r
52         bool operator !=(std::string &s) { return (s!=(std::string)(*this)); }\r
53         operator std::string() { return toString(target); }\r
54 \r
55         bool hasMultipleValues() { return false; }\r
56 \r
57         AUTO_SIZE;\r
58 };\r
59 \r
60 template<class T>\r
61 class ListDator : public BaseDator\r
62 {\r
63 protected:\r
64         std::list<T> &values;\r
65         T toVal(std::string &s)\r
66         {\r
67                 std::stringstream str;\r
68                 str.unsetf(std::ios::skipws);\r
69                 str<<s;\r
70                 T res;\r
71                 str>>res;\r
72                 return res;\r
73         }\r
74         std::string toString(T &val)\r
75         {\r
76                 std::stringstream str;\r
77                 str.unsetf(std::ios::skipws);\r
78                 str<<val;\r
79                 std::string res;\r
80                 str>>res;\r
81                 return res;\r
82         }\r
83 public:\r
84         ListDator(std::list<T> &v) : values(v) { }\r
85         BaseDator &operator =(std::string &s) { values.clear(); values.push_back(toVal(s)); return *this; }\r
86         BaseDator &operator +=(std::string &s) { values.push_back(toVal(s)); return *this; }\r
87         BaseDator &operator -=(std::string &s) { values.remove(toVal(s)); return *this; }\r
88         bool operator ==(std::string &s) { return (std::find(values.begin(),values.end(),toVal(s))!=values.end()); }\r
89         bool operator !=(std::string &s) { return !((*this)==s); }\r
90         \r
91         operator std::string() { return toString(values.back()); }\r
92         operator std::list<T>&() { return values; }\r
93 \r
94         bool hasMultipleValues(){return true;}\r
95 \r
96         AUTO_SIZE;\r
97 \r
98 \r
99 };\r
100 \r
101 #endif\r