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 parameters component #304

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ ign_find_package(SQLite3
# Configure the build
#============================================================================
ign_configure_build(QUIT_IF_BUILD_ERRORS
COMPONENTS log)
COMPONENTS log parameters)

#============================================================================
# ign command line support
Expand Down
2 changes: 2 additions & 0 deletions parameters/include/ignition/transport/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

ign_install_all_headers(COMPONENT parameters)
71 changes: 71 additions & 0 deletions parameters/include/ignition/transport/parameters/Client.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2022 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#ifndef IGNITION_TRANSPORT_PARAMETERS_CLIENT_HH_
#define IGNITION_TRANSPORT_PARAMETERS_CLIENT_HH_

#include <memory>
#include <string>

#include "google/protobuf/message.h"

#include "ignition/msgs/parameter_declarations.pb.h"

#include "ignition/transport/Node.hh"

namespace ignition
{
namespace transport
{
namespace parameters
{

// Inline bracket to help doxygen filtering.
inline namespace IGNITION_TRANSPORT_VERSION_NAMESPACE {

class ParametersClient
{
public:
ParametersClient(
std::string _serverNamespace = "",
unsigned int _timeoutMs = kDefaultTimeoutMs);

std::unique_ptr<google::protobuf::Message>
GetParameter(const std::string & _parameterName);

void
SetParameter(const std::string & _parameterName, const google::protobuf::Message & _msg);

void
DeclareParameter(const std::string & _parameterName, const google::protobuf::Message & _msg);

ignition::msgs::ParameterDeclarations
ListParameters();

private:
std::string serverNamespace;
ignition::transport::Node node;
unsigned int timeoutMs;

constexpr static inline unsigned int kDefaultTimeoutMs = 5000;
};
}
}
}
}

#endif
108 changes: 108 additions & 0 deletions parameters/include/ignition/transport/parameters/Registry.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (C) 2022 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#ifndef IGNITION_TRANSPORT_PARAMETERS_REGISTRY_HH_
#define IGNITION_TRANSPORT_PARAMETERS_REGISTRY_HH_

#include <functional>
#include <memory>
#include <string>

#include <google/protobuf/message.h>

#include "ignition/msgs/parameter_declarations.pb.h"

#include "ignition/transport/config.hh"
#include "ignition/transport/parameters/exceptions.hh"

namespace ignition
{
namespace transport
{
namespace parameters
{
// Inline bracket to help doxygen filtering.
inline namespace IGNITION_TRANSPORT_VERSION_NAMESPACE {

struct ParametersRegistryPrivate;

class ParametersRegistry
{
public: struct ParameterValue
{
std::unique_ptr<google::protobuf::Message> msg;
std::string protoType;
};

public: ParametersRegistry(std::string _parametersServicesNamespace);

public: ~ParametersRegistry();

public: ParametersRegistry(const ParametersRegistry &) = delete;
public: ParametersRegistry &
operator=(const ParametersRegistry &) = delete;
public: ParametersRegistry(ParametersRegistry &&) = default;
public: ParametersRegistry &
operator=(ParametersRegistry &&) = default;

public: void DeclareParameter(
const std::string & _parameterName,
std::unique_ptr<google::protobuf::Message> _initialValue);

public: ParameterValue GetParameter(
const std::string & _parameterName);

public: void SetParameter(
const std::string & _parameterName,
std::unique_ptr<google::protobuf::Message> _value);

public: void SetParameter(
const std::string & _parameterName,
google::protobuf::Message & _value);

public: ignition::msgs::ParameterDeclarations ListParameters();

public: template<typename ProtoMsgT>
ProtoMsgT GetParameter(const std::string & _parameterName)
{
ProtoMsgT ret;
this->WithParameter(
_parameterName,
[nameCStr = _parameterName.c_str() , &ret](google::protobuf::Message & _msg) {
if (_msg.GetDescriptor() != ret.GetDescriptor()) {
throw ParameterInvalidTypeException{
"ParametersRegistry::GetParameter",
nameCStr,
_msg.GetDescriptor()->name().c_str(),
ret.GetDescriptor()->name().c_str()};
}
ret.CopyFrom(_msg);
});
}

private: std::unique_ptr<ParametersRegistryPrivate> dataPtr;

private: void WithParameter(
const std::string & _parameterName,
std::function<void(google::protobuf::Message &)> fn);
};
}
}
}
}

#endif
70 changes: 70 additions & 0 deletions parameters/include/ignition/transport/parameters/exceptions.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2022 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#ifndef IGNITION_TRANSPORT_PARAMETERS_EXCEPTIONS_HH_
#define IGNITION_TRANSPORT_PARAMETERS_EXCEPTIONS_HH_

#include <stdexcept>
#include <string>

namespace ignition
{
namespace transport
{
namespace parameters
{

// Inline bracket to help doxygen filtering.
inline namespace IGNITION_TRANSPORT_VERSION_NAMESPACE {

class ParameterAlreadyDeclaredException : public std::runtime_error
{
public: ParameterAlreadyDeclaredException(
const char * _prefix , const char * _paramName)
: std::runtime_error(
std::string{_prefix} + ": parameter [" +
_paramName + "] is already declared")
{}
};

class ParameterInvalidTypeException : public std::invalid_argument
{
public: ParameterInvalidTypeException(
const char * _prefix ,const char * _paramName,
const char * _expectedParamType, const char * _providedParamType)
: std::invalid_argument(
std::string{_prefix} + ": parameter [" +
_paramName + "] is of type [" + _expectedParamType + "] but type [" +
_providedParamType + "] was provided")
{}
};

class ParameterNotDeclaredException : public std::runtime_error
{
public: ParameterNotDeclaredException(
const char * _prefix , const char * _paramName)
: std::runtime_error(
std::string{_prefix} + ": parameter [" +
_paramName + "] is not declared")
{}
};
}
}
}
}

#endif
20 changes: 20 additions & 0 deletions parameters/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

ign_get_libsources_and_unittests(sources gtest_sources)
list(APPEND sources cmd/ParamCommandAPI.cc)

ign_add_component(parameters SOURCES ${sources} GET_TARGET_NAME param_lib_target)

# Unit tests
# ign_build_tests(
# TYPE "UNIT"
# SOURCES ${gtest_sources}
# LIB_DEPS ${parameters_lib_target} ${EXTRA_TEST_LIB_DEPS}
# )

# target_include_directories(${parameters_lib_target}
# PUBLIC
# "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>")

if(NOT WIN32)
add_subdirectory(cmd)
endif()
Loading