EMANE  1.2.1
anyutils.h
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 #ifndef EMANECONTROLPORTANYUTILS_HEADER_
34 #define EMANECONTROLPORTANYUTILS_HEADER_
35 
36 #include "remotecontrolportapi.pb.h"
37 #include "emane/any.h"
38 
39 namespace EMANE
40 {
41  namespace ControlPort
42  {
43  inline
44  Any::Type toAnyType(EMANERemoteControlPortAPI::Any::AnyType type)
45  {
46  switch(type)
47  {
48  case EMANERemoteControlPortAPI::Any::TYPE_ANY_INT8:
49  return Any::Type::TYPE_INT8;
50 
51  case EMANERemoteControlPortAPI::Any::TYPE_ANY_UINT8:
52  return Any::Type::TYPE_UINT8;
53 
54  case EMANERemoteControlPortAPI::Any::TYPE_ANY_INT16:
55  return Any::Type::TYPE_INT16;
56 
57  case EMANERemoteControlPortAPI::Any::TYPE_ANY_UINT16:
59 
60  case EMANERemoteControlPortAPI::Any::TYPE_ANY_INT32:
61  return Any::Type::TYPE_INT32;
62 
63  case EMANERemoteControlPortAPI::Any::TYPE_ANY_UINT32:
65 
66  case EMANERemoteControlPortAPI::Any::TYPE_ANY_INT64:
67  return Any::Type::TYPE_INT64;
68 
69  case EMANERemoteControlPortAPI::Any::TYPE_ANY_UINT64:
71 
72  case EMANERemoteControlPortAPI::Any::TYPE_ANY_FLOAT:
73  return Any::Type::TYPE_FLOAT;
74 
75  case EMANERemoteControlPortAPI::Any::TYPE_ANY_DOUBLE:
77 
78  case EMANERemoteControlPortAPI::Any::TYPE_ANY_STRING:
80 
81  case EMANERemoteControlPortAPI::Any::TYPE_ANY_BOOLEAN:
82  return Any::Type::TYPE_BOOL;
83 
84  case EMANERemoteControlPortAPI::Any::TYPE_ANY_INETADDR:
86  }
87 
88  throw AnyException("unknown type");
89  }
90 
91  inline
93  {
94  switch(any.type())
95  {
96  case EMANERemoteControlPortAPI::Any::TYPE_ANY_INT8:
97  return Any{static_cast<std::int8_t>(any.i32value())};
98 
99  case EMANERemoteControlPortAPI::Any::TYPE_ANY_UINT8:
100  return Any{static_cast<std::uint8_t>(any.u32value())};
101 
102  case EMANERemoteControlPortAPI::Any::TYPE_ANY_INT16:
103  return Any{static_cast<std::int16_t>(any.i32value())};
104 
105  case EMANERemoteControlPortAPI::Any::TYPE_ANY_UINT16:
106  return Any{static_cast<std::uint16_t>(any.u32value())};
107 
108  case EMANERemoteControlPortAPI::Any::TYPE_ANY_INT32:
109  return Any{static_cast<std::int32_t>(any.i32value())};
110 
111  case EMANERemoteControlPortAPI::Any::TYPE_ANY_UINT32:
112  return Any{static_cast<std::uint32_t>(any.u32value())};
113 
114  case EMANERemoteControlPortAPI::Any::TYPE_ANY_INT64:
115  return Any{any.i64value()};
116 
117  case EMANERemoteControlPortAPI::Any::TYPE_ANY_UINT64:
118  return Any{any.u64value()};
119 
120  case EMANERemoteControlPortAPI::Any::TYPE_ANY_FLOAT:
121  return Any{any.fvalue()};
122 
123  case EMANERemoteControlPortAPI::Any::TYPE_ANY_DOUBLE:
124  return Any{any.dvalue()};
125 
126  case EMANERemoteControlPortAPI::Any::TYPE_ANY_STRING:
127  return Any{any.svalue()};
128 
129  case EMANERemoteControlPortAPI::Any::TYPE_ANY_BOOLEAN:
130  return EMANE::Any{any.bvalue()};
131 
132  case EMANERemoteControlPortAPI::Any::TYPE_ANY_INETADDR:
133  return EMANE::Any{INETAddr(any.svalue().c_str())};
134  }
135 
136  throw AnyException("unknown type");
137  }
138 
139  inline
141  const EMANE::Any & any)
142  {
143  switch(any.getType())
144  {
146  pAny->set_type(EMANERemoteControlPortAPI::Any::TYPE_ANY_INT64);
147  pAny->set_i64value(any.asINT64());
148  break;
149 
151  pAny->set_type(EMANERemoteControlPortAPI::Any::TYPE_ANY_UINT64);
152  pAny->set_u64value(any.asUINT64());
153  break;
154 
156  pAny->set_type(EMANERemoteControlPortAPI::Any::TYPE_ANY_INT32);
157  pAny->set_i32value(any.asINT32());
158  break;
159 
161  pAny->set_type(EMANERemoteControlPortAPI::Any::TYPE_ANY_UINT32);
162  pAny->set_u32value(any.asUINT32());
163  break;
164 
166  pAny->set_type(EMANERemoteControlPortAPI::Any::TYPE_ANY_INT16);
167  pAny->set_i32value(any.asINT16());
168  break;
169 
171  pAny->set_type(EMANERemoteControlPortAPI::Any::TYPE_ANY_UINT16);
172  pAny->set_u32value(any.asUINT16());
173  break;
174 
176  pAny->set_type(EMANERemoteControlPortAPI::Any::TYPE_ANY_INT8);
177  pAny->set_i32value(any.asINT8());
178  break;
179 
181  pAny->set_type(EMANERemoteControlPortAPI::Any::TYPE_ANY_UINT8);
182  pAny->set_u32value(any.asUINT8());
183  break;
184 
186  pAny->set_type(EMANERemoteControlPortAPI::Any::TYPE_ANY_FLOAT);
187  pAny->set_fvalue(any.asFloat());
188  break;
189 
191  pAny->set_type(EMANERemoteControlPortAPI::Any::TYPE_ANY_DOUBLE);
192  pAny->set_dvalue(any.asDouble());
193  break;
194 
196  pAny->set_type(EMANERemoteControlPortAPI::Any::TYPE_ANY_INETADDR);
197  // slight cheat - send INET Addrs using their string
198  // representation
199  pAny->set_svalue(any.asINETAddr().str());
200  break;
201 
203  pAny->set_type(EMANERemoteControlPortAPI::Any::TYPE_ANY_BOOLEAN);
204  pAny->set_bvalue(any.asBool());
205  break;
206 
208  pAny->set_type(EMANERemoteControlPortAPI::Any::TYPE_ANY_STRING);
209  pAny->set_svalue(any.asString());
210  break;
211 
212  default:
213  throw AnyException("unknown type");
214  }
215  }
216  }
217 }
218 
219 
220 
221 #endif // EMANECONTROLPORTANYUTILS_HEADER_
std::uint16_t asUINT16() const
Definition: any.cc:199
float asFloat() const
Definition: any.cc:229
std::uint8_t asUINT8() const
Definition: any.cc:219
Type
Definition: any.h:52
Any toAny(EMANERemoteControlPortAPI::Any any)
Definition: anyutils.h:92
std::int32_t asINT32() const
Definition: any.cc:169
INETAddr asINETAddr() const
Definition: any.cc:249
std::string asString() const
Definition: any.cc:271
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
Any::Type toAnyType(EMANERemoteControlPortAPI::Any::AnyType type)
Definition: anyutils.h:44
std::uint32_t asUINT32() const
Definition: any.cc:179
void convertToAny(EMANERemoteControlPortAPI::Any *pAny, const EMANE::Any &any)
Definition: anyutils.h:140
std::int64_t asINT64() const
Definition: any.cc:149
bool asBool() const
Definition: any.cc:260
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
Type getType() const
Definition: any.cc:281
Definition: agent.h:43