MemberEnum.h
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00022 //----------------------------------------------------------------------
00023 #ifndef ICL_CORE_CONFIG_MEMBER_ENUM_H_INCLUDED
00024 #define ICL_CORE_CONFIG_MEMBER_ENUM_H_INCLUDED
00025 
00026 #include "icl_core/RemoveMemberPointer.h"
00027 #include "icl_core/TemplateHelper.h"
00028 #include "icl_core_config/ConfigHelper.h"
00029 #include "icl_core_config/ConfigManager.h"
00030 #include "icl_core_config/MemberValueIface.h"
00031 
00032 #include <algorithm>
00033 #include <iterator>
00034 #include <string>
00035 #include <vector>
00036 #include <boost/function.hpp>
00037 #include <boost/lambda/bind.hpp>
00038 
00039 #define MEMBER_ENUM_1(suffix, cls, member1, descriptions)                                      \
00040   (new icl_core::config::MemberEnum<                                                           \
00041      icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(&cls::member1)>::Type, cls>(         \
00042      suffix,                                                                                   \
00043      boost::lambda::bind(&cls::member1, boost::lambda::_1), descriptions))
00044 
00045 #define MEMBER_ENUM_2(suffix, cls, member1, member2, descriptions)                             \
00046   (new icl_core::config::MemberEnum<                                                           \
00047      icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(                                     \
00048        &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(                                  \
00049          &cls::member1)>::Type::member2)>::Type, cls>(                                         \
00050      suffix,                                                                                   \
00051      boost::lambda::bind(                                                                      \
00052        &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(&cls::member1)>::Type::member2,   \
00053        boost::lambda::bind(&cls::member1, boost::lambda::_1)), descriptions))
00054 
00055 #define MEMBER_ENUM_3(suffix, cls, member1, member2, member3, descriptions)                    \
00056   (new icl_core::config::MemberEnum<                                                           \
00057      icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(                                     \
00058        &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(                                  \
00059          &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(                                \
00060            &cls::member1)>::Type::member2)>::Type::member3)>::Type, cls>(                      \
00061      suffix,                                                                                   \
00062      boost::lambda::bind(                                                                      \
00063        &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(                                  \
00064          &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(                                \
00065            &cls::member1)>::Type::member2)>::Type::member3,                                    \
00066        boost::lambda::bind(                                                                    \
00067          &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(&cls::member1)>::Type::member2, \
00068          boost::lambda::bind(&cls::member1, boost::lambda::_1))), descriptions))
00069 
00070 namespace icl_core {
00071 namespace config {
00072 
00073 template<typename T, typename Q, typename V = int32_t>
00074 class MemberEnum : public impl::MemberValueIface<Q>
00075 {
00076 public:
00077   MemberEnum(std::string const & config_suffix,
00078              boost::function<T&(Q&)> accessor,
00079              char const * const * descriptions,
00080              char const * end_marker = NULL)
00081     : m_config_suffix(config_suffix),
00082       m_accessor(accessor)
00083   {
00084     if (descriptions != NULL)
00085     {
00086       for (size_t i = 0;
00087            ((end_marker == NULL) && (descriptions[i] != NULL)) ||
00088             ((end_marker != NULL) && (::strcmp(descriptions[i], end_marker) != 0));
00089            ++i)
00090       {
00091         m_descriptions.push_back(std::string(descriptions[i]));
00092       }
00093     }
00094   }
00095   MemberEnum(std::string const & config_suffix,
00096              boost::function<T&(Q&)> accessor,
00097              std::vector<std::string> const & descriptions)
00098     : m_config_suffix(config_suffix),
00099       m_accessor(accessor)
00100   {
00101     std::copy(descriptions.begin(), descriptions.end(), std::back_inserter(m_descriptions));
00102   }
00103   virtual ~MemberEnum() {}
00104 
00105   virtual bool get(std::string const & key,
00106                    typename icl_core::ConvertToRef<Q>::ToRef value) const
00107   {
00108     bool result = false;
00109     if (ConfigManager::instance().get(key, m_str_value))
00110     {
00111       V raw_value;
00112       if (icl_core::string2Enum(m_str_value, raw_value, m_descriptions))
00113       {
00114         m_accessor(value) = T(raw_value);
00115         result = true;
00116       }
00117       else
00118       {
00119         result = false;
00120       }
00121     }
00122     else
00123     {
00124       result = false;
00125     }
00126     return result;
00127   }
00128 
00129   virtual std::string getSuffix() const { return m_config_suffix; }
00130   virtual std::string getStringValue() const { return m_str_value; }
00131 
00132 private:
00133   std::string m_config_suffix;
00134   boost::function<T&(Q&)> m_accessor;
00135   std::vector<std::string> m_descriptions;
00136   mutable std::string m_str_value;
00137 };
00138 
00139 }}
00140 
00141 #endif


fzi_icl_core
Author(s):
autogenerated on Tue Aug 8 2017 02:28:03