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
4466c76f
Unverified
Commit
4466c76f
authored
Oct 09, 2018
by
Dominik Charousset
Committed by
GitHub
Oct 09, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #778
Add free function to set values in config_value dictionaries. Close #769.
parents
5418edea
f8931fc2
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
92 additions
and
9 deletions
+92
-9
libcaf_core/caf/config_value.hpp
libcaf_core/caf/config_value.hpp
+25
-0
libcaf_core/caf/dictionary.hpp
libcaf_core/caf/dictionary.hpp
+9
-9
libcaf_core/src/config_value.cpp
libcaf_core/src/config_value.cpp
+44
-0
libcaf_core/test/config_value.cpp
libcaf_core/test/config_value.cpp
+14
-0
No files found.
libcaf_core/caf/config_value.hpp
View file @
4466c76f
...
...
@@ -530,6 +530,31 @@ T get_or(const actor_system_config& cfg, string_view name,
std
::
string
get_or
(
const
actor_system_config
&
cfg
,
string_view
name
,
const
char
*
default_value
);
/// @private
void
put_impl
(
config_value
::
dictionary
&
dict
,
const
std
::
vector
<
string_view
>&
path
,
config_value
&
value
);
/// @private
void
put_impl
(
config_value
::
dictionary
&
dict
,
string_view
key
,
config_value
&
value
);
/// @private
void
put_impl
(
dictionary
<
config_value
::
dictionary
>&
dict
,
string_view
key
,
config_value
&
value
);
/// Converts `value` to a `config_value` and assigns it to `key`.
/// @param dict Dictionary of key-value pairs.
/// @param key Human-readable nested keys in the form `category.key`.
/// @param value New value for given `key`.
template
<
class
T
>
void
put
(
dictionary
<
config_value
::
dictionary
>&
dict
,
string_view
key
,
T
&&
value
)
{
config_value
tmp
{
std
::
forward
<
T
>
(
value
)};
put_impl
(
dict
,
key
,
tmp
);
}
/// @relates config_value
bool
operator
<
(
const
config_value
&
x
,
const
config_value
&
y
);
...
...
libcaf_core/caf/dictionary.hpp
View file @
4466c76f
...
...
@@ -189,11 +189,11 @@ public:
iterator_bool_pair
emplace
(
K
&&
key
,
T
&&
value
)
{
auto
i
=
lower_bound
(
key
);
if
(
i
==
end
())
return
xs_
.
emplace
(
copy
(
std
::
forward
<
K
>
(
key
)),
std
::
forward
<
T
>
(
value
)
);
return
xs_
.
emplace
(
copy
(
std
::
forward
<
K
>
(
key
)),
V
{
std
::
forward
<
T
>
(
value
)}
);
if
(
i
->
first
==
key
)
return
{
i
,
false
};
return
{
xs_
.
emplace_hint
(
i
,
copy
(
std
::
forward
<
K
>
(
key
)),
std
::
forward
<
T
>
(
value
)
),
V
{
std
::
forward
<
T
>
(
value
)}
),
true
};
}
...
...
@@ -207,18 +207,18 @@ public:
template
<
class
T
>
iterator_bool_pair
insert
(
string_view
key
,
T
&&
value
)
{
return
emplace
(
key
,
std
::
forward
<
T
>
(
value
)
);
return
emplace
(
key
,
V
{
std
::
forward
<
T
>
(
value
)}
);
}
template
<
class
K
,
class
T
>
iterator
emplace_hint
(
iterator
hint
,
K
&&
key
,
T
&&
value
)
{
if
(
hint
==
end
()
||
hint
->
first
>
key
)
return
xs_
.
emplace
(
copy
(
std
::
forward
<
K
>
(
key
)),
std
::
forward
<
T
>
(
value
)
)
return
xs_
.
emplace
(
copy
(
std
::
forward
<
K
>
(
key
)),
V
{
std
::
forward
<
T
>
(
value
)}
)
.
first
;
if
(
hint
->
first
==
key
)
return
hint
;
return
xs_
.
emplace_hint
(
hint
,
copy
(
std
::
forward
<
K
>
(
key
)),
std
::
forward
<
T
>
(
value
)
);
V
{
std
::
forward
<
T
>
(
value
)}
);
}
template
<
class
T
>
...
...
@@ -230,12 +230,12 @@ public:
iterator_bool_pair
insert_or_assign
(
string_view
key
,
T
&&
value
)
{
auto
i
=
lower_bound
(
key
);
if
(
i
==
end
())
return
xs_
.
emplace
(
copy
(
key
),
std
::
forward
<
T
>
(
value
)
);
return
xs_
.
emplace
(
copy
(
key
),
V
{
std
::
forward
<
T
>
(
value
)}
);
if
(
i
->
first
==
key
)
{
i
->
second
=
std
::
forward
<
T
>
(
value
)
;
i
->
second
=
V
{
std
::
forward
<
T
>
(
value
)}
;
return
{
i
,
false
};
}
return
{
xs_
.
emplace_hint
(
i
,
copy
(
key
),
std
::
forward
<
T
>
(
value
)
),
true
};
return
{
xs_
.
emplace_hint
(
i
,
copy
(
key
),
V
{
std
::
forward
<
T
>
(
value
)}
),
true
};
}
template
<
class
T
>
...
...
@@ -247,7 +247,7 @@ public:
hint
->
second
=
std
::
forward
<
T
>
(
value
);
return
hint
;
}
return
xs_
.
emplace_hint
(
hint
,
copy
(
key
),
std
::
forward
<
T
>
(
value
)
);
return
xs_
.
emplace_hint
(
hint
,
copy
(
key
),
V
{
std
::
forward
<
T
>
(
value
)}
);
}
// -- lookup -----------------------------------------------------------------
...
...
libcaf_core/src/config_value.cpp
View file @
4466c76f
...
...
@@ -146,5 +146,49 @@ std::string get_or(const actor_system_config& cfg, string_view name,
return
get_or
(
content
(
cfg
),
name
,
default_value
);
}
void
put_impl
(
config_value
::
dictionary
&
dict
,
const
std
::
vector
<
string_view
>&
path
,
config_value
&
value
)
{
// Sanity check.
if
(
path
.
empty
())
return
;
// Navigate path.
auto
last
=
path
.
end
();
auto
back
=
last
-
1
;
auto
current
=
&
dict
;
// Resolve path by navigating the map-of-maps of create the necessary layout
// when needed.
for
(
auto
i
=
path
.
begin
();
i
!=
back
;
++
i
)
{
auto
iter
=
current
->
emplace
(
*
i
,
config_value
::
dictionary
{}).
first
;
if
(
auto
val
=
get_if
<
config_value
::
dictionary
>
(
&
iter
->
second
))
{
current
=
val
;
}
else
{
iter
->
second
=
config_value
::
dictionary
{};
current
=
&
get
<
config_value
::
dictionary
>
(
iter
->
second
);
}
}
// Set key-value pair on the leaf.
current
->
insert_or_assign
(
*
back
,
std
::
move
(
value
));
}
void
put_impl
(
config_value
::
dictionary
&
dict
,
string_view
key
,
config_value
&
value
)
{
std
::
vector
<
string_view
>
path
;
split
(
path
,
key
,
"."
);
put_impl
(
dict
,
path
,
value
);
}
void
put_impl
(
dictionary
<
config_value
::
dictionary
>&
dict
,
string_view
key
,
config_value
&
value
)
{
// Split the name into a path.
std
::
vector
<
string_view
>
path
;
split
(
path
,
key
,
"."
);
// Sanity check. At the very least, we need a category and a key.
if
(
path
.
size
()
<
2
)
return
;
auto
category
=
path
.
front
();
path
.
erase
(
path
.
begin
());
put_impl
(
dict
[
category
],
path
,
value
);
}
}
// namespace caf
libcaf_core/test/config_value.cpp
View file @
4466c76f
...
...
@@ -250,3 +250,17 @@ CAF_TEST(unsuccessful parsing) {
CAF_CHECK_EQUAL
(
parse
(
"{a=1,"
),
pec
::
unexpected_eof
);
CAF_CHECK_EQUAL
(
parse
(
"{a=1 b=2}"
),
pec
::
unexpected_character
);
}
CAF_TEST
(
put
values
)
{
using
v
=
config_value
;
using
d
=
config_value
::
dictionary
;
using
dd
=
caf
::
dictionary
<
d
>
;
dd
content
;
put
(
content
,
"a.b"
,
42
);
CAF_CHECK_EQUAL
(
content
,
dd
({{
"a"
,
d
({{
"b"
,
v
{
42
}}})}}));
put
(
content
,
"a.b.c"
,
1
);
CAF_CHECK_EQUAL
(
content
,
dd
({{
"a"
,
d
({{
"b"
,
v
{
d
({{
"c"
,
v
{
1
}}})}}})}}));
put
(
content
,
"a.b.d"
,
2
);
CAF_CHECK_EQUAL
(
content
,
dd
({{
"a"
,
d
({{
"b"
,
v
{
d
({{
"c"
,
v
{
1
}},
{
"d"
,
v
{
2
}}})}}})}}));
}
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