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

Implement Database generic class #309

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
27 changes: 18 additions & 9 deletions ddsrouter_cmake/cmake/test/test_target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,24 @@ function(add_test_executable TEST_EXECUTABLE_NAME TEST_SOURCES TEST_NAME TEST_LI

get_win32_path_dependencies(${TEST_EXECUTABLE_NAME} TEST_FRIENDLY_PATH)

foreach(test_name ${TEST_LIST})
add_test(NAME ${TEST_NAME}.${test_name}
COMMAND ${TEST_EXECUTABLE_NAME}
--gtest_filter=${TEST_NAME}.${test_name}:**/${TEST_NAME}.${test_name}/**)

if(TEST_FRIENDLY_PATH)
set_tests_properties(${TEST_NAME}.${test_name} PROPERTIES ENVIRONMENT "PATH=${TEST_FRIENDLY_PATH}")
endif(TEST_FRIENDLY_PATH)
endforeach()
if( TEST_LIST )
# If list of tests is not empty, add each test separatly
foreach(test_name ${TEST_LIST})
add_test(NAME ${TEST_NAME}.${test_name}
COMMAND ${TEST_EXECUTABLE_NAME}
--gtest_filter=${TEST_NAME}**.${test_name}:**/${TEST_NAME}**.${test_name}/**)

if(TEST_FRIENDLY_PATH)
set_tests_properties(${TEST_NAME}.${test_name} PROPERTIES ENVIRONMENT "PATH=${TEST_FRIENDLY_PATH}")
endif(TEST_FRIENDLY_PATH)
endforeach()
else()
# If no tests are provided, create a single test
message(STATUS "Creating general test ${TEST_NAME}.")
add_test(NAME ${TEST_NAME}
COMMAND ${TEST_EXECUTABLE_NAME})
endif( TEST_LIST )


target_compile_definitions(${TEST_EXECUTABLE_NAME}
PRIVATE FASTDDS_ENFORCE_LOG_INFO
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.

/**
* @file DataBase.hpp
*/

#pragma once

namespace eprosima {
namespace ddsrouter {
namespace utils {

template <typename Key, typename Value>
class IDataBase
{
public:

virtual void add(const Key& key, const Value& value) = 0;

virtual void modify(const Key& key, const Value& value) = 0;

virtual void remove(const Key& key) = 0;

virtual bool exist(const Key& key) = 0;

virtual Value get(const Key& key) = 0;

};

} /* namespace utils */
} /* namespace ddsrouter */
} /* namespace eprosima */
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.

/**
* @file IWatchDataBase.hpp
*/

#pragma once

#include <functional>

#include <ddsrouter_utils/collection/database/IDataBase.hpp>

namespace eprosima {
namespace ddsrouter {
namespace utils {

template <typename Key, typename Value>
class IWatchDataBase : public IDataBase<Key, Value>
{
public:

virtual void register_addition_callback(
const std::function<void(Key, Value)>& callback) = 0;

virtual void register_modification_callback(
const std::function<void(Key, Value)>& callback) = 0;

virtual void register_remove_callback(
const std::function<void(Key, Value)>& callback) = 0;

};

} /* namespace utils */
} /* namespace ddsrouter */
} /* namespace eprosima */
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.

/**
* @file DataBase.hpp
*/

#pragma once

#include <map>

#include <ddsrouter_utils/collection/database/IWatchDataBase.hpp>
#include <ddsrouter_utils/thread/manager/IManager.hpp>
#include <ddsrouter_utils/thread/connector/SlotConnector.hpp>
#include <ddsrouter_utils/types/Atomicable.hpp>

namespace eprosima {
namespace ddsrouter {
namespace utils {

enum DataBaseActionKind
{
add = 0,
modify = 1,
remove = 2,
enum_size = 3,
};

template <typename Key, typename Value>
class StdWatchDataBase : public IWatchDataBase<Key, Value>
{

using CallbackType = std::function<void(Key, Value)>;

public:

StdWatchDataBase(
std::shared_ptr<thread::IManager> thread_manager);

virtual ~StdWatchDataBase() = default;

virtual void add(const Key& key, const Value& value) override;

virtual void modify(const Key& key, const Value& value) override;

virtual void remove(const Key& key) override;

virtual bool exist(const Key& key) override;

virtual Value get(const Key& key) override;

virtual void register_addition_callback(
const CallbackType& callback) override;

virtual void register_modification_callback(
const CallbackType& callback) override;

virtual void register_remove_callback(
const CallbackType& callback) override;

virtual void add(Key key, Value value, bool sync_insertion, bool sync_callback_call, bool sync_all_callbacks);

virtual void modify(Key key, Value value, bool sync_insertion, bool sync_callback_call, bool sync_all_callbacks);

virtual void remove(Key key, bool sync_insertion, bool sync_callback_call, bool sync_all_callbacks);

protected:

void sync_add_(const Key& key, const Value& value);
void sync_modify_(const Key& key, const Value& value);
void sync_remove_(const Key& key, const Value& value);

void sync_call_callbacks_common_(
const Key& key,
const Value& value,
DataBaseActionKind action_kind);

void call_callbacks_common_sync_(
const Key& key,
const Value& value,
DataBaseActionKind action_kind);

void register_callback_common_(
const CallbackType& callback,
DataBaseActionKind action_kind);

std::shared_ptr<thread::IManager> thread_manager_;

SharedAtomicable<std::map<Key, Value>> map_database_;

std::array<
SharedAtomicable<
std::vector<
thread::SlotConnector<Key, Value>>>,
DataBaseActionKind::enum_size> callbacks_;

thread::SlotConnector<Key, Value> add_slot_connector_;
thread::SlotConnector<Key, Value> modify_slot_connector_;
thread::SlotConnector<Key> remove_slot_connector_;
thread::SlotConnector<Key, Value, DataBaseActionKind> call_callbacks_slot_connector_;

};

} /* namespace utils */
} /* namespace ddsrouter */
} /* namespace eprosima */

// Include implementation template file
#include <ddsrouter_utils/collection/database/impl/StdWatchDataBase.ipp>
Loading