cgv
Loading...
Searching...
No Matches
reflect_enum.cxx
1#include "reflect_enum.h"
2
3#include <cgv/utils/convert.h>
5
6namespace cgv {
7 namespace reflect {
8
9void abst_enum_reflection_traits::parse_declarations()
10{
11 cgv::utils::parse_enum_declarations(declarations(), ref_names(), ref_values());
12}
13
15{
16 return true;
17}
18
19bool abst_enum_reflection_traits::set_from_string(void* member_ptr, const std::string& str_val)
20{
21 unsigned i = cgv::utils::find_enum_index(str_val, ref_names());
22 if (i == -1)
23 return false;
24 *static_cast<int*>(member_ptr) = ref_values()[i];
25 return true;
26}
27
28void abst_enum_reflection_traits::get_to_string(const void* member_ptr, std::string& str_val)
29{
30 unsigned i = cgv::utils::find_enum_index(*static_cast<const int*>(member_ptr), ref_values());
31 if (i != -1)
32 str_val = to_string(ref_names()[i]);
33 else
34 str_val = "UNDEF";
35}
36
38{
39 return true;
40}
42{
43 return (unsigned) const_cast<abst_enum_reflection_traits*>(this)->ref_values().size();
44}
45std::string abst_enum_reflection_traits::get_enum_name(unsigned i) const
46{
47 return to_string(const_cast<abst_enum_reflection_traits*>(this)->ref_names()[i]);
48}
50{
51 return const_cast<abst_enum_reflection_traits*>(this)->ref_values()[i];
52}
53
54 }
55}
void parse_enum_declarations(const std::string &enum_declarations, std::vector< token > &enum_names, std::vector< int > &enum_values)
parse an enum declaration string into names and values
Definition scan_enum.cxx:10
unsigned find_enum_index(int value, const std::vector< int > &enum_values)
convert value to index
Definition scan_enum.cxx:35
the cgv namespace
Definition print.h:11
Helper functions to process enum declarations from strings.
type independent functionality for all enum fallback implementations
std::string get_enum_name(unsigned i) const
return the name of the i-th enum item
void get_to_string(const void *member_ptr, std::string &str_val)
convert given instance into a string value
bool set_from_string(void *member_ptr, const std::string &str_val)
convert a given string value to the reflected type and store in the instance pointer
unsigned get_nr_enum_items() const
return the number of enum items
bool has_string_conversions() const
whether type can be converted to string, defaults to false
int get_enum_value(unsigned i) const
return the value of the i-th enum item
bool has_enum_interface() const
return whether the traits class implements the enum interface