Skip to content

Commit

Permalink
gologpp: move field_to_value into utils
Browse files Browse the repository at this point in the history
  • Loading branch information
morxa committed Dec 3, 2019
1 parent 84b9075 commit 3974541
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
31 changes: 4 additions & 27 deletions src/plugins/gologpp/exog_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "exog_manager.h"

#include "execution_thread.h"
#include "utils.h"

#include <core/exception.h>
#include <libs/interface/field_iterator.h>
Expand Down Expand Up @@ -54,8 +55,6 @@ const std::unordered_map<interface_fieldtype_t, std::string> ExogManager::iface_
* an interface ID or a pattern.
*/

static Value *field_to_value(InterfaceFieldIterator &fi, unsigned int idx);

/** @class ConfigError
* Thrown if the config is somehow inconsistent with the agent program.
*/
Expand Down Expand Up @@ -169,18 +168,18 @@ ExogManager::BlackboardEventHandler::BlackboardEventHandler(
auto it = iface_type_to_golog_type_.find(fi.get_type());
if (it == iface_type_to_golog_type_.end()) {
throw Exception("Unhandled interface field type %s", fi.get_typename());
}
}

string desired_type = it->second;
if (desired_type != StringType::name() && fi.get_length() > 1) {
desired_type = static_cast<string>(ListType(desired_type));
}
}

if (var_ref.type() != desired_type) {
throw ConfigError(target_exog_->name() + "'s argument " + var_ref.target()->name() + " is a "
+ var_ref.type_name() + ", but the interface field requires "
+ desired_type);
}
}
blackboard_->close(iface);

auto param_it =
Expand Down Expand Up @@ -300,27 +299,5 @@ ExogManager::PatternObserver::bb_interface_created(const char *type, const char
blackboard_, string(type) + "::" + id, target_exog_, exog_manager_));
}

static Value *
field_to_value(InterfaceFieldIterator &fi, unsigned int idx)
{
switch (fi.get_type()) {
case IFT_BOOL: return new Value(BoolType::name(), fi.get_bool(idx));
case IFT_BYTE: return new Value(NumberType::name(), fi.get_byte(idx));
case IFT_ENUM: return new Value(SymbolType::name(), fi.get_enum_string(idx));
case IFT_INT8: return new Value(NumberType::name(), fi.get_int8(idx));
case IFT_FLOAT: return new Value(NumberType::name(), fi.get_float(idx));
case IFT_INT16: return new Value(NumberType::name(), fi.get_int16(idx));
case IFT_INT32: return new Value(NumberType::name(), fi.get_int32(idx));
case IFT_INT64: return new Value(NumberType::name(), fi.get_int64(idx));
case IFT_UINT8: return new Value(NumberType::name(), fi.get_uint8(idx));
case IFT_DOUBLE: return new Value(NumberType::name(), fi.get_double(idx));
case IFT_STRING: return new Value(StringType::name(), fi.get_string());
case IFT_UINT16: return new Value(NumberType::name(), fi.get_uint16(idx));
case IFT_UINT32: return new Value(NumberType::name(), fi.get_uint32(idx));
case IFT_UINT64: return new Value(NumberType::name(), fi.get_uint64(idx));
}
throw Exception("Unhandled interface field type");
}

} // namespace gpp
} // namespace fawkes
24 changes: 24 additions & 0 deletions src/plugins/gologpp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <blackboard/blackboard.h>
#include <golog++/model/value.h>

using gologpp::Value;

namespace fawkes {
namespace gpp {

Expand Down Expand Up @@ -189,5 +191,27 @@ value_to_field(const gologpp::Value &value, InterfaceFieldIterator *field)
boost::apply_visitor(visitor, value.representation());
}

Value *
field_to_value(InterfaceFieldIterator &fi, unsigned int idx)
{
switch (fi.get_type()) {
case IFT_BOOL: return new Value(gologpp::BoolType::name(), fi.get_bool(idx));
case IFT_BYTE: return new Value(gologpp::NumberType::name(), fi.get_byte(idx));
case IFT_ENUM: return new Value(gologpp::SymbolType::name(), fi.get_enum_string(idx));
case IFT_INT8: return new Value(gologpp::NumberType::name(), fi.get_int8(idx));
case IFT_FLOAT: return new Value(gologpp::NumberType::name(), fi.get_float(idx));
case IFT_INT16: return new Value(gologpp::NumberType::name(), fi.get_int16(idx));
case IFT_INT32: return new Value(gologpp::NumberType::name(), fi.get_int32(idx));
case IFT_INT64: return new Value(gologpp::NumberType::name(), fi.get_int64(idx));
case IFT_UINT8: return new Value(gologpp::NumberType::name(), fi.get_uint8(idx));
case IFT_DOUBLE: return new Value(gologpp::NumberType::name(), fi.get_double(idx));
case IFT_STRING: return new Value(gologpp::StringType::name(), fi.get_string());
case IFT_UINT16: return new Value(gologpp::NumberType::name(), fi.get_uint16(idx));
case IFT_UINT32: return new Value(gologpp::NumberType::name(), fi.get_uint32(idx));
case IFT_UINT64: return new Value(gologpp::NumberType::name(), fi.get_uint64(idx));
}
throw Exception("Unhandled interface field type");
}

} // namespace gpp
} // namespace fawkes
3 changes: 2 additions & 1 deletion src/plugins/gologpp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class ValueToFieldVisitor : public boost::static_visitor<>
unsigned int index;
};

void value_to_field(const gologpp::Value &value, InterfaceFieldIterator *field);
void value_to_field(const gologpp::Value &value, InterfaceFieldIterator *field);
gologpp::Value *field_to_value(InterfaceFieldIterator &fi, unsigned int idx);

} // namespace gpp
} // namespace fawkes

0 comments on commit 3974541

Please sign in to comment.