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
33namespace 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 */
44KeyValueInterface::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 */
70KeyValueInterface::~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 */
78const char *
79KeyValueInterface::tostring_ValueType(ValueType value) const
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 */
96char *
97KeyValueInterface::key() const
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 */
106size_t
107KeyValueInterface::maxlenof_key() const
108{
109 return 32;
110}
111
112/** Set key value.
113 * The key entry
114 * @param new_key new key value
115 */
116void
117KeyValueInterface::set_key(const char * new_key)
118{
119 set_field(data->key, new_key);
120}
121
122/** Get value_type value.
123 * The type of the value entry.
124 * @return value_type value
125 */
127KeyValueInterface::value_type() const
128{
129 return (KeyValueInterface::ValueType)data->value_type;
130}
131
132/** Get maximum length of value_type value.
133 * @return length of value_type value, can be length of the array or number of
134 * maximum number of characters for a string
135 */
136size_t
137KeyValueInterface::maxlenof_value_type() const
138{
139 return 1;
140}
141
142/** Set value_type value.
143 * The type of the value entry.
144 * @param new_value_type new value_type value
145 */
146void
147KeyValueInterface::set_value_type(const ValueType new_value_type)
148{
149 set_field(data->value_type, new_value_type);
150}
151
152/** Get value_string value.
153 * Value with type string
154 * @return value_string value
155 */
156char *
157KeyValueInterface::value_string() const
158{
159 return data->value_string;
160}
161
162/** Get maximum length of value_string value.
163 * @return length of value_string value, can be length of the array or number of
164 * maximum number of characters for a string
165 */
166size_t
167KeyValueInterface::maxlenof_value_string() const
168{
169 return 32;
170}
171
172/** Set value_string value.
173 * Value with type string
174 * @param new_value_string new value_string value
175 */
176void
177KeyValueInterface::set_value_string(const char * new_value_string)
178{
179 set_field(data->value_string, new_value_string);
180}
181
182/** Get value_uint value.
183 * Value with type uint32
184 * @return value_uint value
185 */
186uint32_t
187KeyValueInterface::value_uint() const
188{
189 return data->value_uint;
190}
191
192/** Get maximum length of value_uint value.
193 * @return length of value_uint value, can be length of the array or number of
194 * maximum number of characters for a string
195 */
196size_t
197KeyValueInterface::maxlenof_value_uint() const
198{
199 return 1;
200}
201
202/** Set value_uint value.
203 * Value with type uint32
204 * @param new_value_uint new value_uint value
205 */
206void
207KeyValueInterface::set_value_uint(const uint32_t new_value_uint)
208{
209 set_field(data->value_uint, new_value_uint);
210}
211
212/** Get value_int value.
213 * Value with type integer
214 * @return value_int value
215 */
216int32_t
217KeyValueInterface::value_int() const
218{
219 return data->value_int;
220}
221
222/** Get maximum length of value_int value.
223 * @return length of value_int value, can be length of the array or number of
224 * maximum number of characters for a string
225 */
226size_t
227KeyValueInterface::maxlenof_value_int() const
228{
229 return 1;
230}
231
232/** Set value_int value.
233 * Value with type integer
234 * @param new_value_int new value_int value
235 */
236void
237KeyValueInterface::set_value_int(const int32_t new_value_int)
238{
239 set_field(data->value_int, new_value_int);
240}
241
242/** Get value_bool value.
243 * Value with type Bool
244 * @return value_bool value
245 */
246bool
247KeyValueInterface::is_value_bool() const
248{
249 return data->value_bool;
250}
251
252/** Get maximum length of value_bool value.
253 * @return length of value_bool value, can be length of the array or number of
254 * maximum number of characters for a string
255 */
256size_t
257KeyValueInterface::maxlenof_value_bool() const
258{
259 return 1;
260}
261
262/** Set value_bool value.
263 * Value with type Bool
264 * @param new_value_bool new value_bool value
265 */
266void
267KeyValueInterface::set_value_bool(const bool new_value_bool)
268{
269 set_field(data->value_bool, new_value_bool);
270}
271
272/** Get value_byte value.
273 * Value with type byte
274 * @return value_byte value
275 */
276uint8_t
277KeyValueInterface::value_byte() const
278{
279 return data->value_byte;
280}
281
282/** Get maximum length of value_byte value.
283 * @return length of value_byte value, can be length of the array or number of
284 * maximum number of characters for a string
285 */
286size_t
287KeyValueInterface::maxlenof_value_byte() const
288{
289 return 1;
290}
291
292/** Set value_byte value.
293 * Value with type byte
294 * @param new_value_byte new value_byte value
295 */
296void
297KeyValueInterface::set_value_byte(const uint8_t new_value_byte)
298{
299 set_field(data->value_byte, new_value_byte);
300}
301
302/** Get value_float value.
303 * Value with type float
304 * @return value_float value
305 */
306float
307KeyValueInterface::value_float() const
308{
309 return data->value_float;
310}
311
312/** Get maximum length of value_float value.
313 * @return length of value_float value, can be length of the array or number of
314 * maximum number of characters for a string
315 */
316size_t
317KeyValueInterface::maxlenof_value_float() const
318{
319 return 1;
320}
321
322/** Set value_float value.
323 * Value with type float
324 * @param new_value_float new value_float value
325 */
326void
327KeyValueInterface::set_value_float(const float new_value_float)
328{
329 set_field(data->value_float, new_value_float);
330}
331
332/* =========== message create =========== */
333Message *
334KeyValueInterface::create_message(const char *type) const
335{
336 throw UnknownTypeException("The given type '%s' does not match any known "
337 "message type for this interface type.", type);
338}
339
340
341/** Copy values from other interface.
342 * @param other other interface to copy values from
343 */
344void
345KeyValueInterface::copy_values(const Interface *other)
346{
347 const KeyValueInterface *oi = dynamic_cast<const KeyValueInterface *>(other);
348 if (oi == NULL) {
349 throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
350 type(), other->type());
351 }
352 memcpy(data, oi->data, sizeof(KeyValueInterface_data_t));
353}
354
355const char *
356KeyValueInterface::enum_tostring(const char *enumtype, int val) const
357{
358 if (strcmp(enumtype, "ValueType") == 0) {
359 return tostring_ValueType((ValueType)val);
360 }
361 throw UnknownTypeException("Unknown enum type %s", enumtype);
362}
363
364/* =========== messages =========== */
365/** Check if message is valid and can be enqueued.
366 * @param message Message to check
367 * @return true if the message is valid, false otherwise.
368 */
369bool
370KeyValueInterface::message_valid(const Message *message) const
371{
372 return false;
373}
374
375/// @cond INTERNALS
376EXPORT_INTERFACE(KeyValueInterface)
377/// @endcond
378
379
380} // end namespace fawkes
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
const char * type() const
Get type of interface.
Definition: interface.cpp:652
KeyValueInterface Fawkes BlackBoard Interface.
ValueType
Indicator of current o.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
Fawkes library namespace.