File indexing completed on 2025-01-18 09:39:24
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #ifndef BOOST_LOG_SINKS_ATTRIBUTE_MAPPING_HPP_INCLUDED_
0019 #define BOOST_LOG_SINKS_ATTRIBUTE_MAPPING_HPP_INCLUDED_
0020
0021 #include <map>
0022 #include <boost/log/detail/config.hpp>
0023 #include <boost/log/detail/tagged_integer.hpp>
0024 #include <boost/log/core/record_view.hpp>
0025 #include <boost/log/attributes/attribute_name.hpp>
0026 #include <boost/log/attributes/attribute_value_set.hpp>
0027 #include <boost/log/attributes/value_visitation.hpp>
0028 #include <boost/log/detail/header.hpp>
0029
0030 #ifdef BOOST_HAS_PRAGMA_ONCE
0031 #pragma once
0032 #endif
0033
0034 namespace boost {
0035
0036 BOOST_LOG_OPEN_NAMESPACE
0037
0038 namespace sinks {
0039
0040
0041 template< typename MappedT >
0042 struct basic_mapping
0043 {
0044
0045 typedef MappedT mapped_type;
0046
0047 typedef mapped_type result_type;
0048 };
0049
0050 namespace aux {
0051
0052
0053 template< typename MappedT >
0054 struct direct_mapping_visitor
0055 {
0056 typedef void result_type;
0057 typedef MappedT mapped_type;
0058
0059 explicit direct_mapping_visitor(mapped_type& extracted) :
0060 m_Extracted(extracted)
0061 {
0062 }
0063 template< typename T >
0064 void operator() (T const& val) const
0065 {
0066 m_Extracted = mapped_type(val);
0067 }
0068
0069 private:
0070 mapped_type& m_Extracted;
0071 };
0072
0073 template< typename IntT, typename TagT >
0074 struct direct_mapping_visitor< boost::log::aux::tagged_integer< IntT, TagT > >
0075 {
0076 typedef void result_type;
0077 typedef boost::log::aux::tagged_integer< IntT, TagT > mapped_type;
0078
0079 explicit direct_mapping_visitor(mapped_type& extracted) :
0080 m_Extracted(extracted)
0081 {
0082 }
0083 template< typename T >
0084 void operator() (T const& val) const
0085 {
0086 mapped_type v = { static_cast< IntT >(val) };
0087 m_Extracted = v;
0088 }
0089
0090 private:
0091 mapped_type& m_Extracted;
0092 };
0093
0094 }
0095
0096
0097
0098
0099
0100
0101
0102
0103 template< typename MappedT, typename AttributeValueT = int >
0104 class basic_direct_mapping :
0105 public basic_mapping< MappedT >
0106 {
0107
0108 typedef basic_direct_mapping< MappedT > base_type;
0109
0110 public:
0111
0112 typedef AttributeValueT attribute_value_type;
0113
0114 typedef typename base_type::mapped_type mapped_type;
0115
0116 private:
0117
0118 const attribute_name m_Name;
0119
0120 value_visitor_invoker< attribute_value_type > m_Invoker;
0121
0122 mapped_type m_DefaultValue;
0123
0124 public:
0125
0126
0127
0128
0129
0130
0131 explicit basic_direct_mapping(attribute_name const& name, mapped_type const& default_value) :
0132 m_Name(name),
0133 m_DefaultValue(default_value)
0134 {
0135 }
0136
0137
0138
0139
0140
0141
0142
0143 mapped_type operator() (record_view const& rec) const
0144 {
0145 mapped_type res = m_DefaultValue;
0146 aux::direct_mapping_visitor< mapped_type > vis(res);
0147 m_Invoker(m_Name, rec.attribute_values(), vis);
0148 return res;
0149 }
0150 };
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163 template< typename MappedT, typename AttributeValueT = int >
0164 class basic_custom_mapping :
0165 public basic_mapping< MappedT >
0166 {
0167
0168 typedef basic_mapping< MappedT > base_type;
0169
0170 public:
0171
0172 typedef AttributeValueT attribute_value_type;
0173
0174 typedef typename base_type::mapped_type mapped_type;
0175
0176 private:
0177
0178
0179
0180 typedef std::map< attribute_value_type, mapped_type > mapping_type;
0181
0182 class reference_proxy;
0183 friend class reference_proxy;
0184 class reference_proxy
0185 {
0186 mapping_type& m_Mapping;
0187 attribute_value_type m_Key;
0188
0189 public:
0190
0191 reference_proxy(mapping_type& mapping, attribute_value_type const& key) : m_Mapping(mapping), m_Key(key) {}
0192
0193 reference_proxy const& operator= (mapped_type const& val) const
0194 {
0195 m_Mapping[m_Key] = val;
0196 return *this;
0197 }
0198 };
0199
0200
0201 struct visitor;
0202 friend struct visitor;
0203 struct visitor
0204 {
0205 typedef void result_type;
0206
0207 visitor(mapping_type const& mapping, mapped_type& extracted) :
0208 m_Mapping(mapping),
0209 m_Extracted(extracted)
0210 {
0211 }
0212 template< typename T >
0213 void operator() (T const& val) const
0214 {
0215 typename mapping_type::const_iterator it = m_Mapping.find(val);
0216 if (it != m_Mapping.end())
0217 m_Extracted = it->second;
0218 }
0219
0220 private:
0221 mapping_type const& m_Mapping;
0222 mapped_type& m_Extracted;
0223 };
0224
0225
0226
0227 private:
0228
0229 const attribute_name m_Name;
0230
0231 value_visitor_invoker< attribute_value_type > m_Invoker;
0232
0233 mapped_type m_DefaultValue;
0234
0235 mapping_type m_Mapping;
0236
0237 public:
0238
0239
0240
0241
0242
0243
0244 explicit basic_custom_mapping(attribute_name const& name, mapped_type const& default_value) :
0245 m_Name(name),
0246 m_DefaultValue(default_value)
0247 {
0248 }
0249
0250
0251
0252
0253
0254
0255
0256
0257 mapped_type operator() (record_view const& rec) const
0258 {
0259 mapped_type res = m_DefaultValue;
0260 visitor vis(m_Mapping, res);
0261 m_Invoker(m_Name, rec.attribute_values(), vis);
0262 return res;
0263 }
0264
0265
0266
0267
0268
0269
0270
0271
0272 #ifndef BOOST_LOG_DOXYGEN_PASS
0273 reference_proxy operator[] (attribute_value_type const& key)
0274 #else
0275 implementation_defined operator[] (attribute_value_type const& key)
0276 #endif
0277 {
0278 return reference_proxy(m_Mapping, key);
0279 }
0280 };
0281
0282 }
0283
0284 BOOST_LOG_CLOSE_NAMESPACE
0285
0286 }
0287
0288 #include <boost/log/detail/footer.hpp>
0289
0290 #endif