Commit e95b0674 authored by Dominik Charousset's avatar Dominik Charousset

Implement new map type for custom runtime settings

parent 27959b35
...@@ -95,6 +95,7 @@ set(LIBCAF_CORE_SRCS ...@@ -95,6 +95,7 @@ set(LIBCAF_CORE_SRCS
src/response_promise.cpp src/response_promise.cpp
src/resumable.cpp src/resumable.cpp
src/ripemd_160.cpp src/ripemd_160.cpp
src/runtime_settings_map.cpp
src/scheduled_actor.cpp src/scheduled_actor.cpp
src/scoped_actor.cpp src/scoped_actor.cpp
src/scoped_execution_unit.cpp src/scoped_execution_unit.cpp
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include <cstdint>
#include <unordered_map>
#include "caf/atom.hpp"
#include "caf/detail/shared_spinlock.hpp"
#include "caf/variant.hpp"
namespace caf {
/// Thread-safe container for mapping atoms to arbitrary settings.
class runtime_settings_map {
public:
// -- member types -----------------------------------------------------------
using mutex_type = detail::shared_spinlock;
using generic_pointer = void*;
using generic_function_pointer = void (*)();
using mapped_type = variant<none_t, int64_t, uint64_t, atom_value,
generic_pointer, generic_function_pointer>;
// -- thread-safe access -----------------------------------------------------
/// Returns the value mapped to `key`.
mapped_type get(atom_value key) const;
/// Returns the value mapped to `key` or `fallback` if no value is mapped to
/// this key.
mapped_type get_or(atom_value key, mapped_type fallback) const;
/// Maps `key` to `value` and returns the previous value.
void set(atom_value key, mapped_type value);
/// Removes `key` from the map.
void erase(atom_value key);
/// Returns the number of key-value entries.
size_t size() const;
/// Returns whether `size()` equals 0.
bool empty() const {
return size() == 0;
}
private:
// -- member variables -------------------------------------------------------
mutable mutex_type mtx_;
std::unordered_map<atom_value, mapped_type> map_;
};
} // namespace caf
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include "caf/runtime_settings_map.hpp"
#include "caf/locks.hpp"
namespace {
using mapped_type = caf::runtime_settings_map::mapped_type;
} // namespace <anonymous>
namespace caf {
mapped_type runtime_settings_map::get(atom_value key) const {
mapped_type fallback;
return get_or(key, fallback);
}
mapped_type runtime_settings_map::get_or(atom_value key,
mapped_type fallback) const {
shared_lock<mutex_type> guard{mtx_};
auto i = map_.find(key);
return i != map_.end() ? i->second : fallback;
}
void runtime_settings_map::set(atom_value key, mapped_type value) {
if (holds_alternative<none_t>(value)) {
erase(key);
return;
}
unique_lock<mutex_type> guard{mtx_};
auto res = map_.emplace(key, value);
if (!res.second)
res.first->second = value;
}
void runtime_settings_map::erase(atom_value key) {
unique_lock<mutex_type> guard{mtx_};
map_.erase(key);
}
size_t runtime_settings_map::size() const {
shared_lock<mutex_type> guard{mtx_};
return map_.size();
}
} // namespace caf
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#define CAF_SUITE runtime_settings_map
#include "caf/runtime_settings_map.hpp"
#include "caf/test/dsl.hpp"
using namespace caf;
namespace {
void my_fun() {
// nop
}
struct fixture {
runtime_settings_map rsm;
void (*funptr)() = my_fun;
};
} // namespace <anonymous>
CAF_TEST_FIXTURE_SCOPE(runtime_settings_map_tests, fixture)
CAF_TEST(get empty) {
CAF_CHECK(rsm.empty());
CAF_CHECK_EQUAL(rsm.get(atom("foo")), none);
rsm.set(atom("foo"), atom("bar"));
CAF_CHECK(!rsm.empty());
CAF_CHECK_EQUAL(rsm.get(atom("foo")), atom("bar"));
rsm.set(atom("foo"), funptr);
CAF_CHECK_EQUAL(rsm.get(atom("foo")), funptr);
rsm.set(atom("foo"), none);
CAF_CHECK_EQUAL(rsm.size(), 0u);
CAF_CHECK(rsm.empty());
}
CAF_TEST_FIXTURE_SCOPE_END()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment