Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
Actor Framework
Commits
e95b0674
Commit
e95b0674
authored
Aug 17, 2018
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement new map type for custom runtime settings
parent
27959b35
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
195 additions
and
0 deletions
+195
-0
libcaf_core/CMakeLists.txt
libcaf_core/CMakeLists.txt
+1
-0
libcaf_core/caf/runtime_settings_map.hpp
libcaf_core/caf/runtime_settings_map.hpp
+75
-0
libcaf_core/src/runtime_settings_map.cpp
libcaf_core/src/runtime_settings_map.cpp
+64
-0
libcaf_core/test/runtime_settings_map.cpp
libcaf_core/test/runtime_settings_map.cpp
+55
-0
No files found.
libcaf_core/CMakeLists.txt
View file @
e95b0674
...
...
@@ -95,6 +95,7 @@ set(LIBCAF_CORE_SRCS
src/response_promise.cpp
src/resumable.cpp
src/ripemd_160.cpp
src/runtime_settings_map.cpp
src/scheduled_actor.cpp
src/scoped_actor.cpp
src/scoped_execution_unit.cpp
...
...
libcaf_core/caf/runtime_settings_map.hpp
0 → 100644
View file @
e95b0674
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| 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
libcaf_core/src/runtime_settings_map.cpp
0 → 100644
View file @
e95b0674
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| 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
libcaf_core/test/runtime_settings_map.cpp
0 → 100644
View file @
e95b0674
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| 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
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment