MemberEnum.h
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #ifndef ICL_CORE_CONFIG_MEMBER_ENUM_H_INCLUDED
24 #define ICL_CORE_CONFIG_MEMBER_ENUM_H_INCLUDED
25 
31 
32 #include <algorithm>
33 #include <iterator>
34 #include <string>
35 #include <vector>
36 #include <boost/function.hpp>
37 #include <boost/lambda/bind.hpp>
38 
39 #define MEMBER_ENUM_1(suffix, cls, member1, descriptions) \
40  (new icl_core::config::MemberEnum< \
41  icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(&cls::member1)>::Type, cls>( \
42  suffix, \
43  boost::lambda::bind(&cls::member1, boost::lambda::_1), descriptions))
44 
45 #define MEMBER_ENUM_2(suffix, cls, member1, member2, descriptions) \
46  (new icl_core::config::MemberEnum< \
47  icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF( \
48  &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF( \
49  &cls::member1)>::Type::member2)>::Type, cls>( \
50  suffix, \
51  boost::lambda::bind( \
52  &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(&cls::member1)>::Type::member2, \
53  boost::lambda::bind(&cls::member1, boost::lambda::_1)), descriptions))
54 
55 #define MEMBER_ENUM_3(suffix, cls, member1, member2, member3, descriptions) \
56  (new icl_core::config::MemberEnum< \
57  icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF( \
58  &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF( \
59  &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF( \
60  &cls::member1)>::Type::member2)>::Type::member3)>::Type, cls>( \
61  suffix, \
62  boost::lambda::bind( \
63  &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF( \
64  &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF( \
65  &cls::member1)>::Type::member2)>::Type::member3, \
66  boost::lambda::bind( \
67  &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(&cls::member1)>::Type::member2, \
68  boost::lambda::bind(&cls::member1, boost::lambda::_1))), descriptions))
69 
70 namespace icl_core {
71 namespace config {
72 
73 template<typename T, typename Q, typename V = int32_t>
75 {
76 public:
77  MemberEnum(std::string const & config_suffix,
78  boost::function<T&(Q&)> accessor,
79  char const * const * descriptions,
80  char const * end_marker = NULL)
81  : m_config_suffix(config_suffix),
82  m_accessor(accessor)
83  {
84  if (descriptions != NULL)
85  {
86  for (size_t i = 0;
87  ((end_marker == NULL) && (descriptions[i] != NULL)) ||
88  ((end_marker != NULL) && (::strcmp(descriptions[i], end_marker) != 0));
89  ++i)
90  {
91  m_descriptions.push_back(std::string(descriptions[i]));
92  }
93  }
94  }
95  MemberEnum(std::string const & config_suffix,
96  boost::function<T&(Q&)> accessor,
97  std::vector<std::string> const & descriptions)
98  : m_config_suffix(config_suffix),
99  m_accessor(accessor)
100  {
101  std::copy(descriptions.begin(), descriptions.end(), std::back_inserter(m_descriptions));
102  }
103  virtual ~MemberEnum() {}
104 
105  virtual bool get(std::string const & key,
106  typename icl_core::ConvertToRef<Q>::ToRef value) const
107  {
108  bool result = false;
110  {
111  V raw_value;
113  {
114  m_accessor(value) = T(raw_value);
115  result = true;
116  }
117  else
118  {
119  result = false;
120  }
121  }
122  else
123  {
124  result = false;
125  }
126  return result;
127  }
128 
129  virtual std::string getSuffix() const { return m_config_suffix; }
130  virtual std::string getStringValue() const { return m_str_value; }
131 
132 private:
133  std::string m_config_suffix;
134  boost::function<T&(Q&)> m_accessor;
135  std::vector<std::string> m_descriptions;
136  mutable std::string m_str_value;
137 };
138 
139 }}
140 
141 #endif
Helper definitions for template programming.
virtual std::string getStringValue() const
Definition: MemberEnum.h:130
MemberEnum(std::string const &config_suffix, boost::function< T &(Q &)> accessor, std::vector< std::string > const &descriptions)
Definition: MemberEnum.h:95
static ConfigManager & instance()
bool string2Enum(const String &str, int32_t &value, const char *const *descriptions, const char *end_marker)
Definition: EnumHelper.cpp:28
virtual std::string getSuffix() const
Definition: MemberEnum.h:129
MemberEnum(std::string const &config_suffix, boost::function< T &(Q &)> accessor, char const *const *descriptions, char const *end_marker=NULL)
Definition: MemberEnum.h:77
bool get(const String &key, typename ConvertToRef< T >::ToRef value) const
boost::function< T &(Q &)> m_accessor
Definition: MemberEnum.h:134
Contains ConfigManager.
std::vector< std::string > m_descriptions
Definition: MemberEnum.h:135


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58