EMANE  1.2.1
any.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013,2015 - Adjacent Link LLC, Bridgewater, New Jersey
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * * Neither the name of Adjacent Link LLC nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "emane/any.h"
35 
36 EMANE::Any::Any(std::int64_t i64Value):
37  type_{Type::TYPE_INT64},
38  i64Value_{i64Value}{}
39 
40 EMANE::Any::Any(std::uint64_t u64Value):
41  type_{Type::TYPE_UINT64},
42  u64Value_{u64Value}{}
43 
44 EMANE::Any::Any(std::int32_t i32Value):
45  type_{Type::TYPE_INT32},
46  i64Value_{i32Value}{}
47 
48 EMANE::Any::Any(std::uint32_t u32Value):
49  type_{Type::TYPE_UINT32},
50  u64Value_{u32Value}{}
51 
52 EMANE::Any::Any(std::int16_t i16Value):
53  type_{Type::TYPE_INT16},
54  i64Value_{i16Value}{}
55 
56 EMANE::Any::Any(std::uint16_t u16Value):
57  type_{Type::TYPE_UINT16},
58  u64Value_{u16Value}{}
59 
60 EMANE::Any::Any(std::int8_t i8Value):
61  type_{Type::TYPE_INT8},
62  i64Value_{i8Value}{}
63 
64 EMANE::Any::Any(std::uint8_t u8Value):
65  type_{Type::TYPE_UINT8},
66  u64Value_(u8Value){}
67 
68 EMANE::Any::Any(float fValue):
69  type_{Type::TYPE_FLOAT},
70  dValue_{fValue}{}
71 
72 EMANE::Any::Any(double dValue):
73  type_{Type::TYPE_DOUBLE},
74  dValue_{dValue}{}
75 
76 EMANE::Any::Any(const INETAddr & addrValue):
77  type_{Type::TYPE_INET_ADDR},
78  addrValue_(addrValue){}
79 
80 EMANE::Any::Any(const bool & bValue):
81  type_{Type::TYPE_BOOL},
82  u64Value_(bValue){}
83 
84 EMANE::Any::Any(const char * pzValue):
85  type_{Type::TYPE_STRING},
86  sValue_{pzValue}{}
87 
88 EMANE::Any::Any(const std::string & sValue):
89  type_{Type::TYPE_STRING},
90  sValue_{sValue}{}
91 
92 EMANE::Any::Any(const Any& rhs)
93 {
94  type_ = rhs.type_;
95 
96  if(type_ == Type::TYPE_STRING)
97  {
98  new (&sValue_) std::string(rhs.sValue_);
99  }
100  else if(type_ == Type::TYPE_INET_ADDR)
101  {
102  new (&addrValue_) INETAddr(rhs.addrValue_);
103  }
104  else
105  {
106  memcpy(this,&rhs,sizeof(Any));
107  }
108 }
109 
111 {
112  // if std::string or INETAddr cleanup
113  this->~Any();
114 
115  type_ = rhs.type_;
116 
117  // if std::string or INETAddr use placement new
118  // to create object
119  if(type_ == Type::TYPE_STRING)
120  {
121  new (&sValue_) std::string(rhs.sValue_);
122  }
123  else if(type_ == Type::TYPE_INET_ADDR)
124  {
125  new (&addrValue_) INETAddr(rhs.addrValue_);
126  }
127  else
128  {
129  // copy state as binary represenation
130  memcpy(this,&rhs,sizeof(Any));
131  }
132 
133  return *this;
134 }
135 
137 {
138  // if std::string or INETAddr cleanup
139  if(type_ == Type::TYPE_STRING)
140  {
141  sValue_.~basic_string<char>();
142  }
143  else if(type_ == Type::TYPE_INET_ADDR)
144  {
146  }
147 }
148 
149 std::int64_t EMANE::Any::asINT64() const
150 {
151  if(type_ == Type::TYPE_INT64)
152  {
153  return i64Value_;
154  }
155 
156  throw AnyException("Not INT64");
157 }
158 
159 std::uint64_t EMANE::Any::asUINT64() const
160 {
161  if(type_ == Type::TYPE_UINT64)
162  {
163  return u64Value_;
164  }
165 
166  throw AnyException("Not UINT64");
167 }
168 
169 std::int32_t EMANE::Any::asINT32() const
170 {
171  if(type_ == Type::TYPE_INT32)
172  {
173  return i64Value_;
174  }
175 
176  throw AnyException("Not INT32");
177 }
178 
179 std::uint32_t EMANE::Any::asUINT32() const
180 {
181  if(type_ == Type::TYPE_UINT32)
182  {
183  return u64Value_;
184  }
185 
186  throw AnyException("Not UINT32");
187 }
188 
189 std::int16_t EMANE::Any::asINT16() const
190 {
191  if(type_ == Type::TYPE_INT16)
192  {
193  return i64Value_;
194  }
195 
196  throw AnyException("Not INT16");
197 }
198 
199 std::uint16_t EMANE::Any::asUINT16() const
200 {
201  if(type_ == Type::TYPE_UINT16)
202  {
203  return u64Value_;
204  }
205 
206  throw AnyException("Not UINT16");
207 }
208 
209 std::int8_t EMANE::Any::asINT8() const
210 {
211  if(type_ == Type::TYPE_INT8)
212  {
213  return i64Value_;
214  }
215 
216  throw AnyException("Not INT8");
217 }
218 
219 std::uint8_t EMANE::Any::asUINT8() const
220 {
221  if(type_ == Type::TYPE_UINT8)
222  {
223  return u64Value_;
224  }
225 
226  throw AnyException("Not UINT8");
227 }
228 
229 float EMANE::Any::asFloat() const
230 {
231  if(type_ == Type::TYPE_FLOAT)
232  {
233  return dValue_;
234  }
235 
236  throw AnyException("Not Float");
237 }
238 
239 double EMANE::Any::asDouble() const
240 {
241  if(type_ == Type::TYPE_DOUBLE)
242  {
243  return dValue_;
244  }
245 
246  throw AnyException("Not Double");
247 }
248 
250 {
251  if(type_ == Type::TYPE_INET_ADDR)
252  {
253  return addrValue_;
254  }
255 
256  throw AnyException("Not INET Addr");
257 }
258 
259 
260 bool EMANE::Any::asBool() const
261 {
262  if(type_ == Type::TYPE_BOOL)
263  {
264  return u64Value_;
265  }
266 
267  throw AnyException("Not Bool");
268 }
269 
270 
271 std::string EMANE::Any::asString() const
272 {
273  if(type_ == Type::TYPE_STRING)
274  {
275  return sValue_;
276  }
277 
278  throw AnyException("Not String");
279 }
280 
282 {
283  return type_;
284 }
285 
286 std::ostream & operator<<(std::ostream &out, const EMANE::Any & any)
287 {
288  out<<any.toString();
289  return out;
290 }
291 
292 std::string EMANE::Any::toString() const
293 {
294  switch(getType())
295  {
297  return std::to_string(asINT64());
298 
300  return std::to_string(asUINT64());
301 
303  return std::to_string(asINT32());
304 
306  return std::to_string(asUINT32());
307 
309  return std::to_string(asINT16());
310 
312  return std::to_string(asUINT16());
313 
315  return std::to_string(asINT8());
316 
318  return std::to_string(asUINT8());
319 
321  return std::to_string(asFloat());
322 
324  return std::to_string(asDouble());
325 
327  return asINETAddr().str();
328  break;
329 
331  return std::to_string(asBool());
332 
334  return asString();
335 
336  default:
337  break;
338  }
339 
340  return {};
341 }
342 
343 EMANE::Any EMANE::Any::create(std::string sValue, Type type)
344 {
345  try
346  {
347  switch(type)
348  {
350  return Any(Utils::ParameterConvert(sValue).toINT64());
351 
353  return Any(Utils::ParameterConvert(sValue).toUINT64());
354 
356  return Any(Utils::ParameterConvert(sValue).toINT32());
357 
359  return Any(Utils::ParameterConvert(sValue).toUINT32());
360 
362  return Any(Utils::ParameterConvert(sValue).toINT16());
363 
365  return Any(Utils::ParameterConvert(sValue).toUINT16());
366 
368  return Any(Utils::ParameterConvert(sValue).toINT8());
369 
371  return Any(Utils::ParameterConvert(sValue).toUINT8());
372 
374  return Any(Utils::ParameterConvert(sValue).toFloat());
375 
377  return Any(Utils::ParameterConvert(sValue).toDouble());
378 
380  return Any(Utils::ParameterConvert(sValue).toINETAddr());
381 
383  return Any(Utils::ParameterConvert(sValue).toBool());
384 
386  return Any(sValue.c_str());
387 
388  default:
389  break;
390  }
391  }
393  {
394  throw makeException<AnyException>("Conversion failure: %s",exp.what());
395  }
396 
397  throw AnyException("Invalid type");
398 }
399 
400 bool EMANE::Any::operator<=(const EMANE::Any & rhs) const
401 {
402  if(type_ == rhs.type_)
403  {
404  switch(type_)
405  {
410  return i64Value_ <= rhs.i64Value_;
411 
417  return u64Value_ <= rhs.u64Value_;
418 
421  return dValue_ <= rhs.dValue_;
422 
424  return sValue_ <= rhs.sValue_;
425 
426  // case Any::Type::TYPE_INET_ADDR:
427  // return (addrValue_ < rhs. addrValue_) ||
428  // (addrValue_ == rhs. addrValue_);
429 
430  default:
431  break;
432  }
433  }
434 
435  throw EMANE::AnyException("Type mismatch");
436 }
437 
438 bool EMANE::Any::operator>=(const EMANE::Any & rhs) const
439 {
440  if(type_ == rhs.type_)
441  {
442  switch(type_)
443  {
448  return i64Value_ >=rhs.i64Value_;
449 
455  return u64Value_ >= rhs.u64Value_;
456 
459  return dValue_ >= rhs.dValue_;
460 
462  return sValue_ >= rhs.sValue_;
463 
464  // case Any::Type::TYPE_INET_ADDR:
465  // return (addrValue_ == rhs. addrValue_) ||
466  // !(addrValue_ < rhs. addrValue_);
467 
468  default:
469  break;
470  }
471  }
472 
473  throw EMANE::AnyException("Type mismatch");
474 }
475 
476 bool EMANE::Any::operator<(const EMANE::Any & rhs) const
477 {
478  if(type_ == rhs.type_)
479  {
480  switch(type_)
481  {
486  return i64Value_ < rhs.i64Value_;
487 
493  return u64Value_ < rhs.u64Value_;
494 
497  return dValue_ < rhs.dValue_;
498 
500  return sValue_ < rhs.sValue_;
501 
502  // case Any::Type::TYPE_INET_ADDR:
503  // return addrValue_ < rhs. addrValue_;
504 
505  default:
506  break;
507  }
508  }
509 
510  throw EMANE::AnyException("Type mismatch");
511 }
512 
513 bool EMANE::Any::operator>(const EMANE::Any & rhs) const
514 {
515  if(type_ == rhs.type_)
516  {
517  switch(type_)
518  {
523  return i64Value_ > rhs.i64Value_;
524 
530  return u64Value_ > rhs.u64Value_;
531 
534  return dValue_ > rhs.dValue_;
535 
537  return sValue_ > rhs.sValue_;
538 
539  // case Any::Type::TYPE_INET_ADDR:
540  // return (addrValue_ != rhs. addrValue_) &&
541  // !(addrValue_ < rhs. addrValue_);
542 
543  default:
544  break;
545  }
546  }
547 
548  throw EMANE::AnyException("Type mismatch");
549 }
550 
551 std::string EMANE::anyTypeAsString(const Any::Type & type)
552 {
553  switch(type)
554  {
556  return "int64";
557 
559  return "int32";
560 
562  return "int16";
563 
565  return "int8";
566 
568  return "uint64";
569 
571  return "uint32";
572 
574  return "uint16";
575 
577  return "uint8";
578 
580  return "bool";
581 
583  return "float";
584 
586  return "double";
587 
589  return "inetaddr";
590 
592  return "string";
593 
594  default:
595  break;
596  }
597 
598  throw AnyException("Invalid type");
599 }
600 
601 namespace EMANE
602 {
603  template <>
605  {
606  return Any::Type::TYPE_UINT64;
607  }
608 
609  template <>
611  {
612  return Any::Type::TYPE_INT64;
613  }
614 
615  template <>
617  {
618  return Any::Type::TYPE_UINT32;
619  }
620 
621  template <>
623  {
624  return Any::Type::TYPE_INT32;
625  }
626 
627  template <>
629  {
630  return Any::Type::TYPE_UINT16;
631  }
632 
633  template <>
635  {
636  return Any::Type::TYPE_INT16;
637  }
638 
639  template <>
641  {
642  return Any::Type::TYPE_UINT8;
643  }
644 
645  template <>
647  {
648  return Any::Type::TYPE_INT8;
649  }
650 
651  template <>
653  {
654  return Any::Type::TYPE_BOOL;
655  }
656 
657  template <>
659  {
660  return Any::Type::TYPE_FLOAT;
661  }
662 
663  template <>
665  {
666  return Any::Type::TYPE_DOUBLE;
667  }
668 
669  template <>
671  {
672  return Any::Type::TYPE_STRING;
673  }
674 
675  template <>
677  {
678  return Any::Type::TYPE_STRING;
679  }
680 
681  template <>
683  {
685  }
686 }
INETAddr addrValue_
Definition: any.h:338
std::ostream & operator<<(std::ostream &out, const EMANE::Any &any)
Definition: any.cc:286
std::uint16_t asUINT16() const
Definition: any.cc:199
Any(std::int64_t i64Value)
Definition: any.cc:36
std::int64_t i64Value_
Definition: any.h:334
float asFloat() const
Definition: any.cc:229
std::uint8_t asUINT8() const
Definition: any.cc:219
std::uint64_t u64Value_
Definition: any.h:335
std::string sValue_
Definition: any.h:337
Parameter conversion exception class.
Type
Definition: any.h:52
std::string toString() const
Definition: any.cc:292
bool operator<=(const EMANE::Any &rhs) const
Definition: any.cc:400
bool operator>(const EMANE::Any &rhs) const
Definition: any.cc:513
std::int32_t asINT32() const
Definition: any.cc:169
INETAddr asINETAddr() const
Definition: any.cc:249
std::string anyTypeAsString(const Any::Type &type)
Definition: any.cc:551
double dValue_
Definition: any.h:336
const char * what() const
Definition: exception.h:62
Any & operator=(const Any &rhs)
Definition: any.cc:110
std::string asString() const
Definition: any.cc:271
~Any()
Definition: any.cc:136
std::string str(bool bWithPort=true) const
Definition: inetaddr.cc:409
std::uint64_t asUINT64() const
Definition: any.cc:159
double asDouble() const
Definition: any.cc:239
static Any::Type type()
std::uint32_t asUINT32() const
Definition: any.cc:179
std::int64_t asINT64() const
Definition: any.cc:149
bool operator>=(const EMANE::Any &rhs) const
Definition: any.cc:438
bool asBool() const
Definition: any.cc:260
static Any create(std::string sValue, Type type)
Definition: any.cc:343
std::int8_t asINT8() const
Definition: any.cc:209
AnyException is thrown when an exception occurs during creation or conversion of an Any...
Definition: anyexception.h:46
std::int16_t asINT16() const
Definition: any.cc:189
The Any class can contain an instance of one of any type in its support type set. ...
Definition: any.h:49
bool operator<(const EMANE::Any &rhs) const
Definition: any.cc:476
Parameter conversion class with range checks.
Type getType() const
Definition: any.cc:281
Definition: agent.h:43