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
b397c962
Commit
b397c962
authored
Aug 10, 2019
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use new parse API when parsing CLI arguments
parent
314970e4
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
148 additions
and
136 deletions
+148
-136
libcaf_core/caf/config_value.hpp
libcaf_core/caf/config_value.hpp
+31
-7
libcaf_core/caf/detail/parse.hpp
libcaf_core/caf/detail/parse.hpp
+27
-0
libcaf_core/caf/detail/parser/read_bool.hpp
libcaf_core/caf/detail/parser/read_bool.hpp
+2
-2
libcaf_core/caf/make_config_option.hpp
libcaf_core/caf/make_config_option.hpp
+27
-41
libcaf_core/src/make_config_option.cpp
libcaf_core/src/make_config_option.cpp
+47
-75
libcaf_core/src/parse.cpp
libcaf_core/src/parse.cpp
+3
-0
libcaf_core/test/config_option.cpp
libcaf_core/test/config_option.cpp
+4
-4
libcaf_core/test/config_option_set.cpp
libcaf_core/test/config_option_set.cpp
+7
-7
No files found.
libcaf_core/caf/config_value.hpp
View file @
b397c962
...
...
@@ -20,6 +20,7 @@
#include <chrono>
#include <cstdint>
#include <iterator>
#include <map>
#include <string>
#include <type_traits>
...
...
@@ -127,22 +128,22 @@ public:
const
char
*
type_name
()
const
noexcept
;
/// Returns the underlying variant.
inline
variant_type
&
get_data
()
{
variant_type
&
get_data
()
{
return
data_
;
}
/// Returns the underlying variant.
inline
const
variant_type
&
get_data
()
const
{
const
variant_type
&
get_data
()
const
{
return
data_
;
}
/// Returns a pointer to the underlying variant.
inline
variant_type
*
get_data_ptr
()
{
variant_type
*
get_data_ptr
()
{
return
&
data_
;
}
/// Returns a pointer to the underlying variant.
inline
const
variant_type
*
get_data_ptr
()
const
{
const
variant_type
*
get_data_ptr
()
const
{
return
&
data_
;
}
...
...
@@ -153,15 +154,15 @@ private:
// -- auto conversion of related types ---------------------------------------
inline
void
set
(
bool
x
)
{
void
set
(
bool
x
)
{
data_
=
x
;
}
inline
void
set
(
float
x
)
{
void
set
(
float
x
)
{
data_
=
static_cast
<
double
>
(
x
);
}
inline
void
set
(
const
char
*
x
)
{
void
set
(
const
char
*
x
)
{
data_
=
std
::
string
{
x
};
}
...
...
@@ -172,6 +173,29 @@ private:
data_
=
std
::
move
(
x
);
}
template
<
class
T
>
void
set_range
(
T
&
xs
,
std
::
true_type
)
{
auto
&
dict
=
as_dictionary
();
for
(
auto
&
kvp
:
xs
)
dict
.
emplace
(
kvp
.
first
,
std
::
move
(
kvp
.
second
));
}
template
<
class
T
>
void
set_range
(
T
&
xs
,
std
::
false_type
)
{
auto
&
ls
=
as_list
();
ls
.
insert
(
ls
.
begin
(),
std
::
make_move_iterator
(
xs
.
begin
()),
std
::
make_move_iterator
(
xs
.
end
()));
}
template
<
class
T
>
detail
::
enable_if_t
<
detail
::
is_iterable
<
T
>::
value
&&
!
detail
::
is_one_of
<
T
,
string
,
list
,
dictionary
>::
value
>
set
(
T
xs
)
{
using
value_type
=
typename
T
::
value_type
;
detail
::
bool_token
<
detail
::
is_pair
<
value_type
>::
value
>
is_map_type
;
set_range
(
xs
,
is_map_type
);
}
template
<
class
T
>
detail
::
enable_if_t
<
std
::
is_integral
<
T
>::
value
>
set
(
T
x
)
{
data_
=
static_cast
<
int64_t
>
(
x
);
...
...
libcaf_core/caf/detail/parse.hpp
View file @
b397c962
...
...
@@ -20,12 +20,16 @@
#include <cstdint>
#include <iterator>
#include <type_traits>
#include <utility>
#include <vector>
#include "caf/detail/parser/state.hpp"
#include "caf/detail/squashed_int.hpp"
#include "caf/detail/type_traits.hpp"
#include "caf/error.hpp"
#include "caf/fwd.hpp"
#include "caf/none.hpp"
#include "caf/string_view.hpp"
#include "caf/unit.hpp"
...
...
@@ -34,6 +38,10 @@ namespace detail {
using
parse_state
=
parser
::
state
<
string_view
::
iterator
>
;
// -- boolean type -------------------------------------------------------------
void
parse
(
parse_state
&
ps
,
bool
&
x
);
// -- signed integer types -----------------------------------------------------
void
parse
(
parse_state
&
ps
,
int8_t
&
x
);
...
...
@@ -54,6 +62,14 @@ void parse(parse_state& ps, uint32_t& x);
void
parse
(
parse_state
&
ps
,
uint64_t
&
x
);
// -- non-fixed size integer types ---------------------------------------------
template
<
class
T
>
detail
::
enable_if_t
<
std
::
is_integral
<
T
>::
value
>
parse
(
parse_state
&
ps
,
T
&
x
)
{
using
squashed_type
=
squashed_int_t
<
T
>
;
return
parse
(
ps
,
reinterpret_cast
<
squashed_type
&>
(
x
));
}
// -- floating point types -----------------------------------------------------
void
parse
(
parse_state
&
ps
,
float
&
x
);
...
...
@@ -124,5 +140,16 @@ enable_if_tt<is_iterable<T>> parse(parse_state& ps, T& xs) {
ps
.
code
=
ps
.
at_end
()
?
pec
::
success
:
pec
::
trailing_character
;
}
// -- convenience functions ----------------------------------------------------
template
<
class
T
>
error
parse
(
string_view
str
,
T
&
x
)
{
parse_state
ps
{
str
.
begin
(),
str
.
end
()};
parse
(
ps
,
x
);
if
(
ps
.
code
==
pec
::
success
)
return
none
;
return
make_error
(
ps
.
code
,
str
);
}
}
// namespace detail
}
// namespace caf
libcaf_core/caf/detail/parser/read_bool.hpp
View file @
b397c962
...
...
@@ -38,11 +38,11 @@ namespace parser {
/// Reads a boolean.
template
<
class
Iterator
,
class
Sentinel
,
class
Consumer
>
void
read_bool
(
state
<
Iterator
,
Sentinel
>&
ps
,
Consumer
&
consumer
)
{
void
read_bool
(
state
<
Iterator
,
Sentinel
>&
ps
,
Consumer
&
&
consumer
)
{
bool
res
=
false
;
auto
g
=
make_scope_guard
([
&
]
{
if
(
ps
.
code
<=
pec
::
trailing_character
)
consumer
.
value
(
res
);
consumer
.
value
(
std
::
move
(
res
)
);
});
start
();
state
(
init
)
{
...
...
libcaf_core/caf/make_config_option.hpp
View file @
b397c962
...
...
@@ -22,6 +22,7 @@
#include "caf/config_option.hpp"
#include "caf/config_value.hpp"
#include "caf/detail/parse.hpp"
#include "caf/detail/type_name.hpp"
#include "caf/error.hpp"
#include "caf/expected.hpp"
...
...
@@ -34,34 +35,47 @@ namespace caf {
namespace
detail
{
template
<
class
T
>
error
default_config_option_check
(
const
config_value
&
x
)
{
error
check_impl
(
const
config_value
&
x
)
{
if
(
holds_alternative
<
T
>
(
x
))
return
none
;
return
make_error
(
pec
::
type_mismatch
);
}
template
<
class
T
>
void
default_config_option_store
(
void
*
ptr
,
const
config_value
&
x
)
{
void
store_impl
(
void
*
ptr
,
const
config_value
&
x
)
{
*
static_cast
<
T
*>
(
ptr
)
=
get
<
T
>
(
x
);
}
template
<
class
T
>
expected
<
config_value
>
default_config_option_parse
(
void
*
ptr
,
string_view
str
)
{
auto
result
=
config_value
::
parse
(
str
.
begin
(),
str
.
end
());
if
(
result
)
{
if
(
!
holds_alternative
<
T
>
(
*
result
))
return
make_error
(
pec
::
type_mismatch
);
if
(
ptr
!=
nullptr
)
*
static_cast
<
T
*>
(
ptr
)
=
get
<
T
>
(
*
result
);
config_value
get_impl
(
const
void
*
ptr
)
{
return
config_value
{
*
reinterpret_cast
<
const
T
*>
(
ptr
)};
}
template
<
class
T
>
expected
<
config_value
>
parse_impl
(
T
*
ptr
,
string_view
str
)
{
if
(
ptr
!=
nullptr
)
{
if
(
auto
err
=
parse
(
str
,
*
ptr
))
return
err
;
return
config_value
{
*
ptr
};
}
return
result
;
T
tmp
;
if
(
auto
err
=
parse
(
str
,
tmp
))
return
err
;
return
config_value
{
std
::
move
(
tmp
)};
}
expected
<
config_value
>
parse_impl
(
std
::
string
*
ptr
,
string_view
str
);
template
<
class
T
>
expected
<
config_value
>
parse_impl_delegate
(
void
*
ptr
,
string_view
str
)
{
return
parse_impl
(
reinterpret_cast
<
T
*>
(
ptr
),
str
);
}
template
<
class
T
>
config_option
::
meta_state
*
option_meta_state_instance
()
{
static
config_option
::
meta_state
obj
{
default_config_option_check
<
T
>
,
default_config_option_store
<
T
>
,
nullptr
,
default_config_option_parse
<
T
>
,
detail
::
type_name
<
T
>
()};
static
config_option
::
meta_state
obj
{
check_impl
<
T
>
,
store_impl
<
T
>
,
get_impl
<
T
>
,
parse_impl_delegate
<
T
>
,
detail
::
type_name
<
T
>
()};
return
&
obj
;
}
...
...
@@ -101,32 +115,4 @@ config_option make_ms_resolution_config_option(size_t& storage,
string_view
name
,
string_view
description
);
// -- specializations for common types -----------------------------------------
#define CAF_SPECIALIZE_META_STATE(type) \
extern config_option::meta_state type##_meta_state; \
template <> \
inline config_option::meta_state* option_meta_state_instance<type>() { \
return &type##_meta_state; \
}
namespace
detail
{
CAF_SPECIALIZE_META_STATE
(
atom_value
)
CAF_SPECIALIZE_META_STATE
(
bool
)
CAF_SPECIALIZE_META_STATE
(
size_t
)
extern
config_option
::
meta_state
string_meta_state
;
template
<
>
inline
config_option
::
meta_state
*
option_meta_state_instance
<
std
::
string
>
()
{
return
&
string_meta_state
;
}
}
// namespace detail
#undef CAF_SPECIALIZE_META_STATE
}
// namespace caf
libcaf_core/src/make_config_option.cpp
View file @
b397c962
...
...
@@ -32,40 +32,50 @@ using std::string;
namespace
caf
{
namespace
{
using
meta_state
=
config_option
::
meta_state
;
namespace
detail
{
template
<
class
T
>
config_value
get_impl
(
const
void
*
ptr
)
{
CAF_ASSERT
(
ptr
!=
nullptr
);
return
config_value
{
*
static_cast
<
const
T
*>
(
ptr
)};
expected
<
config_value
>
parse_impl
(
std
::
string
*
ptr
,
string_view
str
)
{
// Parse quoted strings, otherwise consume the entire string.
auto
e
=
str
.
end
();
auto
i
=
std
::
find_if
(
str
.
begin
(),
e
,
[](
char
c
)
{
return
!
isspace
(
c
);
});
if
(
i
==
e
)
{
if
(
ptr
!=
nullptr
)
ptr
->
assign
(
i
,
e
);
return
config_value
{
std
::
string
{
i
,
e
}};
}
if
(
*
i
==
'"'
)
{
if
(
ptr
==
nullptr
)
{
std
::
string
tmp
;
if
(
auto
err
=
parse
(
str
,
tmp
))
return
err
;
return
config_value
{
std
::
move
(
tmp
)};
}
else
{
if
(
auto
err
=
parse
(
str
,
*
ptr
))
return
err
;
return
config_value
{
*
ptr
};
}
}
if
(
ptr
!=
nullptr
)
ptr
->
assign
(
str
.
begin
(),
str
.
end
());
return
config_value
{
std
::
string
{
str
.
begin
(),
str
.
end
()}};
}
error
bool_check
(
const
config_value
&
x
)
{
if
(
holds_alternative
<
bool
>
(
x
))
return
none
;
return
make_error
(
pec
::
type_mismatch
);
}
}
// namespace detail
void
bool_store
(
void
*
ptr
,
const
config_value
&
x
)
{
*
static_cast
<
bool
*>
(
ptr
)
=
get
<
bool
>
(
x
);
}
namespace
{
using
meta_state
=
config_option
::
meta_state
;
void
bool_store_neg
(
void
*
ptr
,
const
config_value
&
x
)
{
*
static_cast
<
bool
*>
(
ptr
)
=
!
get
<
bool
>
(
x
);
}
config_value
bool_get
(
const
void
*
ptr
)
{
return
config_value
{
*
static_cast
<
const
bool
*>
(
ptr
)};
}
config_value
bool_get_neg
(
const
void
*
ptr
)
{
return
config_value
{
!*
static_cast
<
const
bool
*>
(
ptr
)};
}
meta_state
bool_neg_meta
{
bool_check
,
bool_store_neg
,
bool_get_neg
,
nullptr
,
detail
::
type_name
<
bool
>
()};
meta_state
bool_neg_meta
{
detail
::
check_impl
<
bool
>
,
bool_store_neg
,
bool_get_neg
,
nullptr
,
detail
::
type_name
<
bool
>
()};
meta_state
us_res_meta
{
[](
const
config_value
&
x
)
->
error
{
...
...
@@ -86,63 +96,25 @@ meta_state us_res_meta{
detail
::
type_name
<
timespan
>
()
};
meta_state
ms_res_meta
{
[](
const
config_value
&
x
)
->
error
{
meta_state
ms_res_meta
{[](
const
config_value
&
x
)
->
error
{
if
(
holds_alternative
<
timespan
>
(
x
))
return
none
;
return
make_error
(
pec
::
type_mismatch
);
},
[](
void
*
ptr
,
const
config_value
&
x
)
{
*
static_cast
<
size_t
*>
(
ptr
)
=
static_cast
<
size_t
>
(
get
<
timespan
>
(
x
).
count
()
/
1000000
);
*
static_cast
<
size_t
*>
(
ptr
)
=
static_cast
<
size_t
>
(
get
<
timespan
>
(
x
).
count
()
/
1000000
);
},
[](
const
void
*
ptr
)
->
config_value
{
auto
ival
=
static_cast
<
int64_t
>
(
*
static_cast
<
const
size_t
*>
(
ptr
));
auto
ival
=
static_cast
<
int64_t
>
(
*
static_cast
<
const
size_t
*>
(
ptr
));
timespan
val
{
ival
*
1000000
};
return
config_value
{
val
};
},
nullptr
,
detail
::
type_name
<
timespan
>
()};
expected
<
config_value
>
parse_atom
(
void
*
ptr
,
string_view
str
)
{
if
(
str
.
size
()
>
10
)
return
make_error
(
pec
::
too_many_characters
);
auto
is_illegal
=
[](
char
c
)
{
return
!
(
isalnum
(
c
)
||
c
==
'_'
||
c
==
' '
);
};
auto
i
=
std
::
find_if
(
str
.
begin
(),
str
.
end
(),
is_illegal
);
if
(
i
!=
str
.
end
())
return
make_error
(
pec
::
unexpected_character
,
std
::
string
{
"invalid character: "
}
+
*
i
);
auto
result
=
atom_from_string
(
str
);
if
(
ptr
!=
nullptr
)
*
reinterpret_cast
<
atom_value
*>
(
ptr
)
=
result
;
return
config_value
{
result
};
}
expected
<
config_value
>
parse_string
(
void
*
ptr
,
string_view
str
)
{
std
::
string
result
{
str
.
begin
(),
str
.
end
()};
if
(
ptr
!=
nullptr
)
*
reinterpret_cast
<
std
::
string
*>
(
ptr
)
=
result
;
return
config_value
{
std
::
move
(
result
)};
}
nullptr
,
detail
::
type_name
<
timespan
>
()};
}
// namespace
namespace
detail
{
DEFAULT_META
(
atom_value
,
parse_atom
)
DEFAULT_META
(
size_t
,
default_config_option_parse
<
size_t
>
)
DEFAULT_META
(
string
,
parse_string
)
config_option
::
meta_state
bool_meta_state
{
bool_check
,
bool_store
,
bool_get
,
default_config_option_parse
<
bool
>
,
detail
::
type_name
<
bool
>
()};
}
// namespace detail
config_option
make_negated_config_option
(
bool
&
storage
,
string_view
category
,
string_view
name
,
string_view
description
)
{
...
...
libcaf_core/src/parse.cpp
View file @
b397c962
...
...
@@ -21,6 +21,7 @@
#include "caf/detail/consumer.hpp"
#include "caf/detail/parser/chars.hpp"
#include "caf/detail/parser/read_atom.hpp"
#include "caf/detail/parser/read_bool.hpp"
#include "caf/detail/parser/read_floating_point.hpp"
#include "caf/detail/parser/read_signed_integer.hpp"
#include "caf/detail/parser/read_string.hpp"
...
...
@@ -37,6 +38,8 @@
namespace
caf
{
namespace
detail
{
PARSE_IMPL
(
bool
,
bool
)
PARSE_IMPL
(
int8_t
,
signed_integer
)
PARSE_IMPL
(
int16_t
,
signed_integer
)
...
...
libcaf_core/test/config_option.cpp
View file @
b397c962
...
...
@@ -155,20 +155,20 @@ CAF_TEST(type int64_t) {
CAF_TEST
(
type
float
)
{
CAF_CHECK_EQUAL
(
unbox
(
read
<
float
>
(
"-1.0"
)),
-
1.0
f
);
CAF_CHECK_EQUAL
(
unbox
(
read
<
float
>
(
"-0.1"
)),
-
0.1
f
);
CAF_CHECK_EQUAL
(
read
<
float
>
(
"0"
),
none
);
CAF_CHECK_EQUAL
(
read
<
float
>
(
"0"
),
0.
f
);
CAF_CHECK_EQUAL
(
read
<
float
>
(
"
\"
0.1
\"
"
),
none
);
}
CAF_TEST
(
type
double
)
{
CAF_CHECK_EQUAL
(
unbox
(
read
<
double
>
(
"-1.0"
)),
-
1.0
);
CAF_CHECK_EQUAL
(
unbox
(
read
<
double
>
(
"-0.1"
)),
-
0.1
);
CAF_CHECK_EQUAL
(
read
<
double
>
(
"0"
),
none
);
CAF_CHECK_EQUAL
(
read
<
double
>
(
"0"
),
0.
);
CAF_CHECK_EQUAL
(
read
<
double
>
(
"
\"
0.1
\"
"
),
none
);
}
CAF_TEST
(
type
string
)
{
CAF_CHECK_EQUAL
(
unbox
(
read
<
string
>
(
"foo"
)),
"foo"
);
CAF_CHECK_EQUAL
(
unbox
(
read
<
string
>
(
"
\"
foo
\"
"
)),
"
\"
foo
\"
"
);
CAF_CHECK_EQUAL
(
unbox
(
read
<
string
>
(
"
\"
foo
\"
"
)),
"
foo
"
);
}
CAF_TEST
(
type
atom
)
{
...
...
libcaf_core/test/config_option_set.cpp
View file @
b397c962
...
...
@@ -124,20 +124,20 @@ CAF_TEST(atom parameters) {
CAF_TEST
(
string
parameters
)
{
opts
.
add
<
std
::
string
>
(
"value,v"
,
"some value"
);
CAF_MESSAGE
(
"test string option with and without quotes"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"--value=
\"
foo
bar
\"
"
}),
"
\"
foobar
\"
"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"--value=
\"
foo
\\
tbar
\"
"
}),
"foo
\t
bar
"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"--value=foobar"
}),
"foobar"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"-v"
,
"
\"
foobar
\"
"
}),
"
\"
foobar
\"
"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"-v"
,
"
\"
foobar
\"
"
}),
"
foobar
"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"-v"
,
"foobar"
}),
"foobar"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"-v
\"
foobar
\"
"
}),
"
\"
foobar
\"
"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"-v
\"
foobar
\"
"
}),
"
foobar
"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"-vfoobar"
}),
"foobar"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"--value=
\"
'abc'
\"
"
}),
"
\"
'abc'
\"
"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"--value=
\"
'abc'
\"
"
}),
"
'abc'
"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"--value='abc'"
}),
"'abc'"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"-v"
,
"
\"
'abc'
\"
"
}),
"
\"
'abc'
\"
"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"-v"
,
"
\"
'abc'
\"
"
}),
"
'abc'
"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"-v"
,
"'abc'"
}),
"'abc'"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"-v'abc'"
}),
"'abc'"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"--value=
\"
123
\"
"
}),
"
\"
123
\"
"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"--value=
\"
123
\"
"
}),
"
123
"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"--value=123"
}),
"123"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"-v"
,
"
\"
123
\"
"
}),
"
\"
123
\"
"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"-v"
,
"
\"
123
\"
"
}),
"
123
"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"-v"
,
"123"
}),
"123"
);
CAF_CHECK_EQUAL
(
read
<
std
::
string
>
({
"-v123"
}),
"123"
);
}
...
...
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