Fawkes API  Fawkes Development Version
KeyValueInterface.cpp
1 
2 /***************************************************************************
3  * KeyValueInterface.cpp - Fawkes BlackBoard Interface - KeyValueInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2015 Gesche Gierse
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <interfaces/KeyValueInterface.h>
25 
26 #include <core/exceptions/software.h>
27 
28 #include <map>
29 #include <string>
30 #include <cstring>
31 #include <cstdlib>
32 
33 namespace fawkes {
34 
35 /** @class KeyValueInterface <interfaces/KeyValueInterface.h>
36  * KeyValueInterface Fawkes BlackBoard Interface.
37  * Key-Value interface. Use this to publish Key-Value based information, if you do not want to create a new interface type for the data. This interface can be used for different kind of data, but should only contain one value at a time. Set the value_type field to represent which kind of value should be transported (e.g. TYPE_INT for integer) and fill the data in the correct value field (e.g. value_int).
38  * @ingroup FawkesInterfaces
39  */
40 
41 
42 
43 /** Constructor */
44 KeyValueInterface::KeyValueInterface() : Interface()
45 {
46  data_size = sizeof(KeyValueInterface_data_t);
47  data_ptr = malloc(data_size);
48  data = (KeyValueInterface_data_t *)data_ptr;
49  data_ts = (interface_data_ts_t *)data_ptr;
50  memset(data_ptr, 0, data_size);
51  enum_map_ValueType[(int)TypeStr] = "TypeStr";
52  enum_map_ValueType[(int)TypeInt] = "TypeInt";
53  enum_map_ValueType[(int)TypeUint] = "TypeUint";
54  enum_map_ValueType[(int)TypeBool] = "TypeBool";
55  enum_map_ValueType[(int)TypeByte] = "TypeByte";
56  enum_map_ValueType[(int)TypeFloat] = "TypeFloat";
57  add_fieldinfo(IFT_STRING, "key", 32, data->key);
58  add_fieldinfo(IFT_ENUM, "value_type", 1, &data->value_type, "ValueType", &enum_map_ValueType);
59  add_fieldinfo(IFT_STRING, "value_string", 32, data->value_string);
60  add_fieldinfo(IFT_UINT32, "value_uint", 1, &data->value_uint);
61  add_fieldinfo(IFT_INT32, "value_int", 1, &data->value_int);
62  add_fieldinfo(IFT_BOOL, "value_bool", 1, &data->value_bool);
63  add_fieldinfo(IFT_BYTE, "value_byte", 1, &data->value_byte);
64  add_fieldinfo(IFT_FLOAT, "value_float", 1, &data->value_float);
65  unsigned char tmp_hash[] = {0xf1, 0x89, 0x81, 0x4f, 0xb9, 0x6e, 0x5c, 0xc8, 0x78, 0x90, 0x1a, 0x10, 0xdb, 0xa9, 0xa0, 0x52};
66  set_hash(tmp_hash);
67 }
68 
69 /** Destructor */
70 KeyValueInterface::~KeyValueInterface()
71 {
72  free(data_ptr);
73 }
74 /** Convert ValueType constant to string.
75  * @param value value to convert to string
76  * @return constant value as string.
77  */
78 const char *
80 {
81  switch (value) {
82  case TypeStr: return "TypeStr";
83  case TypeInt: return "TypeInt";
84  case TypeUint: return "TypeUint";
85  case TypeBool: return "TypeBool";
86  case TypeByte: return "TypeByte";
87  case TypeFloat: return "TypeFloat";
88  default: return "UNKNOWN";
89  }
90 }
91 /* Methods */
92 /** Get key value.
93  * The key entry
94  * @return key value
95  */
96 char *
98 {
99  return data->key;
100 }
101 
102 /** Get maximum length of key value.
103  * @return length of key value, can be length of the array or number of
104  * maximum number of characters for a string
105  */
106 size_t
108 {
109  return 32;
110 }
111 
112 /** Set key value.
113  * The key entry
114  * @param new_key new key value
115  */
116 void
117 KeyValueInterface::set_key(const char * new_key)
118 {
119  strncpy(data->key, new_key, sizeof(data->key));
120  data_changed = true;
121 }
122 
123 /** Get value_type value.
124  * The type of the value entry.
125  * @return value_type value
126  */
129 {
130  return (KeyValueInterface::ValueType)data->value_type;
131 }
132 
133 /** Get maximum length of value_type value.
134  * @return length of value_type value, can be length of the array or number of
135  * maximum number of characters for a string
136  */
137 size_t
139 {
140  return 1;
141 }
142 
143 /** Set value_type value.
144  * The type of the value entry.
145  * @param new_value_type new value_type value
146  */
147 void
149 {
150  data->value_type = new_value_type;
151  data_changed = true;
152 }
153 
154 /** Get value_string value.
155  * Value with type string
156  * @return value_string value
157  */
158 char *
160 {
161  return data->value_string;
162 }
163 
164 /** Get maximum length of value_string value.
165  * @return length of value_string value, can be length of the array or number of
166  * maximum number of characters for a string
167  */
168 size_t
170 {
171  return 32;
172 }
173 
174 /** Set value_string value.
175  * Value with type string
176  * @param new_value_string new value_string value
177  */
178 void
179 KeyValueInterface::set_value_string(const char * new_value_string)
180 {
181  strncpy(data->value_string, new_value_string, sizeof(data->value_string));
182  data_changed = true;
183 }
184 
185 /** Get value_uint value.
186  * Value with type uint32
187  * @return value_uint value
188  */
189 uint32_t
191 {
192  return data->value_uint;
193 }
194 
195 /** Get maximum length of value_uint value.
196  * @return length of value_uint value, can be length of the array or number of
197  * maximum number of characters for a string
198  */
199 size_t
201 {
202  return 1;
203 }
204 
205 /** Set value_uint value.
206  * Value with type uint32
207  * @param new_value_uint new value_uint value
208  */
209 void
210 KeyValueInterface::set_value_uint(const uint32_t new_value_uint)
211 {
212  data->value_uint = new_value_uint;
213  data_changed = true;
214 }
215 
216 /** Get value_int value.
217  * Value with type integer
218  * @return value_int value
219  */
220 int32_t
222 {
223  return data->value_int;
224 }
225 
226 /** Get maximum length of value_int value.
227  * @return length of value_int value, can be length of the array or number of
228  * maximum number of characters for a string
229  */
230 size_t
232 {
233  return 1;
234 }
235 
236 /** Set value_int value.
237  * Value with type integer
238  * @param new_value_int new value_int value
239  */
240 void
241 KeyValueInterface::set_value_int(const int32_t new_value_int)
242 {
243  data->value_int = new_value_int;
244  data_changed = true;
245 }
246 
247 /** Get value_bool value.
248  * Value with type Bool
249  * @return value_bool value
250  */
251 bool
253 {
254  return data->value_bool;
255 }
256 
257 /** Get maximum length of value_bool value.
258  * @return length of value_bool value, can be length of the array or number of
259  * maximum number of characters for a string
260  */
261 size_t
263 {
264  return 1;
265 }
266 
267 /** Set value_bool value.
268  * Value with type Bool
269  * @param new_value_bool new value_bool value
270  */
271 void
272 KeyValueInterface::set_value_bool(const bool new_value_bool)
273 {
274  data->value_bool = new_value_bool;
275  data_changed = true;
276 }
277 
278 /** Get value_byte value.
279  * Value with type byte
280  * @return value_byte value
281  */
282 uint8_t
284 {
285  return data->value_byte;
286 }
287 
288 /** Get maximum length of value_byte value.
289  * @return length of value_byte value, can be length of the array or number of
290  * maximum number of characters for a string
291  */
292 size_t
294 {
295  return 1;
296 }
297 
298 /** Set value_byte value.
299  * Value with type byte
300  * @param new_value_byte new value_byte value
301  */
302 void
303 KeyValueInterface::set_value_byte(const uint8_t new_value_byte)
304 {
305  data->value_byte = new_value_byte;
306  data_changed = true;
307 }
308 
309 /** Get value_float value.
310  * Value with type float
311  * @return value_float value
312  */
313 float
315 {
316  return data->value_float;
317 }
318 
319 /** Get maximum length of value_float value.
320  * @return length of value_float value, can be length of the array or number of
321  * maximum number of characters for a string
322  */
323 size_t
325 {
326  return 1;
327 }
328 
329 /** Set value_float value.
330  * Value with type float
331  * @param new_value_float new value_float value
332  */
333 void
334 KeyValueInterface::set_value_float(const float new_value_float)
335 {
336  data->value_float = new_value_float;
337  data_changed = true;
338 }
339 
340 /* =========== message create =========== */
341 Message *
343 {
344  throw UnknownTypeException("The given type '%s' does not match any known "
345  "message type for this interface type.", type);
346 }
347 
348 
349 /** Copy values from other interface.
350  * @param other other interface to copy values from
351  */
352 void
354 {
355  const KeyValueInterface *oi = dynamic_cast<const KeyValueInterface *>(other);
356  if (oi == NULL) {
357  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
358  type(), other->type());
359  }
360  memcpy(data, oi->data, sizeof(KeyValueInterface_data_t));
361 }
362 
363 const char *
364 KeyValueInterface::enum_tostring(const char *enumtype, int val) const
365 {
366  if (strcmp(enumtype, "ValueType") == 0) {
367  return tostring_ValueType((ValueType)val);
368  }
369  throw UnknownTypeException("Unknown enum type %s", enumtype);
370 }
371 
372 /* =========== messages =========== */
373 /** Check if message is valid and can be enqueued.
374  * @param message Message to check
375  * @return true if the message is valid, false otherwise.
376  */
377 bool
379 {
380  return false;
381 }
382 
383 /// @cond INTERNALS
384 EXPORT_INTERFACE(KeyValueInterface)
385 /// @endcond
386 
387 
388 } // end namespace fawkes
uint32_t value_uint() const
Get value_uint value.
size_t maxlenof_value_string() const
Get maximum length of value_string value.
void set_value_bool(const bool new_value_bool)
Set value_bool value.
size_t maxlenof_value_byte() const
Get maximum length of value_byte value.
const char * tostring_ValueType(ValueType value) const
Convert ValueType constant to string.
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:124
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
ValueType
Indicator of current o.
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:314
size_t maxlenof_key() const
Get maximum length of key value.
void set_value_float(const float new_value_float)
Set value_float value.
int32_t value_int() const
Get value_int value.
Fawkes library namespace.
KeyValueInterface Fawkes BlackBoard Interface.
uint8_t value_byte() const
Get value_byte value.
string field
Definition: types.h:47
size_t maxlenof_value_type() const
Get maximum length of value_type value.
byte field, alias for uint8
Definition: types.h:48
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
void set_value_type(const ValueType new_value_type)
Set value_type value.
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:133
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:125
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
void set_value_uint(const uint32_t new_value_uint)
Set value_uint value.
bool data_changed
Indicator if data has changed.
Definition: interface.h:222
const char * type() const
Get type of interface.
Definition: interface.cpp:651
bool is_value_bool() const
Get value_bool value.
void set_key(const char *new_key)
Set key value.
void set_value_byte(const uint8_t new_value_byte)
Set value_byte value.
char * key() const
Get key value.
size_t maxlenof_value_float() const
Get maximum length of value_float value.
size_t maxlenof_value_int() const
Get maximum length of value_int value.
char * value_string() const
Get value_string value.
virtual Message * create_message(const char *type) const
Create message based on type name.
float field
Definition: types.h:45
float value_float() const
Get value_float value.
virtual void copy_values(const Interface *other)
Copy values from other interface.
32 bit integer field
Definition: types.h:41
ValueType value_type() const
Get value_type value.
size_t maxlenof_value_uint() const
Get maximum length of value_uint value.
size_t maxlenof_value_bool() const
Get maximum length of value_bool value.
void add_fieldinfo(interface_fieldtype_t type, const char *name, size_t length, void *value, const char *enumtype=0, const interface_enum_map_t *enum_map=0)
Add an entry to the info list.
Definition: message.cpp:436
boolean field
Definition: types.h:36
void set_value_string(const char *new_value_string)
Set value_string value.
const char * type() const
Get message type.
Definition: message.cpp:378
32 bit unsigned integer field
Definition: types.h:42
field with interface specific enum type
Definition: types.h:49
void set_value_int(const int32_t new_value_int)
Set value_int value.