Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add state_representation::Parameters to clproto #180

Merged
merged 12 commits into from
Aug 25, 2021
6 changes: 3 additions & 3 deletions protocol/clproto_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ else()
endif()

set(PROTOBUF_DIR ${PROJECT_SOURCE_DIR}/../protobuf)
set(PROTOBUF_BINDINGDS_DIR ${PROTOBUF_DIR}/bindings/cpp)
set(PROTOBUF_BINDINGS_DIR ${PROTOBUF_DIR}/bindings/cpp)

add_custom_target(generate_proto_bindings COMMAND make cpp_bindings
WORKING_DIRECTORY ${PROTOBUF_DIR}
)

file(GLOB_RECURSE GENERATED_PROTO_BINDINGS "${PROTOBUF_BINDINGDS_DIR}/*.pb.cc" "${PROTOBUF_BINDINGDS_DIR}/*.pb.h")
file(GLOB_RECURSE GENERATED_PROTO_BINDINGS "${PROTOBUF_BINDINGS_DIR}/*.pb.cc" "${PROTOBUF_BINDINGS_DIR}/*.pb.h")

add_library(${PROJECT_NAME}_bindings STATIC ${GENERATED_PROTO_BINDINGS})
target_include_directories(${PROJECT_NAME}_bindings PUBLIC ${PROTOBUF_BINDINGDS_DIR})
target_include_directories(${PROJECT_NAME}_bindings PUBLIC ${PROTOBUF_BINDINGS_DIR})
set_property(TARGET ${PROJECT_NAME}_bindings PROPERTY POSITION_INDEPENDENT_CODE ON)

