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
b079d078
Commit
b079d078
authored
Jun 08, 2018
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify key lookup API
parent
fdf82e06
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
39 deletions
+32
-39
libcaf_core/caf/config_value.hpp
libcaf_core/caf/config_value.hpp
+18
-31
libcaf_core/test/config_value.cpp
libcaf_core/test/config_value.cpp
+14
-8
No files found.
libcaf_core/caf/config_value.hpp
View file @
b079d078
...
...
@@ -31,6 +31,7 @@
#include "caf/detail/type_traits.hpp"
#include "caf/fwd.hpp"
#include "caf/optional.hpp"
#include "caf/string_algorithms.hpp"
#include "caf/sum_type.hpp"
#include "caf/sum_type_access.hpp"
#include "caf/timestamp.hpp"
...
...
@@ -359,11 +360,14 @@ struct config_value_access<std::map<std::string, V>> {
}
};
///
Retrieves
.
///
Tries to retrieve the value associated to `name` from `xs`
.
/// @relates config_value
template
<
class
T
>
optional
<
T
>
get_if
(
const
config_value
::
dictionary
*
xs
,
std
::
initializer_list
<
std
::
string
>
path
)
{
const
std
::
string
&
name
)
{
// Split the name into a path.
std
::
vector
<
std
::
string
>
path
;
split
(
path
,
name
,
"."
);
// Sanity check.
if
(
path
.
size
()
==
0
)
return
none
;
...
...
@@ -388,43 +392,26 @@ optional<T> get_if(const config_value::dictionary* xs,
return
none
;
}
/// Retrieves the value associated to `name` from `xs`.
/// @relates config_value
template
<
class
T
>
optional
<
T
>
get_if
(
const
config_value
::
dictionary
*
xs
,
std
::
string
path
)
{
return
get_if
<
T
>
(
xs
,
{
path
});
}
/// @relates config_value
template
<
class
T
>
T
get
(
const
config_value
::
dictionary
&
xs
,
std
::
initializer_list
<
std
::
string
>
path
)
{
auto
result
=
get_if
<
T
>
(
&
xs
,
std
::
move
(
path
));
if
(
!
result
)
CAF_RAISE_ERROR
(
"invalid type found"
);
return
std
::
move
(
*
result
);
}
/// @relates config_value
template
<
class
T
>
T
get
(
const
config_value
::
dictionary
&
xs
,
std
::
string
path
)
{
return
get
<
T
>
(
xs
,
{
path
});
}
/// @relates config_value
template
<
class
T
>
T
get_or
(
const
config_value
::
dictionary
&
xs
,
std
::
initializer_list
<
std
::
string
>
path
,
const
T
&
default_value
)
{
auto
result
=
get
<
T
>
(
xs
,
std
::
move
(
path
));
T
get
(
const
config_value
::
dictionary
&
xs
,
const
std
::
string
&
name
)
{
auto
result
=
get_if
<
T
>
(
&
xs
,
name
);
if
(
result
)
return
*
result
;
return
default_value
;
return
std
::
move
(
*
result
)
;
CAF_RAISE_ERROR
(
"invalid type or name found"
)
;
}
/// Retrieves the value associated to `name` from `xs` or returns
/// `default_value`.
/// @relates config_value
template
<
class
T
>
T
get_or
(
const
config_value
::
dictionary
&
xs
,
std
::
string
path
,
T
get_or
(
const
config_value
::
dictionary
&
xs
,
const
std
::
string
&
name
,
const
T
&
default_value
)
{
return
get_or
(
xs
,
{
path
},
default_value
);
auto
result
=
get_if
<
T
>
(
&
xs
,
name
);
if
(
result
)
return
std
::
move
(
*
result
);
return
default_value
;
}
/// @relates config_value
...
...
libcaf_core/test/config_value.cpp
View file @
b079d078
...
...
@@ -124,19 +124,25 @@ CAF_TEST(append) {
CAF_TEST
(
homogeneous
dictionary
)
{
using
integer_map
=
std
::
map
<
std
::
string
,
int64_t
>
;
auto
xs
=
dict
()
.
add
(
"value-1"
,
config_value
{
1
})
.
add
(
"value-1"
,
config_value
{
1
00000
})
.
add
(
"value-2"
,
config_value
{
2
})
.
add
(
"value-3"
,
config_value
{
3
})
.
add
(
"value-4"
,
config_value
{
4
})
.
make
();
integer_map
ys
{
{
"value-1"
,
1
},
{
"value-1"
,
1
00000
},
{
"value-2"
,
2
},
{
"value-3"
,
3
},
{
"value-4"
,
4
},
};
CAF_CHECK_EQUAL
(
get
<
int64_t
>
(
xs
,
"value-1"
),
1
);
CAF_CHECK_EQUAL
(
get
<
integer_map
>
(
config_value
{
xs
}),
ys
);
config_value
xs_cv
{
xs
};
CAF_CHECK_EQUAL
(
get_if
<
int64_t
>
(
&
xs
,
"value-1"
),
int64_t
{
100000
});
CAF_CHECK_EQUAL
(
get_if
<
int32_t
>
(
&
xs
,
"value-1"
),
int32_t
{
100000
});
CAF_CHECK_EQUAL
(
get_if
<
int16_t
>
(
&
xs
,
"value-1"
),
none
);
CAF_CHECK_EQUAL
(
get
<
int64_t
>
(
xs
,
"value-1"
),
100000
);
CAF_CHECK_EQUAL
(
get
<
int32_t
>
(
xs
,
"value-1"
),
100000
);
CAF_CHECK_EQUAL
(
get_if
<
integer_map
>
(
&
xs_cv
),
ys
);
CAF_CHECK_EQUAL
(
get
<
integer_map
>
(
xs_cv
),
ys
);
}
CAF_TEST
(
heterogeneous
dictionary
)
{
...
...
@@ -152,9 +158,9 @@ CAF_TEST(heterogeneous dictionary) {
.
make_cv
())
.
make
();
CAF_CHECK_EQUAL
(
get
<
atom_value
>
(
xs
,
{
"scheduler"
,
"policy"
}
),
atom
(
"none"
));
CAF_CHECK_EQUAL
(
get
<
int64_t
>
(
xs
,
{
"scheduler"
,
"max-threads"
}
),
2
);
CAF_CHECK_EQUAL
(
get_if
<
double
>
(
&
xs
,
{
"scheduler"
,
"max-threads"
}
),
none
);
CAF_CHECK_EQUAL
(
get
<
atom_value
>
(
xs
,
"scheduler.policy"
),
atom
(
"none"
));
CAF_CHECK_EQUAL
(
get
<
int64_t
>
(
xs
,
"scheduler.max-threads"
),
2
);
CAF_CHECK_EQUAL
(
get_if
<
double
>
(
&
xs
,
"scheduler.max-threads"
),
none
);
string_list
nodes
{
"sun"
,
"venus"
,
"mercury"
,
"earth"
,
"mars"
};
CAF_CHECK_EQUAL
(
get
<
string_list
>
(
xs
,
{
"nodes"
,
"preload"
}
),
nodes
);
CAF_CHECK_EQUAL
(
get
<
string_list
>
(
xs
,
"nodes.preload"
),
nodes
);
}
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