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
896f0105
Commit
896f0105
authored
Jun 19, 2018
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reduce state in config_option
parent
7478c1eb
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
40 additions
and
43 deletions
+40
-43
libcaf_core/caf/config_option.hpp
libcaf_core/caf/config_option.hpp
+11
-11
libcaf_core/caf/make_config_option.hpp
libcaf_core/caf/make_config_option.hpp
+8
-10
libcaf_core/src/config_option.cpp
libcaf_core/src/config_option.cpp
+14
-12
libcaf_core/src/make_config_option.cpp
libcaf_core/src/make_config_option.cpp
+7
-10
No files found.
libcaf_core/caf/config_option.hpp
View file @
896f0105
...
...
@@ -29,18 +29,20 @@ class config_option {
public:
// -- member types -----------------------------------------------------------
/// Custom vtable-like struct for delegating to type-specific functions.
struct
vtbl_type
{
/// Custom vtable-like struct for delegating to type-specific functions and
/// storing type-specific information shared by several config options.
struct
meta_state
{
error
(
*
check
)(
const
config_value
&
);
void
(
*
store
)(
void
*
,
const
config_value
&
);
std
::
string
(
*
type_name
)();
std
::
string
type_name
;
bool
is_flag
;
};
// -- constructors, destructors, and assignment operators --------------------
/// Constructs a config option.
config_option
(
std
::
string
category
,
const
char
*
name
,
std
::
string
description
,
bool
is_flag
,
const
vtbl_type
&
vtbl
,
void
*
storage
=
nullptr
);
const
meta_state
*
meta
,
void
*
storage
=
nullptr
);
config_option
(
config_option
&&
)
=
default
;
...
...
@@ -65,10 +67,6 @@ public:
return
description_
;
}
inline
bool
is_flag
()
const
noexcept
{
return
is_flag_
;
}
/// Returns the full name for this config option as "<category>.<long name>".
std
::
string
full_name
()
const
;
...
...
@@ -80,15 +78,17 @@ public:
void
store
(
const
config_value
&
x
)
const
;
/// Returns a human-readable representation of this option's expected type.
std
::
string
type_name
()
const
;
const
std
::
string
&
type_name
()
const
noexcept
;
/// Returns whether this config option stores a boolean flag.
bool
is_flag
()
const
noexcept
;
private:
std
::
string
category_
;
std
::
string
long_name_
;
std
::
string
short_names_
;
std
::
string
description_
;
bool
is_flag_
;
vtbl_type
vtbl_
;
const
meta_state
*
meta_
;
mutable
void
*
value_
;
};
...
...
libcaf_core/caf/make_config_option.hpp
View file @
896f0105
...
...
@@ -29,25 +29,24 @@ namespace caf {
template
<
class
T
>
config_option
make_config_option
(
const
char
*
category
,
const
char
*
name
,
const
char
*
description
)
{
config_option
::
vtbl_type
vtbl
{
static
config_option
::
meta_state
meta
{
[](
const
config_value
&
x
)
->
error
{
if
(
holds_alternative
<
T
>
(
x
))
return
none
;
return
make_error
(
pec
::
type_mismatch
);
},
nullptr
,
[]
{
return
detail
::
type_name
<
T
>
();
}
detail
::
type_name
<
T
>
(),
false
};
return
{
category
,
name
,
description
,
false
,
vtbl
};
return
{
category
,
name
,
description
,
&
meta
};
}
/// Creates a config option that synchronizes with `storage`.
template
<
class
T
>
config_option
make_config_option
(
T
&
storage
,
const
char
*
category
,
const
char
*
name
,
const
char
*
description
)
{
config_option
::
vtbl_type
vtbl
{
static
config_option
::
meta_state
meta
{
[](
const
config_value
&
x
)
->
error
{
if
(
holds_alternative
<
T
>
(
x
))
return
none
;
...
...
@@ -56,11 +55,10 @@ config_option make_config_option(T& storage, const char* category,
[](
void
*
ptr
,
const
config_value
&
x
)
{
*
static_cast
<
T
*>
(
ptr
)
=
get
<
T
>
(
x
);
},
[]
{
return
detail
::
type_name
<
T
>
();
}
detail
::
type_name
<
T
>
(),
false
};
return
{
category
,
name
,
description
,
false
,
vtbl
,
&
storage
};
return
{
category
,
name
,
description
,
&
meta
,
&
storage
};
}
// -- backward compatbility, do not use for new code ! -------------------------
...
...
libcaf_core/src/config_option.cpp
View file @
896f0105
...
...
@@ -45,16 +45,15 @@ string get_short_names(const char* name) {
namespace
caf
{
config_option
::
config_option
(
string
category
,
const
char
*
name
,
string
description
,
bool
is_flag
,
const
vtbl_type
&
vtbl
,
void
*
value
)
string
description
,
const
meta_state
*
meta
,
void
*
value
)
:
category_
(
move
(
category
)),
long_name_
(
get_long_name
(
name
)),
short_names_
(
get_short_names
(
name
)),
description_
(
move
(
description
)),
is_flag_
(
is_flag
),
vtbl_
(
vtbl
),
meta_
(
meta
),
value_
(
value
)
{
// nop
CAF_ASSERT
(
meta_
!=
nullptr
);
}
string
config_option
::
full_name
()
const
{
...
...
@@ -65,20 +64,23 @@ string config_option::full_name() const {
}
error
config_option
::
check
(
const
config_value
&
x
)
const
{
CAF_ASSERT
(
vtbl_
.
check
!=
nullptr
);
return
vtbl_
.
check
(
x
);
CAF_ASSERT
(
meta_
->
check
!=
nullptr
);
return
meta_
->
check
(
x
);
}
void
config_option
::
store
(
const
config_value
&
x
)
const
{
if
(
value_
!=
nullptr
)
{
CAF_ASSERT
(
vtbl_
.
store
!=
nullptr
);
vtbl_
.
store
(
value_
,
x
);
CAF_ASSERT
(
meta_
->
store
!=
nullptr
);
meta_
->
store
(
value_
,
x
);
}
}
std
::
string
config_option
::
type_name
()
const
{
CAF_ASSERT
(
vtbl_
.
type_name
!=
nullptr
);
return
vtbl_
.
type_name
();
const
std
::
string
&
config_option
::
type_name
()
const
noexcept
{
return
meta_
->
type_name
;
}
bool
config_option
::
is_flag
()
const
noexcept
{
return
meta_
->
is_flag
;
}
}
// namespace caf
libcaf_core/src/make_config_option.cpp
View file @
896f0105
...
...
@@ -22,7 +22,7 @@ namespace caf {
namespace
{
using
vtbl_type
=
config_option
::
vtbl_typ
e
;
using
meta_state
=
config_option
::
meta_stat
e
;
error
bool_check
(
const
config_value
&
x
)
{
if
(
holds_alternative
<
bool
>
(
x
))
...
...
@@ -38,33 +38,30 @@ void bool_store_neg(void* ptr, const config_value& x) {
*
static_cast
<
bool
*>
(
ptr
)
=
!
get
<
bool
>
(
x
);
}
std
::
string
bool_name
()
{
return
detail
::
type_name
<
bool
>
();
}
constexpr
vtbl_type
bool_vtbl
{
bool_check
,
bool_store
,
bool_name
};
meta_state
bool_meta
{
bool_check
,
bool_store
,
detail
::
type_name
<
bool
>
(),
true
};
constexpr
vtbl_type
bool_neg_vtbl
{
bool_check
,
bool_store_neg
,
bool_name
};
meta_state
bool_neg_meta
{
bool_check
,
bool_store_neg
,
detail
::
type_name
<
bool
>
(),
true
};
}
// namespace anonymous
config_option
make_negated_config_option
(
bool
&
storage
,
const
char
*
category
,
const
char
*
name
,
const
char
*
description
)
{
return
{
category
,
name
,
description
,
true
,
bool_neg_vtbl
,
&
storage
};
return
{
category
,
name
,
description
,
&
bool_neg_meta
,
&
storage
};
}
template
<
>
config_option
make_config_option
<
bool
>
(
const
char
*
category
,
const
char
*
name
,
const
char
*
description
)
{
return
{
category
,
name
,
description
,
true
,
bool_vtbl
};
return
{
category
,
name
,
description
,
&
bool_meta
};
}
template
<
>
config_option
make_config_option
<
bool
>
(
bool
&
storage
,
const
char
*
category
,
const
char
*
name
,
const
char
*
description
)
{
return
{
category
,
name
,
description
,
true
,
bool_vtbl
,
&
storage
};
return
{
category
,
name
,
description
,
&
bool_meta
,
&
storage
};
}
}
// namespace caf
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