MemberValue.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_VALUE_H_INCLUDED
00024 #define ICL_CORE_CONFIG_MEMBER_VALUE_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 #include "icl_core_config/Util.h"
00032 
00033 #include <string>
00034 #include <boost/function.hpp>
00035 #include <boost/lambda/bind.hpp>
00036 
00037 #define MEMBER_VALUE_1(suffix, cls, member1)                                                   \
00038   (new icl_core::config::MemberValue<                                                          \
00039      icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(&cls::member1)>::Type, cls>(         \
00040      suffix,                                                                                   \
00041      boost::lambda::bind(&cls::member1, boost::lambda::_1)))
00042 
00043 #define MEMBER_VALUE_2(suffix, cls, member1, member2)                                          \
00044   (new icl_core::config::MemberValue<                                                          \
00045      icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(                                     \
00046        &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(                                  \
00047          &cls::member1)>::Type::member2)>::Type, cls>(                                         \
00048      suffix,                                                                                   \
00049      boost::lambda::bind(                                                                      \
00050        &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(&cls::member1)>::Type::member2,   \
00051        boost::lambda::bind(&cls::member1, boost::lambda::_1))))
00052 
00053 #define MEMBER_VALUE_3(suffix, cls, member1, member2, member3)                                 \
00054   (new icl_core::config::MemberValue<                                                          \
00055      icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(                                     \
00056        &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(                                  \
00057          &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(                                \
00058            &cls::member1)>::Type::member2)>::Type::member3)>::Type, cls>(                      \
00059      suffix,                                                                                   \
00060      boost::lambda::bind(                                                                      \
00061        &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(                                  \
00062          &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(                                \
00063            &cls::member1)>::Type::member2)>::Type::member3,                                    \
00064        boost::lambda::bind(                                                                    \
00065          &icl_core::RemoveMemberPointer<ICL_CORE_CONFIG_TYPEOF(&cls::member1)>::Type::member2, \
00066          boost::lambda::bind(&cls::member1, boost::lambda::_1)))))
00067 
00068 namespace icl_core {
00069 namespace config {
00070 
00071 template<typename T, typename Q>
00072 class MemberValue : public impl::MemberValueIface<Q>
00073 {
00074 public:
00075   MemberValue(std::string const & config_suffix,
00076               boost::function<T&(Q&)> accessor)
00077     : m_config_suffix(config_suffix),
00078       m_accessor(accessor)
00079   {
00080   }
00081   virtual ~MemberValue() {}
00082 
00083   virtual bool get(std::string const & key,
00084                    typename icl_core::ConvertToRef<Q>::ToRef value) const
00085   {
00086     bool result = false;
00087     if (ConfigManager::instance().get(key, m_str_value))
00088     {
00089       try
00090       {
00091         m_accessor(value) = impl::hexical_cast<T>(m_str_value);
00092         result = true;
00093       }
00094       catch (...)
00095       {
00096         result = false;
00097       }
00098     }
00099     else
00100     {
00101       result = false;
00102     }
00103     return result;
00104   }
00105 
00106   virtual std::string getSuffix() const { return m_config_suffix; }
00107   virtual std::string getStringValue() const { return m_str_value; }
00108 
00109 private:
00110   std::string m_config_suffix;
00111   boost::function<T&(Q&)> m_accessor;
00112   mutable std::string m_str_value;
00113 };
00114 
00116 template<typename Q>
00117 class MemberValue<bool, Q> : public impl::MemberValueIface<Q>
00118 {
00119 public:
00120   MemberValue(std::string const & config_suffix,
00121               boost::function<bool&(Q&)> accessor)
00122     : m_config_suffix(config_suffix),
00123       m_accessor(accessor)
00124   {
00125   }
00126   virtual ~MemberValue() {}
00127 
00128   virtual bool get(std::string const & key,
00129                    typename icl_core::ConvertToRef<Q>::ToRef value) const
00130   {
00131     bool result = false;
00132     if (ConfigManager::instance().get(key, m_str_value))
00133     {
00134       try
00135       {
00136         m_accessor(value) = impl::strict_bool_cast(m_str_value);
00137         result = true;
00138       }
00139       catch (...)
00140       {
00141         result = false;
00142       }
00143     }
00144     else
00145     {
00146       result = false;
00147     }
00148     return result;
00149   }
00150 
00151   virtual std::string getSuffix() const { return m_config_suffix; }
00152   virtual std::string getStringValue() const { return m_str_value; }
00153 
00154 private:
00155   std::string m_config_suffix;
00156   boost::function<bool&(Q&)> m_accessor;
00157   mutable std::string m_str_value;
00158 };
00159 
00160 }}
00161 
00162 #endif


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