CXL is a supplemental library to the standard C++ library(STL).
function_traits
includearity
,result_type
andarg<N>
, also definedarguments_tuple
that contains all argument typesmake_function
creates std::function wrapper that matches the original function signaturemake_function_type<F>
is the correspoding type for std::function wrappervariant
Similiar to Boost.Variant, with fullmove
and rvalue-reference support, less hacks, i.e. avoid using ofboost::detail::variant::void_
to fulfill all template arguments.- Minimal reflection support by defining some metadata with macros:
CXL_BEGIN_REFLECTED(TYPE, COUNT, ...)
begins a embedded metadata definition:TYPE
the type that metadata is embedded in.COUNT
number of elements belong toTYPE
.- Also some additional attributes can be defined:
CXL_SQL_TABLE(X)
attach asql_table()
attribute,X
is a string literal.CXL_XML_NODE(X)
attach axml_node()
attribute,X
is a string literal.CXL_XML_NAMESPACE
attach axml_namespace()
attribute,X
is a string literal.
CXL_END_REFLECTED()
ends a embedded metadata definitionCXL_EXT_BEGIN_REFLECTED(TYPE, COUNT, ...)
begins a standalone metadata definition, this macro must appear in the global namespace, besides this, it has same syntax and effects asCXL_BEGIN_REFLECTED(TYPE, COUNT, ...)
.CXL_EXT_END_REFLECTED()
same asCXL_END_REFLECTED()
, but ends a standalone metadata definition.CXL_REFLECTED_MEMBER(INDEX, NAME, ...)
define the metadata of a member, must appear insideCXL_BEGIN_REFLECTED
/CXL_EXT_BEGIN_REFLECTED
andCXL_END_REFLECTED
/CXL_EXT_END_REFLECTED
.TYPE
the type of the memberINDEX
the index of the member, must start from 0 and be continuous.- Also some additional attributes can be defined:
CXL_JSON_KEY(X)
attach ajson_key()
attribute,X
is a string literal.CXL_XML_NAMESPACE(X)
attach axml_namespace()
attribute,X
is a string literal.CXL_SQL_FIELD
attach asql_field()
attribute,X
is a string literal.
CXL_REFLECTED_MEMBER_KEY(INDEX, NAME, KEY, ...)
same asCXL_REFLECTED_MEMBER
, but defines akey()
attributes.CXL_REFLECTED_RO_MEMBER
, same asCXL_REFLECTED_MEMBER
, but indicates the member is readonly.CXL_REFLECTED_RO_MEMBER_KEY
same asCXL_REFLECTED_MEMBER_KEY
, but indicates the member is readonly.CXL_REFLECTED_ATTRIBUTE(INDEX, TYPE, NAME, GETTER, SETTER, ...)
, same asCXL_REFLECTED_MEMBER
, but the attribute is get/set withGETTER
andSETTER
:CXL_MEM_GETTER(NAME)
use member functionNAME
as the getter.CXL_MEM_SETTER
use member functionNAME
as the setter
CXL_REFLECTED_ATTRIBUTE_KEY
same asCXL_REFLECTED_ATTRIBUTE
, but defineskey()
attribute.CXL_REFLECTED_RO_ATTRIBUTE
same asCXL_REFLECTED_ATTRIBUTE
, but the member is readonly.CXL_REFLECTED_RO_ATTRIBUTE_KEY
same asCXL_REFLECTED_ATTRIBUTE_KEY
, but the member is readonly.
- After metadata are defined, reflection support is added to the type:
constexpr bool cxl::reflectable<T>
is true if T supports reflection, all types with metadata defined do, as well asstd::tuple
,std::pair
, andstd::array
.to_variant_t<T>
is the correspondingvariant
type, which includes types of all attributes ofT
, used to dynamically access all members in T.tuple_size<T>
same asstd::tuple_size<T>
, but also supports reflectable types.tuple_element<I, T>
same asstd::tuple_element<I, T>
, but also supports reflectable types.get_variant<T>(i, t)
returns avariant
contains thei
-th memeber oft
.get<Type>(i, t)
return thei
-th member oft
with typeType
, if theType
is not the actual type of the member, astd::bad_cast
exception will be thrown.set(i, t, v)
set thei
-th member oft
tov
, if the type ofv
is not the actual type of the member, or the member is readonly, astd::bad_cast
exception will be thrown.const char *get_name<T>()
return the name recorded in metadata ofT
.const char *get_sql_table<T>()
return thesql_table()
attribute ofT
, returns toname()
if the attribute doesn't exist.const char *get_xml_node<T>()
return thexml_node()
attribute ofT
, returns toname()
if the attribute doesn't exist.const char *get_xml_namespce<T>()
return thexml_node()
attribute ofT
, returns""
if the attribute doesn't exist.const char *get_element_name<T>(size_t i)
returns the name ofi
-th member of T.const char *get_element_key<T>(size_t i)
returns the attributekey()
ofi
-th member of T, by default this attribute has same value with name.const char *get_element_sql_field<T>(size_t i)
returns the attributesql_field()
ofi
-th member of T, returnskey()
if the attribute doesn't exists.const char *get_element_json_key<T>(size_t i)
returns the attributejson_key()
ofi
-th member of T, returnskey()
if the attribute doesn't exists.const char *get_element_xml_node<T>()
return thexml_node()
attribute ofT
, returnskey()
if the attribute doesn't exist.const char *get_xml_namespace<T>()
return thexml_node()
attribute ofT
, returns""
if the attribute doesn't exist.
- Type with metadata defined supports
std::get
, and can be treated as astd::tuple
in most scenarios.