123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- #ifndef BOOST_LOG_SINKS_ATTRIBUTE_MAPPING_HPP_INCLUDED_
- #define BOOST_LOG_SINKS_ATTRIBUTE_MAPPING_HPP_INCLUDED_
- #include <map>
- #include <boost/log/detail/config.hpp>
- #include <boost/log/detail/tagged_integer.hpp>
- #include <boost/log/core/record_view.hpp>
- #include <boost/log/attributes/attribute_name.hpp>
- #include <boost/log/attributes/attribute_value_set.hpp>
- #include <boost/log/attributes/value_visitation.hpp>
- #include <boost/log/detail/header.hpp>
- #ifdef BOOST_HAS_PRAGMA_ONCE
- #pragma once
- #endif
- namespace boost {
- BOOST_LOG_OPEN_NAMESPACE
- namespace sinks {
- template< typename MappedT >
- struct basic_mapping
- {
-
- typedef MappedT mapped_type;
-
- typedef mapped_type result_type;
- };
- namespace aux {
-
- template< typename MappedT >
- struct direct_mapping_visitor
- {
- typedef void result_type;
- typedef MappedT mapped_type;
- explicit direct_mapping_visitor(mapped_type& extracted) :
- m_Extracted(extracted)
- {
- }
- template< typename T >
- void operator() (T const& val) const
- {
- m_Extracted = mapped_type(val);
- }
- private:
- mapped_type& m_Extracted;
- };
-
- template< typename IntT, typename TagT >
- struct direct_mapping_visitor< boost::log::aux::tagged_integer< IntT, TagT > >
- {
- typedef void result_type;
- typedef boost::log::aux::tagged_integer< IntT, TagT > mapped_type;
- explicit direct_mapping_visitor(mapped_type& extracted) :
- m_Extracted(extracted)
- {
- }
- template< typename T >
- void operator() (T const& val) const
- {
- mapped_type v = { static_cast< IntT >(val) };
- m_Extracted = v;
- }
- private:
- mapped_type& m_Extracted;
- };
- }
- template< typename MappedT, typename AttributeValueT = int >
- class basic_direct_mapping :
- public basic_mapping< MappedT >
- {
-
- typedef basic_direct_mapping< MappedT > base_type;
- public:
-
- typedef AttributeValueT attribute_value_type;
-
- typedef typename base_type::mapped_type mapped_type;
- private:
-
- const attribute_name m_Name;
-
- value_visitor_invoker< attribute_value_type > m_Invoker;
-
- mapped_type m_DefaultValue;
- public:
-
- explicit basic_direct_mapping(attribute_name const& name, mapped_type const& default_value) :
- m_Name(name),
- m_DefaultValue(default_value)
- {
- }
-
- mapped_type operator() (record_view const& rec) const
- {
- mapped_type res = m_DefaultValue;
- aux::direct_mapping_visitor< mapped_type > vis(res);
- m_Invoker(m_Name, rec.attribute_values(), vis);
- return res;
- }
- };
- template< typename MappedT, typename AttributeValueT = int >
- class basic_custom_mapping :
- public basic_mapping< MappedT >
- {
-
- typedef basic_mapping< MappedT > base_type;
- public:
-
- typedef AttributeValueT attribute_value_type;
-
- typedef typename base_type::mapped_type mapped_type;
- private:
-
-
- typedef std::map< attribute_value_type, mapped_type > mapping_type;
-
- class reference_proxy;
- friend class reference_proxy;
- class reference_proxy
- {
- mapping_type& m_Mapping;
- attribute_value_type m_Key;
- public:
-
- reference_proxy(mapping_type& mapping, attribute_value_type const& key) : m_Mapping(mapping), m_Key(key) {}
-
- reference_proxy const& operator= (mapped_type const& val) const
- {
- m_Mapping[m_Key] = val;
- return *this;
- }
- };
-
- struct visitor;
- friend struct visitor;
- struct visitor
- {
- typedef void result_type;
- visitor(mapping_type const& mapping, mapped_type& extracted) :
- m_Mapping(mapping),
- m_Extracted(extracted)
- {
- }
- template< typename T >
- void operator() (T const& val) const
- {
- typename mapping_type::const_iterator it = m_Mapping.find(val);
- if (it != m_Mapping.end())
- m_Extracted = it->second;
- }
- private:
- mapping_type const& m_Mapping;
- mapped_type& m_Extracted;
- };
-
- private:
-
- const attribute_name m_Name;
-
- value_visitor_invoker< attribute_value_type > m_Invoker;
-
- mapped_type m_DefaultValue;
-
- mapping_type m_Mapping;
- public:
-
- explicit basic_custom_mapping(attribute_name const& name, mapped_type const& default_value) :
- m_Name(name),
- m_DefaultValue(default_value)
- {
- }
-
- mapped_type operator() (record_view const& rec) const
- {
- mapped_type res = m_DefaultValue;
- visitor vis(m_Mapping, res);
- m_Invoker(m_Name, rec.attribute_values(), vis);
- return res;
- }
-
- #ifndef BOOST_LOG_DOXYGEN_PASS
- reference_proxy operator[] (attribute_value_type const& key)
- #else
- implementation_defined operator[] (attribute_value_type const& key)
- #endif
- {
- return reference_proxy(m_Mapping, key);
- }
- };
- }
- BOOST_LOG_CLOSE_NAMESPACE
- }
- #include <boost/log/detail/footer.hpp>
- #endif
|