diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ebde61..1120163 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,10 @@ add_library(SimCore STATIC include/SimCore/Position.hpp src/SimCore/Position.cpp + include/SimCore/SafeMap.hpp + src/SimCore/SafeMap.cpp + + ) target_link_libraries(SimCore @@ -89,5 +93,8 @@ IF (${TEST_SIMCORE_LIBRARY}) target_link_libraries(test_TrackClass Catch2::Catch2 SimCore eigen loguru) catch_discover_tests(test_TrackClass) + add_executable(test_SafeMap tests/test_SafeMap.cpp) + target_link_libraries(test_SafeMap Catch2::Catch2 SimCore loguru) + catch_discover_tests(test_SafeMap) ENDIF() diff --git a/include/SimCore/SafeMap.hpp b/include/SimCore/SafeMap.hpp new file mode 100644 index 0000000..fb71d56 --- /dev/null +++ b/include/SimCore/SafeMap.hpp @@ -0,0 +1,97 @@ +#pragma once +#include +#include +#include + + + +namespace SimCore { + + template + class SafeMap { + + private: + std::map store; + mutable std::mutex mx; + std::condition_variable c; + + protected: + + + public: + SafeMap() + { + store = std::map(); + } + + void addValue(K key, V value ) + { + std::lock_guard lock(mx); + store.emplace(key,value); + c.notify_one(); + + } + + void removePair(K key) + { + std::lock_guard lock(mx); + auto search = store.find(key); + if (search != store.end()) { + search = store.erase(search); + } + c.notify_one(); + + } + + void overRideValue(K key, V newValue) + { + std::lock_guard lock(mx); + auto search = store.find(key); + if (search != store.end()) { + store[key] = newValue; + } + c.notify_one(); + + + } + + + V getValue(K key) + { + std::lock_guard lock(mx); + V value; + auto search = store.find(key); + if (search != store.end()) { + value = store[key]; + } + return value; + + } + + bool hasKey(K key) + { + std::lock_guard lock(mx); + auto search = store.find(key); + if (search != store.end()) { + return true; + }else { + return false; + } + + } + + + long size() + { + + std::lock_guard lock(std::mutex); + + return store.size(); + } + + + + + }; +} + diff --git a/src/SimCore/SafeMap.cpp b/src/SimCore/SafeMap.cpp new file mode 100644 index 0000000..1a2ca98 --- /dev/null +++ b/src/SimCore/SafeMap.cpp @@ -0,0 +1,17 @@ +#include +#include +#include + + +namespace SimCore { + + + // template + // SafeMap< K, V>::SafeMap(){ + // store = std::map(); + + // } + + + +} \ No newline at end of file diff --git a/tests/test_SafeMap.cpp b/tests/test_SafeMap.cpp new file mode 100644 index 0000000..5ee2ba3 --- /dev/null +++ b/tests/test_SafeMap.cpp @@ -0,0 +1,41 @@ + +#include +#define CATCH_CONFIG_MAIN +#include +#include +#include + + + + +SCENARIO("Testing the SimCore Track") +{ + GIVEN("different Attributes for a Track in different forms") + { + + int i = 0; + WHEN("constructing Track Object with data") + { + auto store = SimCore::SafeMap(); + + store.addValue("h1", 1); + store.addValue("h2", 2); + LOG_S(INFO)<<"store size = " << store.size(); + + store.removePair("h2"); + THEN("check if Track attributes are correct") + { + REQUIRE(store.size() == 1); + REQUIRE(store.getValue("h1") == 1); + + store.overRideValue("h1", 3); + REQUIRE(store.size() == 1); + REQUIRE(store.getValue("h1") == 3); + + + + + } //THEN + } // WHEN + } // GIVEN +} //SCENARIO \ No newline at end of file