Fawkes API Fawkes Development Version
utils.cpp
1/***************************************************************************
2 * utils.cpp - Common utility functions used with Golog++
3 *
4 * Created: Wed 30 Oct 2019 14:31:18 CET 14:31
5 * Copyright 2019 Till Hofmann <hofmann@kbsg.rwth-aachen.de>
6 ****************************************************************************/
7
8/* This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Library General Public License for more details.
17 *
18 * Read the full text in the LICENSE.GPL file in the doc directory.
19 */
20
21#include "utils.h"
22
23#include <blackboard/blackboard.h>
24#include <golog++/model/value.h>
25
26using gologpp::Value;
27
28namespace fawkes {
29namespace gpp {
30
31/** @class ValueToFieldVisitor
32 * A visitor that converts a gologpp::Value to an interface field value.
33 * The visitor checks the types of the input gologpp::Value and the ouput
34 * InterfaceFieldIterator. If they match, the field is set. Otherwise, an
35 * exception is thrown.
36 */
37
38/** Constructor.
39 * @param field The field to set.
40 * @param index The index to set if field is an array.
41 */
43: field(field), index(index)
44{
45}
46
47/** Convert the given value and set the field accordingly.
48 * @param v The value to set the field to.
49 */
50void
52{
53 switch (field->get_type()) {
54 case IFT_INT32: field->set_int32(v, index); break;
55 case IFT_INT64: field->set_int64(v, index); break;
56 case IFT_UINT16: field->set_uint16(v, index); break;
57 case IFT_UINT32: field->set_uint32(v, index); break;
58 case IFT_UINT64: field->set_uint64(v, index); break;
59 default: throw Exception("Invalid cast from unsigned int to %s", field->get_typename());
60 }
61}
62
63/** Convert the given value and set the field accordingly.
64 * @param v The value to set the field to.
65 */
66void
68{
69 switch (field->get_type()) {
70 case IFT_INT32: field->set_int32(v, index); break;
71 case IFT_INT64: field->set_int64(v, index); break;
72 default: throw Exception("Invalid cast from int to %s", field->get_typename());
73 }
74}
75
76/** Convert the given value and set the field accordingly.
77 * @param v The value to set the field to.
78 */
79void
81{
82 switch (field->get_type()) {
83 case IFT_INT64: field->set_int64(v, index); break;
84 case IFT_UINT32: field->set_uint32(v, index); break;
85 case IFT_UINT64: field->set_uint64(v, index); break;
86 default: throw Exception("Invalid cast from unsigned long to %s", field->get_typename());
87 }
88}
89
90/** Convert the given value and set the field accordingly.
91 * @param v The value to set the field to.
92 */
93void
95{
96 switch (field->get_type()) {
97 case IFT_UINT32: field->set_uint32(v, index); break;
98 case IFT_INT64: field->set_int64(v, index); break;
99 default: throw Exception("Invalid cast from long to %s", field->get_typename());
100 }
101}
102
103/** Convert the given value and set the field accordingly.
104 * @param v The value to set the field to.
105 */
106void
108{
109 switch (field->get_type()) {
110 case IFT_FLOAT: field->set_float(v, index); break;
111 case IFT_DOUBLE: field->set_double(v, index); break;
112 default: throw Exception("Invalid cast from double to %s", field->get_typename());
113 }
114}
115
116/** Convert the given value and set the field accordingly.
117 * @param v The value to set the field to.
118 */
119void
121{
122 switch (field->get_type()) {
123 case IFT_STRING:
124 if (index != 0) {
125 throw Exception("Invalid cast, string arrays are not supported");
126 }
127 field->set_string(v.c_str());
128 break;
129 case IFT_ENUM: try { field->set_enum_string(v.c_str());
130 } catch (IllegalArgumentException &e) {
131 throw Exception("Cannot convert string '%s' into enum: %s", v.c_str(), e.what_no_backtrace());
132 }
133 break;
134 default: throw Exception("Invalid cast from string to %s", field->get_typename());
135 }
136}
137
138/** Convert the given value and set the field accordingly.
139 * @param v The value to set the field to.
140 */
141void
143{
144 switch (field->get_type()) {
145 case IFT_BOOL: field->set_bool(v, index);
146 default: throw Exception("Invalid cast from bool to %s", field->get_typename());
147 }
148}
149
150/** Golog++ does not support void* types. Thus, this always throws.
151 * @param v The value to set the field to.
152 */
153void
155{
156 switch (field->get_type()) {
157 default: throw Exception("Invalid cast from void* to %s", field->get_typename());
158 }
159}
160
161/** Not implemented yet.
162 * @param v The value to set the field to.
163 */
164void
165ValueToFieldVisitor::operator()(gologpp::CompoundType::Representation v)
166{
167 switch (field->get_type()) {
168 default: throw Exception("Invalid cast from compound to %s", field->get_typename());
169 }
170}
171
172/** Convert the given list by calling a visitor recursively for each item of the list.
173 * @param v The value to set the field to.
174 */
175void
176ValueToFieldVisitor::operator()(gologpp::ListType::Representation v)
177{
178 if (index != 0) {
179 throw Exception("Invalid cast, cannot convert a list with an offset or nested lists");
180 }
181 for (size_t i = 0; i < v.size(); i++) {
182 ValueToFieldVisitor visitor(field, i);
183 boost::apply_visitor(visitor, v[i]->representation());
184 }
185}
186
187void
188value_to_field(const gologpp::Value &value, InterfaceFieldIterator *field)
189{
190 ValueToFieldVisitor visitor(field);
191 boost::apply_visitor(visitor, value.representation());
192}
193
194Value *
195field_to_value(InterfaceFieldIterator &fi, unsigned int idx)
196{
197 using namespace gologpp;
198
199 switch (fi.get_type()) {
200 case IFT_BOOL: return new Value(get_type<BoolType>(), fi.get_bool(idx));
201 case IFT_BYTE: return new Value(get_type<NumberType>(), fi.get_byte(idx));
202 case IFT_ENUM: return new Value(get_type<SymbolType>(), fi.get_enum_string(idx));
203 case IFT_INT8: return new Value(get_type<NumberType>(), fi.get_int8(idx));
204 case IFT_FLOAT: return new Value(get_type<NumberType>(), fi.get_float(idx));
205 case IFT_INT16: return new Value(get_type<NumberType>(), fi.get_int16(idx));
206 case IFT_INT32: return new Value(get_type<NumberType>(), fi.get_int32(idx));
207 case IFT_INT64: return new Value(get_type<NumberType>(), fi.get_int64(idx));
208 case IFT_UINT8: return new Value(get_type<NumberType>(), fi.get_uint8(idx));
209 case IFT_DOUBLE: return new Value(get_type<NumberType>(), fi.get_double(idx));
210 case IFT_STRING: return new Value(get_type<StringType>(), fi.get_string());
211 case IFT_UINT16: return new Value(get_type<NumberType>(), fi.get_uint16(idx));
212 case IFT_UINT32: return new Value(get_type<NumberType>(), fi.get_uint32(idx));
213 case IFT_UINT64: return new Value(get_type<NumberType>(), fi.get_uint64(idx));
214 }
215 throw Exception("Unhandled interface field type");
216}
217
218} // namespace gpp
219} // namespace fawkes
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual const char * what_no_backtrace() const noexcept
Get primary string (does not implicitly print the back trace).
Definition: exception.cpp:663
Expected parameter is missing.
Definition: software.h:80
Interface field iterator.
const char * get_enum_string(unsigned int index=0) const
Get value of current enum field as string.
float get_float(unsigned int index=0) const
Get value of current field as float.
int16_t get_int16(unsigned int index=0) const
Get value of current field as integer.
void set_int64(int64_t i, unsigned int index=0)
Set value of current field as integer.
int8_t get_int8(unsigned int index=0) const
Get value of current field as integer.
int32_t get_int32(unsigned int index=0) const
Get value of current field as integer.
void set_string(const char *s)
Set value of current field as string.
int64_t get_int64(unsigned int index=0) const
Get value of current field as integer.
void set_double(double f, unsigned int index=0)
Set value of current field as double.
uint64_t get_uint64(unsigned int index=0) const
Get value of current field as unsigned integer.
uint16_t get_uint16(unsigned int index=0) const
Get value of current field as unsigned integer.
double get_double(unsigned int index=0) const
Get value of current field as double.
uint32_t get_uint32(unsigned int index=0) const
Get value of current field as unsigned integer.
void set_uint64(uint64_t i, unsigned int index=0)
Set value of current field as unsigned integer.
interface_fieldtype_t get_type() const
Get type of current field.
void set_float(float f, unsigned int index=0)
Set value of current field as float.
void set_enum_string(const char *e, unsigned int index=0)
Set value of current field as enum (from an integer).
void set_uint16(uint16_t i, unsigned int index=0)
Set value of current field as unsigned integer.
void set_int32(int32_t i, unsigned int index=0)
Set value of current field as integer.
const char * get_string() const
Get value of current field as string.
uint8_t get_byte(unsigned int index=0) const
Get value of current field as byte.
uint8_t get_uint8(unsigned int index=0) const
Get value of current field as unsigned integer.
bool get_bool(unsigned int index=0) const
Get value of current field as bool.
void set_bool(bool b, unsigned int index=0)
Set value of current field as bool.
void set_uint32(uint32_t i, unsigned int index=0)
Set value of current field as unsigned integer.
const char * get_typename() const
Get type of current field as string.
A visitor that converts a gologpp::Value to an interface field value.
Definition: utils.h:37
void operator()(unsigned int v)
Convert the given value and set the field accordingly.
Definition: utils.cpp:51
ValueToFieldVisitor(InterfaceFieldIterator *field, unsigned int index=0)
Constructor.
Definition: utils.cpp:42
Fawkes library namespace.
@ IFT_INT8
8 bit integer field
Definition: types.h:38
@ IFT_UINT32
32 bit unsigned integer field
Definition: types.h:43
@ IFT_FLOAT
float field
Definition: types.h:46
@ IFT_BYTE
byte field, alias for uint8
Definition: types.h:49
@ IFT_UINT64
64 bit unsigned integer field
Definition: types.h:45
@ IFT_UINT16
16 bit unsigned integer field
Definition: types.h:41
@ IFT_INT32
32 bit integer field
Definition: types.h:42
@ IFT_INT64
64 bit integer field
Definition: types.h:44
@ IFT_DOUBLE
double field
Definition: types.h:47
@ IFT_INT16
16 bit integer field
Definition: types.h:40
@ IFT_STRING
string field
Definition: types.h:48
@ IFT_BOOL
boolean field
Definition: types.h:37
@ IFT_ENUM
field with interface specific enum type
Definition: types.h:50
@ IFT_UINT8
8 bit unsigned integer field
Definition: types.h:39