add_library(${PROJECT_NAME} SHARED
Expand Down
53 changes: 50 additions & 3 deletions protocol/clproto_cpp/include/clproto.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@

namespace clproto {

/**
* @class DecodingException
* @brief A DedocdingException is raised whenever a
* decoding operation fails due to invalid encoding.
*/
class DecodingException : public std::runtime_error {
public:
explicit DecodingException(const std::string& msg);
};

/**
* @enum MessageType
* @brief The MessageType enumeration contains the possible
* message types in the clproto.
* @details The values and order of this enumeration
* are synchronized with the fields of the protobuf
* StateMessage type, allowing a one-to-one mapping
* to the StateMessage type case.
*/
enum MessageType {
UNKNOWN_MESSAGE = 0,
STATE_MESSAGE = 1,
Expand All @@ -27,6 +41,28 @@ enum MessageType {
JOINT_TORQUES_MESSAGE = 13,
SHAPE_MESSAGE = 14,
ELLIPSOID_MESSAGE = 15,
PARAMETER_MESSAGE = 16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No comma at the end?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not that is the last one of the enum

};

/**
* @enum ParameterMessageType
* @brief The ParameterMessageType enumeration contains the
* possible value types contained in a parameter message.
* @details The values and order of this enumeration
* are synchronized with the fields of the protobuf
* ParameterValue type, allowing a one-to-one mapping
* to the ParameterValue type case.
*/
enum ParameterMessageType {
UNKNOWN_PARAMETER = 0,
DOUBLE = 1,
DOUBLE_ARRAY = 2,
BOOL = 3,
BOOL_ARRAY = 4,
STRING = 5,
STRING_ARRAY = 6,
MATRIX = 7,
VECTOR = 8
};

/**
Expand All @@ -45,14 +81,23 @@ bool is_valid(const std::string& msg);
*/
MessageType check_message_type(const std::string& msg);

/**
* @brief Check which control libraries parameter type a
* serialized binary string can be decoded as, if at all.
* @param msg The serialized binary string to check
* @return The ParameterMessageType of the contained type or UNKNOWN
*/
ParameterMessageType check_parameter_message_type(const std::string& msg);

/**
* @brief Encode a control libraries object into
* a serialized binary string representation (wire format).
* @tparam T The provided control libraries object type
* @param obj The control libraries object to encode
* @return The serialized binary string encoding
*/
template<typename T> std::string encode(const T& obj);
template<typename T>
std::string encode(const T& obj);

/**
* @brief Decode a serialized binary string from
Expand All @@ -63,7 +108,8 @@ template<typename T> std::string encode(const T& obj);
* @param msg The serialized binary string to decode
* @return A new instance of the control libraries object
*/
template<typename T> T decode(const std::string& msg);
template<typename T>
T decode(const std::string& msg);

/**
* @brief Exception safe decoding of a serialized binary string
Expand All @@ -75,6 +121,7 @@ template<typename T> T decode(const std::string& msg);
* @param obj A reference to a control libraries object
* @return A success status boolean
*/
template<typename T> bool decode(const std::string& msg, T& obj);
template<typename T>
bool decode(const std::string& msg, T& obj);

}
44 changes: 43 additions & 1 deletion protocol/clproto_cpp/include/clproto/decoders.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#include <google/protobuf/repeated_field.h>
#include <state_representation/parameters/Parameter.hpp>
#include <state_representation/parameters/parameter.pb.h>

#include "clproto.h"

namespace clproto {
Expand All @@ -10,7 +14,7 @@ class DecoderNotImplementedException : public DecodingException {
};

/**
* @brief Decoding helper method
* @brief Decoding helper method.
* @tparam ObjT The control libraries output type
* @tparam MsgT The protocol message input type
* @param message The protocol message object
Expand All @@ -19,9 +23,47 @@ class DecoderNotImplementedException : public DecodingException {
template<typename ObjT, typename MsgT>
ObjT decoder(const MsgT& message);

/**
* @brief Decoding helper method for a RepeatedField message
* into vector data.
* @tparam FieldT The datatype within the repeated field
* @param message A RepeatedField message
* @return The decoded vector of data
*/
template<typename FieldT>
std::vector<FieldT> decoder(const google::protobuf::RepeatedField<FieldT>& message);

/**
* @brief Decoding helper method for a RepeatedPtrField message
* into vector data.
* @tparam FieldT The datatype within the repeated field
* @param message A RepeatedPtrField message
* @return The decoded vector of data
*/
template<typename FieldT>
std::vector<FieldT> decoder(const google::protobuf::RepeatedPtrField<FieldT>& message);

/**
* @brief Decoding helper method for the Parameter type.
* @tparam ParamT The type contained within the Parameter object
* @param message The protocol Parameter message object
* @return The decoded control libraries Parameter object
*/
template<typename ParamT>
state_representation::Parameter<ParamT> decoder(const state_representation::proto::Parameter& message);

template<typename ObjT, typename MsgT>
ObjT decoder(const MsgT&) {
throw DecoderNotImplementedException("Templated decoder function not implemented!");
}

template<typename FieldT>
std::vector<FieldT> decoder(const google::protobuf::RepeatedField<FieldT>& message) {
return {message.begin(), message.end()};
}

template<typename FieldT>
std::vector<FieldT> decoder(const google::protobuf::RepeatedPtrField<FieldT>& message) {
return {message.begin(), message.end()};
}
}
34 changes: 26 additions & 8 deletions protocol/clproto_cpp/include/clproto/encoders.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <google/protobuf/repeated_field.h>
#include <state_representation/parameters/Parameter.hpp>
#include <state_representation/parameters/parameter.pb.h>

namespace clproto {

Expand All @@ -10,7 +12,7 @@ class EncoderNotImplementedException : public std::runtime_error {
};

/**
* @brief Encoding helper method
* @brief Encoding helper method.
* @tparam MsgT The protocol message output type
* @tparam ObjT The control libraries input type
* @param object The control libraries object
Expand All @@ -20,24 +22,40 @@ template<typename MsgT, typename ObjT>
MsgT encoder(const ObjT& object);

/**
* @brief Encoding helper method for C-style arrays into
* a RepeatedField message type
* @brief Encoding helper method for the Parameter type.
* @tparam ParamT The type contained within the Parameter object
* @param parameter The control libraries Parameter object
* @return The encoded protocol Parameter message object
*/
template<typename ParamT>
state_representation::proto::Parameter encoder(const state_representation::Parameter<ParamT>& parameter);

/**
* @brief Encoding helper method for vector data into
* a RepeatedField message type.
* @tparam FieldT The datatype within the repeated field
* @param data A C-style array of data
* @param size The length of the data array
* @param data A vector of data
* @return The encoded RepeatedField protocol message object
*/
template<typename FieldT>
google::protobuf::RepeatedField<FieldT> encoder(const FieldT* data, std::size_t size);
google::protobuf::RepeatedField<FieldT> encoder(const std::vector<FieldT>& data);

/**
* @brief Encoding helper method for Eigen data into
* a RepeatedField message type.
* @param matrix An Eigen matrix of data
* @return The encoded RepeatedField protocol message object
*/
google::protobuf::RepeatedField<double> encoder(const Eigen::MatrixXd& matrix);

template<typename MsgT, typename ObjT>
MsgT encoder(const ObjT&) {
throw EncoderNotImplementedException("Templated encoder function not implemented!");
}

template<typename FieldT>
google::protobuf::RepeatedField<FieldT> encoder(const FieldT*, std::size_t) {
throw EncoderNotImplementedException("Templated encoder function not implemented!");
google::protobuf::RepeatedField<FieldT> encoder(const std::vector<FieldT>& data) {
return google::protobuf::RepeatedField<FieldT>({data.begin(), data.end()});
}

}
Loading