-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_context.hpp
More file actions
44 lines (34 loc) · 1.52 KB
/
basic_context.hpp
File metadata and controls
44 lines (34 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#pragma once
#include <any>
#include <cinttypes>
#include <functional>
#include <memory>
namespace context {
/// The main interface to context. This class can be customized via ContextImpl
/// to provide alternative storage and lookup strategies. The ContextImpl is a
/// template that takes in a Key type and a key comparison function. The
/// ContextImpl must satify the following expressions that return ContextImpl:
/// ContextImpl::make_root()` - to create the parent context.
/// ContextImpl::make(Key, std::any, ContextImpl& parent) - to derive a new
/// context from another context.
template <template <class, class> class ContextImpl, class Key,
class KeyEqual = std::equal_to<Key>>
class basic_context {
using ContextImplType = ContextImpl<Key, KeyEqual>;
ContextImplType impl_;
public:
basic_context() : impl_(ContextImplType::make_root()) {}
basic_context(ContextImplType i) : impl_(std::move(i)) {}
basic_context with_value(Key key, std::any value) {
auto i = ContextImplType::make(std::move(key), std::move(value), impl_);
return basic_context{std::move(i)};
}
template <class ValueType, class... Args>
basic_context with_args(Key key, Args &&... args) {
std::any value{std::in_place_type<ValueType>, std::forward<Args>(args)...};
auto i = ContextImplType::make(std::move(key), std::move(value), impl_);
return basic_context{std::move(i)};
}
std::any value(const Key &key) const { return impl_.lookup(key); }
};
} // end namespace context