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/mpl/if.hpp>
22 #include <boost/move/core.hpp>
23 #include <boost/log/detail/config.hpp>
24 #include <boost/log/attributes/attribute_name.hpp>
25 #include <boost/log/attributes/attribute.hpp>
26 #include <boost/log/detail/header.hpp>
28 #ifdef BOOST_HAS_PRAGMA_ONCE
34 BOOST_LOG_OPEN_NAMESPACE
37 class attribute_value_set;
41 //! Reference proxy object to implement \c operator[]
42 class attribute_set_reference_proxy
46 typedef attribute_name key_type;
47 //! Mapped attribute type
48 typedef attribute mapped_type;
51 attribute_set* const m_pContainer;
56 explicit attribute_set_reference_proxy(attribute_set* pContainer, key_type const& key) BOOST_NOEXCEPT :
57 m_pContainer(pContainer),
62 //! Conversion operator (would be invoked in case of reading from the container)
63 BOOST_FORCEINLINE operator mapped_type() const BOOST_NOEXCEPT
65 return read_mapped_value();
67 //! Assignment operator (would be invoked in case of writing to the container)
68 mapped_type& operator= (mapped_type const& val) const;
71 //! Reads the referenced mapped value from the container
72 mapped_type read_mapped_value() const BOOST_NOEXCEPT;
78 * \brief An attribute set class.
80 * An attribute set is an associative container with attribute name as a key and
81 * pointer to the attribute as a mapped value. The container allows storing only one element for each distinct
82 * key value. In most regards attribute set container provides interface similar to \c std::unordered_map.
83 * However, there are differences in \c operator[] semantics and a number of optimizations with regard to iteration.
84 * Besides, attribute names are stored as a read-only <tt>attribute_name</tt>'s instead of \c std::string,
85 * which saves memory and CPU time.
89 BOOST_COPYABLE_AND_MOVABLE_ALT(attribute_set)
91 friend class attribute_value_set;
92 friend class aux::attribute_set_reference_proxy;
96 typedef attribute_name key_type;
97 //! Mapped attribute type
98 typedef attribute mapped_type;
101 typedef std::pair< const key_type, mapped_type > value_type;
103 typedef value_type& reference;
104 //! Const reference type
105 typedef value_type const& const_reference;
107 typedef value_type* pointer;
108 //! Const pointer type
109 typedef value_type const* const_pointer;
111 typedef std::size_t size_type;
113 typedef std::ptrdiff_t difference_type;
119 struct implementation;
120 friend struct implementation;
122 //! A base class for the container nodes
130 BOOST_DELETED_FUNCTION(node_base(node_base const&))
131 BOOST_DELETED_FUNCTION(node_base& operator= (node_base const&))
134 //! Container elements
142 node(key_type const& key, mapped_type const& data);
146 #ifndef BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS
147 template< bool fConstV > class iter;
148 template< bool fConstV > friend class iter;
150 template< bool fConstV >
153 friend class iter< !fConstV >;
154 friend class attribute_set;
158 typedef attribute_set::difference_type difference_type;
159 typedef attribute_set::value_type value_type;
160 typedef typename mpl::if_c<
162 attribute_set::const_reference,
163 attribute_set::reference
165 typedef typename mpl::if_c<
167 attribute_set::const_pointer,
168 attribute_set::pointer
170 typedef std::bidirectional_iterator_tag iterator_category;
174 BOOST_CONSTEXPR iter() : m_pNode(NULL) {}
175 explicit iter(node_base* pNode) BOOST_NOEXCEPT : m_pNode(pNode) {}
176 iter(iter< false > const& that) BOOST_NOEXCEPT : m_pNode(that.m_pNode) {}
180 iter& operator= (iter< f > const& that) BOOST_NOEXCEPT
182 m_pNode = that.m_pNode;
188 bool operator== (iter< f > const& that) const BOOST_NOEXCEPT { return (m_pNode == that.m_pNode); }
190 bool operator!= (iter< f > const& that) const BOOST_NOEXCEPT { return (m_pNode != that.m_pNode); }
193 iter& operator++ () BOOST_NOEXCEPT
195 m_pNode = m_pNode->m_pNext;
198 iter& operator-- () BOOST_NOEXCEPT
200 m_pNode = m_pNode->m_pPrev;
203 iter operator++ (int) BOOST_NOEXCEPT
206 m_pNode = m_pNode->m_pNext;
209 iter operator-- (int) BOOST_NOEXCEPT
212 m_pNode = m_pNode->m_pPrev;
217 pointer operator-> () const BOOST_NOEXCEPT { return &(static_cast< node* >(m_pNode)->m_Value); }
218 reference operator* () const BOOST_NOEXCEPT { return static_cast< node* >(m_pNode)->m_Value; }
220 node_base* base() const BOOST_NOEXCEPT { return m_pNode; }
229 #ifndef BOOST_LOG_DOXYGEN_PASS
231 typedef iter< false > iterator;
232 //! Const iterator type
233 typedef iter< true > const_iterator;
236 * Iterator type. The iterator complies to the bidirectional iterator requirements.
238 typedef implementation_defined iterator;
240 * Constant iterator type. The iterator complies to the bidirectional iterator requirements with read-only capabilities.
242 typedef implementation_defined const_iterator;
243 #endif // BOOST_LOG_DOXYGEN_PASS
246 //! Pointer to implementation
247 implementation* m_pImpl;
251 * Default constructor.
253 * \post <tt>empty() == true</tt>
255 BOOST_LOG_API attribute_set();
260 * \post <tt>size() == that.size() && std::equal(begin(), end(), that.begin()) == true</tt>
262 BOOST_LOG_API attribute_set(attribute_set const& that);
267 attribute_set(BOOST_RV_REF(attribute_set) that) BOOST_NOEXCEPT : m_pImpl(that.m_pImpl)
273 * Destructor. All stored references to attributes are released.
275 BOOST_LOG_API ~attribute_set() BOOST_NOEXCEPT;
278 * Copy assignment operator.
280 * \post <tt>size() == that.size() && std::equal(begin(), end(), that.begin()) == true</tt>
282 attribute_set& operator= (attribute_set that) BOOST_NOEXCEPT
289 * Swaps two instances of the container.
291 * \b Throws: Nothing.
293 void swap(attribute_set& that) BOOST_NOEXCEPT
295 implementation* const p = m_pImpl;
296 m_pImpl = that.m_pImpl;
301 * \return Iterator to the first element of the container.
303 BOOST_LOG_API iterator begin() BOOST_NOEXCEPT;
305 * \return Iterator to the after-the-last element of the container.
307 BOOST_LOG_API iterator end() BOOST_NOEXCEPT;
309 * \return Constant iterator to the first element of the container.
311 BOOST_LOG_API const_iterator begin() const BOOST_NOEXCEPT;
313 * \return Constant iterator to the after-the-last element of the container.
315 BOOST_LOG_API const_iterator end() const BOOST_NOEXCEPT;
318 * \return Number of elements in the container.
320 BOOST_LOG_API size_type size() const BOOST_NOEXCEPT;
322 * \return true if there are no elements in the container, false otherwise.
324 bool empty() const BOOST_NOEXCEPT { return (this->size() == 0); }
327 * The method finds the attribute by name.
329 * \param key Attribute name.
330 * \return Iterator to the found element or end() if the attribute with such name is not found.
332 BOOST_LOG_API iterator find(key_type key) BOOST_NOEXCEPT;
334 * The method finds the attribute by name.
336 * \param key Attribute name.
337 * \return Iterator to the found element or \c end() if the attribute with such name is not found.
339 const_iterator find(key_type key) const BOOST_NOEXCEPT
341 return const_iterator(const_cast< attribute_set* >(this)->find(key));
344 * The method counts the number of the attribute occurrences in the container. Since there can be only one
345 * attribute with a particular key, the method always return 0 or 1.
347 * \param key Attribute name.
348 * \return The number of times the attribute is found in the container.
350 size_type count(key_type key) const BOOST_NOEXCEPT { return size_type(this->find(key) != this->end()); }
353 * Combined lookup/insertion operator. The operator semantics depends on the further usage of the returned reference.
354 * \li If the reference is used as an assignment target, the assignment expression is equivalent to element insertion,
355 * where the element is composed of the second argument of the \c operator[] as a key and the second argument of assignment
357 * \li If the returned reference is used in context where a conversion to the mapped type is required,
358 * the result of the conversion is equivalent to the mapped value found with the second argument of the \c operator[] as a key,
359 * if such an element exists in the container, or a default-constructed mapped value, if an element does not exist in the
362 * \param key Attribute name.
363 * \return A smart reference object of unspecified type.
365 aux::attribute_set_reference_proxy operator[] (key_type key) BOOST_NOEXCEPT
367 return aux::attribute_set_reference_proxy(this, key);
372 * \param key Attribute name.
373 * \return If an element with the corresponding attribute name is found in the container, its mapped value
374 * is returned. Otherwise a default-constructed mapped value is returned.
376 mapped_type operator[] (key_type key) const BOOST_NOEXCEPT
378 const_iterator it = this->find(key);
382 return mapped_type();
388 * \param key Attribute name.
389 * \param data Pointer to the attribute. Must not be NULL.
390 * \returns A pair of values. If second is true, the insertion succeeded and the first component points to the
391 * inserted element. Otherwise the first component points to the element that prevents insertion.
393 BOOST_LOG_API std::pair< iterator, bool > insert(key_type key, mapped_type const& data);
398 * \param value An element to be inserted.
399 * \returns A pair of values. If second is true, the insertion succeeded and the first component points to the
400 * inserted element. Otherwise the first component points to the element that prevents insertion.
402 std::pair< iterator, bool > insert(const_reference value)
404 return this->insert(value.first, value.second);
408 * Mass insertion method.
410 * \param begin A forward iterator that points to the first element to be inserted.
411 * \param end A forward iterator that points to the after-the-last element to be inserted.
413 template< typename FwdIteratorT >
414 void insert(FwdIteratorT begin, FwdIteratorT end)
416 for (; begin != end; ++begin)
417 this->insert(*begin);
421 * Mass insertion method with ability to acquire iterators to the inserted elements.
423 * \param begin A forward iterator that points to the first element to be inserted.
424 * \param end A forward iterator that points to the after-the-last element to be inserted.
425 * \param out An output iterator that receives results of insertion of the elements
427 template< typename FwdIteratorT, typename OutputIteratorT >
428 void insert(FwdIteratorT begin, FwdIteratorT end, OutputIteratorT out)
430 for (; begin != end; ++begin, ++out)
431 *out = this->insert(*begin);
435 * The method erases all attributes with the specified name
437 * \post All iterators to the erased elements become invalid.
438 * \param key Attribute name.
439 * \return Tne number of erased elements
441 BOOST_LOG_API size_type erase(key_type key) BOOST_NOEXCEPT;
443 * The method erases the specified attribute
445 * \post All iterators to the erased element become invalid.
446 * \param it A valid iterator to the element to be erased.
447 * \return Tne number of erased elements
449 BOOST_LOG_API void erase(iterator it) BOOST_NOEXCEPT;
451 * The method erases all attributes within the specified range
453 * \pre \a end is reachable from \a begin with a finite number of increments.
454 * \post All iterators to the erased elements become invalid.
455 * \param begin An iterator that points to the first element to be erased.
456 * \param end An iterator that points to the after-the-last element to be erased.
458 BOOST_LOG_API void erase(iterator begin, iterator end) BOOST_NOEXCEPT;
461 * The method removes all elements from the container
463 * \post <tt>empty() == true</tt>
465 BOOST_LOG_API void clear() BOOST_NOEXCEPT;
471 inline void swap(attribute_set& left, attribute_set& right) BOOST_NOEXCEPT
478 //! Reads the referenced mapped value from the container
479 inline attribute_set_reference_proxy::mapped_type attribute_set_reference_proxy::read_mapped_value() const BOOST_NOEXCEPT
481 attribute_set::iterator it = m_pContainer->find(m_key);
482 if (it != m_pContainer->end())
485 return mapped_type();
488 //! Assignment operator (would be invoked in case of writing to the container)
489 inline attribute_set_reference_proxy::mapped_type& attribute_set_reference_proxy::operator= (mapped_type const& val) const
491 std::pair< attribute_set::iterator, bool > res = m_pContainer->insert(m_key, val);
493 res.first->second = val;
494 return res.first->second;
499 #ifndef BOOST_LOG_DOXYGEN_PASS
500 inline attribute& attribute::operator= (aux::attribute_set_reference_proxy const& that) BOOST_NOEXCEPT
502 attribute attr = that;
508 BOOST_LOG_CLOSE_NAMESPACE // namespace log
512 #include <boost/log/detail/footer.hpp>
514 #endif // BOOST_LOG_ATTRIBUTE_SET_HPP_INCLUDED_