2 * Copyright Andrey Semashev 2007 - 2015.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
8 * \file attribute_set.hpp
9 * \author Andrey Semashev
12 * This header contains definition of the attribute set container.
15 #ifndef BOOST_LOG_ATTRIBUTE_SET_HPP_INCLUDED_
16 #define BOOST_LOG_ATTRIBUTE_SET_HPP_INCLUDED_
21 #include <boost/move/core.hpp>
22 #include <boost/core/enable_if.hpp>
23 #include <boost/type_traits/conditional.hpp>
24 #include <boost/log/detail/config.hpp>
25 #include <boost/log/detail/sfinae_tools.hpp>
26 #include <boost/log/attributes/attribute_name.hpp>
27 #include <boost/log/attributes/attribute.hpp>
28 #include <boost/log/detail/header.hpp>
30 #ifdef BOOST_HAS_PRAGMA_ONCE
36 BOOST_LOG_OPEN_NAMESPACE
39 class attribute_value_set;
43 //! Reference proxy object to implement \c operator[]
44 class attribute_set_reference_proxy
48 typedef attribute_name key_type;
49 //! Mapped attribute type
50 typedef attribute mapped_type;
53 attribute_set* const m_pContainer;
58 explicit attribute_set_reference_proxy(attribute_set* pContainer, key_type const& key) BOOST_NOEXCEPT :
59 m_pContainer(pContainer),
64 //! Conversion operator (would be invoked in case of reading from the container)
65 BOOST_FORCEINLINE operator mapped_type() const BOOST_NOEXCEPT
67 return read_mapped_value();
69 //! Assignment operator (would be invoked in case of writing to the container)
70 mapped_type& operator= (mapped_type const& val) const;
73 //! Reads the referenced mapped value from the container
74 mapped_type read_mapped_value() const BOOST_NOEXCEPT;
80 * \brief An attribute set class.
82 * An attribute set is an associative container with attribute name as a key and
83 * pointer to the attribute as a mapped value. The container allows storing only one element for each distinct
84 * key value. In most regards attribute set container provides interface similar to \c std::unordered_map.
85 * However, there are differences in \c operator[] semantics and a number of optimizations with regard to iteration.
86 * Besides, attribute names are stored as a read-only <tt>attribute_name</tt>'s instead of \c std::string,
87 * which saves memory and CPU time.
91 BOOST_COPYABLE_AND_MOVABLE_ALT(attribute_set)
93 friend class attribute_value_set;
94 friend class aux::attribute_set_reference_proxy;
98 typedef attribute_name key_type;
99 //! Mapped attribute type
100 typedef attribute mapped_type;
103 typedef std::pair< const key_type, mapped_type > value_type;
105 typedef value_type& reference;
106 //! Const reference type
107 typedef value_type const& const_reference;
109 typedef value_type* pointer;
110 //! Const pointer type
111 typedef value_type const* const_pointer;
113 typedef std::size_t size_type;
115 typedef std::ptrdiff_t difference_type;
121 struct implementation;
122 friend struct implementation;
124 //! A base class for the container nodes
132 BOOST_DELETED_FUNCTION(node_base(node_base const&))
133 BOOST_DELETED_FUNCTION(node_base& operator= (node_base const&))
136 //! Container elements
144 node(key_type const& key, mapped_type const& data);
148 #ifndef BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS
149 template< bool fConstV > class iter;
150 template< bool fConstV > friend class iter;
152 template< bool fConstV >
155 friend class iter< !fConstV >;
156 friend class attribute_set;
160 typedef attribute_set::difference_type difference_type;
161 typedef attribute_set::value_type value_type;
162 typedef typename boost::conditional<
164 attribute_set::const_reference,
165 attribute_set::reference
167 typedef typename boost::conditional<
169 attribute_set::const_pointer,
170 attribute_set::pointer
172 typedef std::bidirectional_iterator_tag iterator_category;
176 BOOST_CONSTEXPR iter() BOOST_NOEXCEPT : m_pNode(NULL) {}
177 explicit iter(node_base* pNode) BOOST_NOEXCEPT : m_pNode(pNode) {}
178 #if !defined(BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS)
179 template< bool fOtherConstV, typename = typename boost::enable_if_c< !fOtherConstV && fOtherConstV != fConstV >::type >
180 iter(iter< fOtherConstV > const& that) BOOST_NOEXCEPT : m_pNode(that.m_pNode) {}
182 template< bool fOtherConstV >
183 iter(iter< fOtherConstV > const& that, typename boost::enable_if_c< !fOtherConstV && fOtherConstV != fConstV, boost::log::aux::sfinae_dummy >::type = boost::log::aux::sfinae_dummy()) BOOST_NOEXCEPT :
184 m_pNode(that.m_pNode)
190 template< bool fOtherConstV >
191 typename boost::enable_if_c< !fOtherConstV && fOtherConstV != fConstV, iter& >::type operator= (iter< fOtherConstV > const& that) BOOST_NOEXCEPT
193 m_pNode = that.m_pNode;
198 template< bool fOtherConstV >
199 typename boost::enable_if_c< !fOtherConstV || fOtherConstV == fConstV, bool >::type operator== (iter< fOtherConstV > const& that) const BOOST_NOEXCEPT { return (m_pNode == that.m_pNode); }
200 template< bool fOtherConstV >
201 typename boost::enable_if_c< !fOtherConstV || fOtherConstV == fConstV, bool >::type operator!= (iter< fOtherConstV > const& that) const BOOST_NOEXCEPT { return (m_pNode != that.m_pNode); }
204 iter& operator++ () BOOST_NOEXCEPT
206 m_pNode = m_pNode->m_pNext;
209 iter& operator-- () BOOST_NOEXCEPT
211 m_pNode = m_pNode->m_pPrev;
214 iter operator++ (int) BOOST_NOEXCEPT
217 m_pNode = m_pNode->m_pNext;
220 iter operator-- (int) BOOST_NOEXCEPT
223 m_pNode = m_pNode->m_pPrev;
228 pointer operator-> () const BOOST_NOEXCEPT { return &(static_cast< node* >(m_pNode)->m_Value); }
229 reference operator* () const BOOST_NOEXCEPT { return static_cast< node* >(m_pNode)->m_Value; }
231 node_base* base() const BOOST_NOEXCEPT { return m_pNode; }
240 #ifndef BOOST_LOG_DOXYGEN_PASS
242 typedef iter< false > iterator;
243 //! Const iterator type
244 typedef iter< true > const_iterator;
247 * Iterator type. The iterator complies to the bidirectional iterator requirements.
249 typedef implementation_defined iterator;
251 * Constant iterator type. The iterator complies to the bidirectional iterator requirements with read-only capabilities.
253 typedef implementation_defined const_iterator;
254 #endif // BOOST_LOG_DOXYGEN_PASS
257 //! Pointer to implementation
258 implementation* m_pImpl;
262 * Default constructor.
264 * \post <tt>empty() == true</tt>
266 BOOST_LOG_API attribute_set();
271 * \post <tt>size() == that.size() && std::equal(begin(), end(), that.begin()) == true</tt>
273 BOOST_LOG_API attribute_set(attribute_set const& that);
278 attribute_set(BOOST_RV_REF(attribute_set) that) BOOST_NOEXCEPT : m_pImpl(that.m_pImpl)
284 * Destructor. All stored references to attributes are released.
286 BOOST_LOG_API ~attribute_set() BOOST_NOEXCEPT;
289 * Copy assignment operator.
291 * \post <tt>size() == that.size() && std::equal(begin(), end(), that.begin()) == true</tt>
293 attribute_set& operator= (attribute_set that) BOOST_NOEXCEPT
300 * Swaps two instances of the container.
302 * \b Throws: Nothing.
304 void swap(attribute_set& that) BOOST_NOEXCEPT
306 implementation* const p = m_pImpl;
307 m_pImpl = that.m_pImpl;
312 * \return Iterator to the first element of the container.
314 BOOST_LOG_API iterator begin() BOOST_NOEXCEPT;
316 * \return Iterator to the after-the-last element of the container.
318 BOOST_LOG_API iterator end() BOOST_NOEXCEPT;
320 * \return Constant iterator to the first element of the container.
322 BOOST_LOG_API const_iterator begin() const BOOST_NOEXCEPT;
324 * \return Constant iterator to the after-the-last element of the container.
326 BOOST_LOG_API const_iterator end() const BOOST_NOEXCEPT;
329 * \return Number of elements in the container.
331 BOOST_LOG_API size_type size() const BOOST_NOEXCEPT;
333 * \return true if there are no elements in the container, false otherwise.
335 bool empty() const BOOST_NOEXCEPT { return (this->size() == 0); }
338 * The method finds the attribute by name.
340 * \param key Attribute name.
341 * \return Iterator to the found element or end() if the attribute with such name is not found.
343 BOOST_LOG_API iterator find(key_type key) BOOST_NOEXCEPT;
345 * The method finds the attribute by name.
347 * \param key Attribute name.
348 * \return Iterator to the found element or \c end() if the attribute with such name is not found.
350 const_iterator find(key_type key) const BOOST_NOEXCEPT
352 return const_iterator(const_cast< attribute_set* >(this)->find(key));
355 * The method counts the number of the attribute occurrences in the container. Since there can be only one
356 * attribute with a particular key, the method always return 0 or 1.
358 * \param key Attribute name.
359 * \return The number of times the attribute is found in the container.
361 size_type count(key_type key) const BOOST_NOEXCEPT { return size_type(this->find(key) != this->end()); }
364 * Combined lookup/insertion operator. The operator semantics depends on the further usage of the returned reference.
365 * \li If the reference is used as an assignment target, the assignment expression is equivalent to element insertion,
366 * where the element is composed of the second argument of the \c operator[] as a key and the second argument of assignment
368 * \li If the returned reference is used in context where a conversion to the mapped type is required,
369 * the result of the conversion is equivalent to the mapped value found with the second argument of the \c operator[] as a key,
370 * if such an element exists in the container, or a default-constructed mapped value, if an element does not exist in the
373 * \param key Attribute name.
374 * \return A smart reference object of unspecified type.
376 aux::attribute_set_reference_proxy operator[] (key_type key) BOOST_NOEXCEPT
378 return aux::attribute_set_reference_proxy(this, key);
383 * \param key Attribute name.
384 * \return If an element with the corresponding attribute name is found in the container, its mapped value
385 * is returned. Otherwise a default-constructed mapped value is returned.
387 mapped_type operator[] (key_type key) const BOOST_NOEXCEPT
389 const_iterator it = this->find(key);
393 return mapped_type();
399 * \param key Attribute name.
400 * \param data Pointer to the attribute. Must not be NULL.
401 * \returns A pair of values. If second is true, the insertion succeeded and the first component points to the
402 * inserted element. Otherwise the first component points to the element that prevents insertion.
404 BOOST_LOG_API std::pair< iterator, bool > insert(key_type key, mapped_type const& data);
409 * \param value An element to be inserted.
410 * \returns A pair of values. If second is true, the insertion succeeded and the first component points to the
411 * inserted element. Otherwise the first component points to the element that prevents insertion.
413 std::pair< iterator, bool > insert(const_reference value)
415 return this->insert(value.first, value.second);
419 * Mass insertion method.
421 * \param begin A forward iterator that points to the first element to be inserted.
422 * \param end A forward iterator that points to the after-the-last element to be inserted.
424 template< typename FwdIteratorT >
425 void insert(FwdIteratorT begin, FwdIteratorT end)
427 for (; begin != end; ++begin)
428 this->insert(*begin);
432 * Mass insertion method with ability to acquire iterators to the inserted elements.
434 * \param begin A forward iterator that points to the first element to be inserted.
435 * \param end A forward iterator that points to the after-the-last element to be inserted.
436 * \param out An output iterator that receives results of insertion of the elements
438 template< typename FwdIteratorT, typename OutputIteratorT >
439 void insert(FwdIteratorT begin, FwdIteratorT end, OutputIteratorT out)
441 for (; begin != end; ++begin, ++out)
442 *out = this->insert(*begin);
446 * The method erases all attributes with the specified name
448 * \post All iterators to the erased elements become invalid.
449 * \param key Attribute name.
450 * \return Tne number of erased elements
452 BOOST_LOG_API size_type erase(key_type key) BOOST_NOEXCEPT;
454 * The method erases the specified attribute
456 * \post All iterators to the erased element become invalid.
457 * \param it A valid iterator to the element to be erased.
458 * \return Tne number of erased elements
460 BOOST_LOG_API void erase(iterator it) BOOST_NOEXCEPT;
462 * The method erases all attributes within the specified range
464 * \pre \a end is reachable from \a begin with a finite number of increments.
465 * \post All iterators to the erased elements become invalid.
466 * \param begin An iterator that points to the first element to be erased.
467 * \param end An iterator that points to the after-the-last element to be erased.
469 BOOST_LOG_API void erase(iterator begin, iterator end) BOOST_NOEXCEPT;
472 * The method removes all elements from the container
474 * \post <tt>empty() == true</tt>
476 BOOST_LOG_API void clear() BOOST_NOEXCEPT;
482 inline void swap(attribute_set& left, attribute_set& right) BOOST_NOEXCEPT
489 //! Reads the referenced mapped value from the container
490 inline attribute_set_reference_proxy::mapped_type attribute_set_reference_proxy::read_mapped_value() const BOOST_NOEXCEPT
492 attribute_set::iterator it = m_pContainer->find(m_key);
493 if (it != m_pContainer->end())
496 return mapped_type();
499 //! Assignment operator (would be invoked in case of writing to the container)
500 inline attribute_set_reference_proxy::mapped_type& attribute_set_reference_proxy::operator= (mapped_type const& val) const
502 std::pair< attribute_set::iterator, bool > res = m_pContainer->insert(m_key, val);
504 res.first->second = val;
505 return res.first->second;
510 #ifndef BOOST_LOG_DOXYGEN_PASS
511 inline attribute& attribute::operator= (aux::attribute_set_reference_proxy const& that) BOOST_NOEXCEPT
513 attribute attr = that;
519 BOOST_LOG_CLOSE_NAMESPACE // namespace log
523 #include <boost/log/detail/footer.hpp>
525 #endif // BOOST_LOG_ATTRIBUTE_SET_HPP_INCLUDED_