1 | // |
---|
2 | // DynamicAny.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/DynamicAny.cpp#7 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Core |
---|
8 | // Module: DynamicAny |
---|
9 | // |
---|
10 | // Copyright (c) 2007, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include "Poco/DynamicAny.h" |
---|
38 | #include <algorithm> |
---|
39 | #include <cctype> |
---|
40 | |
---|
41 | |
---|
42 | namespace Poco { |
---|
43 | |
---|
44 | |
---|
45 | DynamicAny::DynamicAny(): _pHolder(0) |
---|
46 | { |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | DynamicAny::DynamicAny(const char* pVal): |
---|
51 | _pHolder(new DynamicAnyHolderImpl<std::string>(pVal)) |
---|
52 | { |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | DynamicAny::DynamicAny(const DynamicAny& other): |
---|
57 | _pHolder(0) |
---|
58 | { |
---|
59 | if (other._pHolder) |
---|
60 | _pHolder = other._pHolder->clone(); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | DynamicAny::~DynamicAny() |
---|
65 | { |
---|
66 | delete _pHolder; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | DynamicAny& DynamicAny::operator = (const DynamicAny& other) |
---|
71 | { |
---|
72 | DynamicAny tmp(other); |
---|
73 | swap(tmp); |
---|
74 | return *this; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | const DynamicAny DynamicAny::operator + (const DynamicAny& other) const |
---|
79 | { |
---|
80 | if (isInteger()) |
---|
81 | { |
---|
82 | if(isSigned()) |
---|
83 | return add<Poco::Int64>(other); |
---|
84 | else |
---|
85 | return add<Poco::UInt64>(other); |
---|
86 | } |
---|
87 | else if (isNumeric()) |
---|
88 | return add<double>(other); |
---|
89 | else if (isString()) |
---|
90 | return add<std::string>(other); |
---|
91 | else |
---|
92 | throw InvalidArgumentException("Invalid operation for this data type."); |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | DynamicAny& DynamicAny::operator += (const DynamicAny& other) |
---|
97 | { |
---|
98 | if (isInteger()) |
---|
99 | { |
---|
100 | if(isSigned()) |
---|
101 | return *this = add<Poco::Int64>(other); |
---|
102 | else |
---|
103 | return *this = add<Poco::UInt64>(other); |
---|
104 | } |
---|
105 | else if (isNumeric()) |
---|
106 | return *this = add<double>(other); |
---|
107 | else if (isString()) |
---|
108 | return *this = add<std::string>(other); |
---|
109 | else |
---|
110 | throw InvalidArgumentException("Invalid operation for this data type."); |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | const DynamicAny DynamicAny::operator - (const DynamicAny& other) const |
---|
115 | { |
---|
116 | if (isInteger()) |
---|
117 | { |
---|
118 | if(isSigned()) |
---|
119 | return subtract<Poco::Int64>(other); |
---|
120 | else |
---|
121 | return subtract<Poco::UInt64>(other); |
---|
122 | } |
---|
123 | else if (isNumeric()) |
---|
124 | return subtract<double>(other); |
---|
125 | else |
---|
126 | throw InvalidArgumentException("Invalid operation for this data type."); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | DynamicAny& DynamicAny::operator -= (const DynamicAny& other) |
---|
131 | { |
---|
132 | if (isInteger()) |
---|
133 | { |
---|
134 | if(isSigned()) |
---|
135 | return *this = subtract<Poco::Int64>(other); |
---|
136 | else |
---|
137 | return *this = subtract<Poco::UInt64>(other); |
---|
138 | } |
---|
139 | else if (isNumeric()) |
---|
140 | return *this = subtract<double>(other); |
---|
141 | else |
---|
142 | throw InvalidArgumentException("Invalid operation for this data type."); |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | const DynamicAny DynamicAny::operator * (const DynamicAny& other) const |
---|
147 | { |
---|
148 | if (isInteger()) |
---|
149 | { |
---|
150 | if(isSigned()) |
---|
151 | return multiply<Poco::Int64>(other); |
---|
152 | else |
---|
153 | return multiply<Poco::UInt64>(other); |
---|
154 | } |
---|
155 | else if (isNumeric()) |
---|
156 | return multiply<double>(other); |
---|
157 | else |
---|
158 | throw InvalidArgumentException("Invalid operation for this data type."); |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | DynamicAny& DynamicAny::operator *= (const DynamicAny& other) |
---|
163 | { |
---|
164 | if (isInteger()) |
---|
165 | { |
---|
166 | if(isSigned()) |
---|
167 | return *this = multiply<Poco::Int64>(other); |
---|
168 | else |
---|
169 | return *this = multiply<Poco::UInt64>(other); |
---|
170 | } |
---|
171 | else if (isNumeric()) |
---|
172 | return *this = multiply<double>(other); |
---|
173 | else |
---|
174 | throw InvalidArgumentException("Invalid operation for this data type."); |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | const DynamicAny DynamicAny::operator / (const DynamicAny& other) const |
---|
179 | { |
---|
180 | if (isInteger()) |
---|
181 | { |
---|
182 | if(isSigned()) |
---|
183 | return divide<Poco::Int64>(other); |
---|
184 | else |
---|
185 | return divide<Poco::UInt64>(other); |
---|
186 | } |
---|
187 | else if (isNumeric()) |
---|
188 | return divide<double>(other); |
---|
189 | else |
---|
190 | throw InvalidArgumentException("Invalid operation for this data type."); |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | DynamicAny& DynamicAny::operator /= (const DynamicAny& other) |
---|
195 | { |
---|
196 | if (isInteger()) |
---|
197 | { |
---|
198 | if(isSigned()) |
---|
199 | return *this = divide<Poco::Int64>(other); |
---|
200 | else |
---|
201 | return *this = divide<Poco::UInt64>(other); |
---|
202 | } |
---|
203 | else if (isNumeric()) |
---|
204 | return *this = divide<double>(other); |
---|
205 | else |
---|
206 | throw InvalidArgumentException("Invalid operation for this data type."); |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | DynamicAny& DynamicAny::operator ++ () |
---|
211 | { |
---|
212 | if (!isInteger()) |
---|
213 | throw InvalidArgumentException("Invalid operation for this data type."); |
---|
214 | |
---|
215 | return *this = *this + 1; |
---|
216 | } |
---|
217 | |
---|
218 | const DynamicAny DynamicAny::operator ++ (int) |
---|
219 | { |
---|
220 | if (!isInteger()) |
---|
221 | throw InvalidArgumentException("Invalid operation for this data type."); |
---|
222 | |
---|
223 | DynamicAny tmp(*this); |
---|
224 | *this += 1; |
---|
225 | return tmp; |
---|
226 | } |
---|
227 | |
---|
228 | DynamicAny& DynamicAny::operator -- () |
---|
229 | { |
---|
230 | if (!isInteger()) |
---|
231 | throw InvalidArgumentException("Invalid operation for this data type."); |
---|
232 | |
---|
233 | return *this = *this - 1; |
---|
234 | } |
---|
235 | |
---|
236 | const DynamicAny DynamicAny::operator -- (int) |
---|
237 | { |
---|
238 | if (!isInteger()) |
---|
239 | throw InvalidArgumentException("Invalid operation for this data type."); |
---|
240 | |
---|
241 | DynamicAny tmp(*this); |
---|
242 | *this -= 1; |
---|
243 | return tmp; |
---|
244 | } |
---|
245 | |
---|
246 | |
---|
247 | bool DynamicAny::operator == (const DynamicAny& other) const |
---|
248 | { |
---|
249 | if (isEmpty() || other.isEmpty()) return false; |
---|
250 | return convert<std::string>() == other.convert<std::string>(); |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | bool DynamicAny::operator == (const char* other) const |
---|
255 | { |
---|
256 | if (isEmpty()) return false; |
---|
257 | return convert<std::string>() == other; |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | bool DynamicAny::operator != (const DynamicAny& other) const |
---|
262 | { |
---|
263 | if (isEmpty() && other.isEmpty()) return false; |
---|
264 | else if (isEmpty() || other.isEmpty()) return true; |
---|
265 | |
---|
266 | return convert<std::string>() != other.convert<std::string>(); |
---|
267 | } |
---|
268 | |
---|
269 | |
---|
270 | bool DynamicAny::operator != (const char* other) const |
---|
271 | { |
---|
272 | if (isEmpty()) return true; |
---|
273 | return convert<std::string>() != other; |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | bool DynamicAny::operator < (const DynamicAny& other) const |
---|
278 | { |
---|
279 | if (isEmpty() || other.isEmpty()) return false; |
---|
280 | return convert<std::string>() < other.convert<std::string>(); |
---|
281 | } |
---|
282 | |
---|
283 | |
---|
284 | bool DynamicAny::operator <= (const DynamicAny& other) const |
---|
285 | { |
---|
286 | if (isEmpty() || other.isEmpty()) return false; |
---|
287 | return convert<std::string>() <= other.convert<std::string>(); |
---|
288 | } |
---|
289 | |
---|
290 | |
---|
291 | bool DynamicAny::operator > (const DynamicAny& other) const |
---|
292 | { |
---|
293 | if (isEmpty() || other.isEmpty()) return false; |
---|
294 | return convert<std::string>() > other.convert<std::string>(); |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | bool DynamicAny::operator >= (const DynamicAny& other) const |
---|
299 | { |
---|
300 | if (isEmpty() || other.isEmpty()) return false; |
---|
301 | return convert<std::string>() >= other.convert<std::string>(); |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | bool DynamicAny::operator || (const DynamicAny& other) const |
---|
306 | { |
---|
307 | if (isEmpty() || other.isEmpty()) return false; |
---|
308 | return convert<bool>() || other.convert<bool>(); |
---|
309 | } |
---|
310 | |
---|
311 | |
---|
312 | bool DynamicAny::operator && (const DynamicAny& other) const |
---|
313 | { |
---|
314 | if (isEmpty() || other.isEmpty()) return false; |
---|
315 | return convert<bool>() && other.convert<bool>(); |
---|
316 | } |
---|
317 | |
---|
318 | |
---|
319 | void DynamicAny::empty() |
---|
320 | { |
---|
321 | delete _pHolder; |
---|
322 | _pHolder = 0; |
---|
323 | } |
---|
324 | |
---|
325 | |
---|
326 | } // namespace Poco::Poco |
---|