attribute_set.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  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)
  6. */
  7. /*!
  8. * \file attribute_set.hpp
  9. * \author Andrey Semashev
  10. * \date 08.03.2007
  11. *
  12. * This header contains definition of the attribute set container.
  13. */
  14. #ifndef BOOST_LOG_ATTRIBUTE_SET_HPP_INCLUDED_
  15. #define BOOST_LOG_ATTRIBUTE_SET_HPP_INCLUDED_
  16. #include <cstddef>
  17. #include <utility>
  18. #include <iterator>
  19. #include <boost/mpl/if.hpp>
  20. #include <boost/move/core.hpp>
  21. #include <boost/log/detail/config.hpp>
  22. #include <boost/log/attributes/attribute_name.hpp>
  23. #include <boost/log/attributes/attribute.hpp>
  24. #include <boost/log/detail/header.hpp>
  25. #ifdef BOOST_HAS_PRAGMA_ONCE
  26. #pragma once
  27. #endif
  28. namespace boost {
  29. BOOST_LOG_OPEN_NAMESPACE
  30. class attribute_set;
  31. class attribute_value_set;
  32. namespace aux {
  33. //! Reference proxy object to implement \c operator[]
  34. class attribute_set_reference_proxy
  35. {
  36. private:
  37. //! Key type
  38. typedef attribute_name key_type;
  39. //! Mapped attribute type
  40. typedef attribute mapped_type;
  41. private:
  42. attribute_set* const m_pContainer;
  43. const key_type m_key;
  44. public:
  45. //! Constructor
  46. explicit attribute_set_reference_proxy(attribute_set* pContainer, key_type const& key) BOOST_NOEXCEPT :
  47. m_pContainer(pContainer),
  48. m_key(key)
  49. {
  50. }
  51. //! Conversion operator (would be invoked in case of reading from the container)
  52. BOOST_FORCEINLINE operator mapped_type() const BOOST_NOEXCEPT
  53. {
  54. return read_mapped_value();
  55. }
  56. //! Assignment operator (would be invoked in case of writing to the container)
  57. mapped_type& operator= (mapped_type const& val) const;
  58. private:
  59. //! Reads the referenced mapped value from the container
  60. mapped_type read_mapped_value() const BOOST_NOEXCEPT;
  61. };
  62. } // namespace aux
  63. /*!
  64. * \brief An attribute set class.
  65. *
  66. * An attribute set is an associative container with attribute name as a key and
  67. * pointer to the attribute as a mapped value. The container allows storing only one element for each distinct
  68. * key value. In most regards attribute set container provides interface similar to \c std::unordered_map.
  69. * However, there are differences in \c operator[] semantics and a number of optimizations with regard to iteration.
  70. * Besides, attribute names are stored as a read-only <tt>attribute_name</tt>'s instead of \c std::string,
  71. * which saves memory and CPU time.
  72. */
  73. class attribute_set
  74. {
  75. BOOST_COPYABLE_AND_MOVABLE_ALT(attribute_set)
  76. friend class attribute_value_set;
  77. friend class aux::attribute_set_reference_proxy;
  78. public:
  79. //! Key type
  80. typedef attribute_name key_type;
  81. //! Mapped attribute type
  82. typedef attribute mapped_type;
  83. //! Value type
  84. typedef std::pair< const key_type, mapped_type > value_type;
  85. //! Reference type
  86. typedef value_type& reference;
  87. //! Const reference type
  88. typedef value_type const& const_reference;
  89. //! Pointer type
  90. typedef value_type* pointer;
  91. //! Const pointer type
  92. typedef value_type const* const_pointer;
  93. //! Size type
  94. typedef std::size_t size_type;
  95. //! Difference type
  96. typedef std::ptrdiff_t difference_type;
  97. private:
  98. //! \cond
  99. //! Implementation
  100. struct implementation;
  101. friend struct implementation;
  102. //! A base class for the container nodes
  103. struct node_base
  104. {
  105. node_base* m_pPrev;
  106. node_base* m_pNext;
  107. node_base();
  108. BOOST_DELETED_FUNCTION(node_base(node_base const&))
  109. BOOST_DELETED_FUNCTION(node_base& operator= (node_base const&))
  110. };
  111. //! Container elements
  112. struct node;
  113. friend struct node;
  114. struct node :
  115. public node_base
  116. {
  117. value_type m_Value;
  118. node(key_type const& key, mapped_type const& data);
  119. };
  120. //! Iterator class
  121. #ifndef BOOST_LOG_NO_MEMBER_TEMPLATE_FRIENDS
  122. template< bool fConstV > class iter;
  123. template< bool fConstV > friend class iter;
  124. #endif
  125. template< bool fConstV >
  126. class iter
  127. {
  128. friend class iter< !fConstV >;
  129. friend class attribute_set;
  130. public:
  131. // Standard typedefs
  132. typedef attribute_set::difference_type difference_type;
  133. typedef attribute_set::value_type value_type;
  134. typedef typename mpl::if_c<
  135. fConstV,
  136. attribute_set::const_reference,
  137. attribute_set::reference
  138. >::type reference;
  139. typedef typename mpl::if_c<
  140. fConstV,
  141. attribute_set::const_pointer,
  142. attribute_set::pointer
  143. >::type pointer;
  144. typedef std::bidirectional_iterator_tag iterator_category;
  145. public:
  146. // Constructors
  147. BOOST_CONSTEXPR iter() : m_pNode(NULL) {}
  148. explicit iter(node_base* pNode) BOOST_NOEXCEPT : m_pNode(pNode) {}
  149. iter(iter< false > const& that) BOOST_NOEXCEPT : m_pNode(that.m_pNode) {}
  150. //! Assignment
  151. template< bool f >
  152. iter& operator= (iter< f > const& that) BOOST_NOEXCEPT
  153. {
  154. m_pNode = that.m_pNode;
  155. return *this;
  156. }
  157. // Comparison
  158. template< bool f >
  159. bool operator== (iter< f > const& that) const BOOST_NOEXCEPT { return (m_pNode == that.m_pNode); }
  160. template< bool f >
  161. bool operator!= (iter< f > const& that) const BOOST_NOEXCEPT { return (m_pNode != that.m_pNode); }
  162. // Modification
  163. iter& operator++ () BOOST_NOEXCEPT
  164. {
  165. m_pNode = m_pNode->m_pNext;
  166. return *this;
  167. }
  168. iter& operator-- () BOOST_NOEXCEPT
  169. {
  170. m_pNode = m_pNode->m_pPrev;
  171. return *this;
  172. }
  173. iter operator++ (int) BOOST_NOEXCEPT
  174. {
  175. iter tmp(*this);
  176. m_pNode = m_pNode->m_pNext;
  177. return tmp;
  178. }
  179. iter operator-- (int) BOOST_NOEXCEPT
  180. {
  181. iter tmp(*this);
  182. m_pNode = m_pNode->m_pPrev;
  183. return tmp;
  184. }
  185. // Dereferencing
  186. pointer operator-> () const BOOST_NOEXCEPT { return &(static_cast< node* >(m_pNode)->m_Value); }
  187. reference operator* () const BOOST_NOEXCEPT { return static_cast< node* >(m_pNode)->m_Value; }
  188. node_base* base() const BOOST_NOEXCEPT { return m_pNode; }
  189. private:
  190. node_base* m_pNode;
  191. };
  192. //! \endcond
  193. public:
  194. #ifndef BOOST_LOG_DOXYGEN_PASS
  195. //! Iterator type
  196. typedef iter< false > iterator;
  197. //! Const iterator type
  198. typedef iter< true > const_iterator;
  199. #else
  200. /*!
  201. * Iterator type. The iterator complies to the bidirectional iterator requirements.
  202. */
  203. typedef implementation_defined iterator;
  204. /*!
  205. * Constant iterator type. The iterator complies to the bidirectional iterator requirements with read-only capabilities.
  206. */
  207. typedef implementation_defined const_iterator;
  208. #endif // BOOST_LOG_DOXYGEN_PASS
  209. private:
  210. //! Pointer to implementation
  211. implementation* m_pImpl;
  212. public:
  213. /*!
  214. * Default constructor.
  215. *
  216. * \post <tt>empty() == true</tt>
  217. */
  218. BOOST_LOG_API attribute_set();
  219. /*!
  220. * Copy constructor.
  221. *
  222. * \post <tt>size() == that.size() && std::equal(begin(), end(), that.begin()) == true</tt>
  223. */
  224. BOOST_LOG_API attribute_set(attribute_set const& that);
  225. /*!
  226. * Move constructor
  227. */
  228. attribute_set(BOOST_RV_REF(attribute_set) that) BOOST_NOEXCEPT : m_pImpl(that.m_pImpl)
  229. {
  230. that.m_pImpl = NULL;
  231. }
  232. /*!
  233. * Destructor. All stored references to attributes are released.
  234. */
  235. BOOST_LOG_API ~attribute_set() BOOST_NOEXCEPT;
  236. /*!
  237. * Copy assignment operator.
  238. *
  239. * \post <tt>size() == that.size() && std::equal(begin(), end(), that.begin()) == true</tt>
  240. */
  241. attribute_set& operator= (attribute_set that) BOOST_NOEXCEPT
  242. {
  243. this->swap(that);
  244. return *this;
  245. }
  246. /*!
  247. * Swaps two instances of the container.
  248. *
  249. * \b Throws: Nothing.
  250. */
  251. void swap(attribute_set& that) BOOST_NOEXCEPT
  252. {
  253. implementation* const p = m_pImpl;
  254. m_pImpl = that.m_pImpl;
  255. that.m_pImpl = p;
  256. }
  257. /*!
  258. * \return Iterator to the first element of the container.
  259. */
  260. BOOST_LOG_API iterator begin() BOOST_NOEXCEPT;
  261. /*!
  262. * \return Iterator to the after-the-last element of the container.
  263. */
  264. BOOST_LOG_API iterator end() BOOST_NOEXCEPT;
  265. /*!
  266. * \return Constant iterator to the first element of the container.
  267. */
  268. BOOST_LOG_API const_iterator begin() const BOOST_NOEXCEPT;
  269. /*!
  270. * \return Constant iterator to the after-the-last element of the container.
  271. */
  272. BOOST_LOG_API const_iterator end() const BOOST_NOEXCEPT;
  273. /*!
  274. * \return Number of elements in the container.
  275. */
  276. BOOST_LOG_API size_type size() const BOOST_NOEXCEPT;
  277. /*!
  278. * \return true if there are no elements in the container, false otherwise.
  279. */
  280. bool empty() const BOOST_NOEXCEPT { return (this->size() == 0); }
  281. /*!
  282. * The method finds the attribute by name.
  283. *
  284. * \param key Attribute name.
  285. * \return Iterator to the found element or end() if the attribute with such name is not found.
  286. */
  287. BOOST_LOG_API iterator find(key_type key) BOOST_NOEXCEPT;
  288. /*!
  289. * The method finds the attribute by name.
  290. *
  291. * \param key Attribute name.
  292. * \return Iterator to the found element or \c end() if the attribute with such name is not found.
  293. */
  294. const_iterator find(key_type key) const BOOST_NOEXCEPT
  295. {
  296. return const_iterator(const_cast< attribute_set* >(this)->find(key));
  297. }
  298. /*!
  299. * The method counts the number of the attribute occurrences in the container. Since there can be only one
  300. * attribute with a particular key, the method always return 0 or 1.
  301. *
  302. * \param key Attribute name.
  303. * \return The number of times the attribute is found in the container.
  304. */
  305. size_type count(key_type key) const BOOST_NOEXCEPT { return size_type(this->find(key) != this->end()); }
  306. /*!
  307. * Combined lookup/insertion operator. The operator semantics depends on the further usage of the returned reference.
  308. * \li If the reference is used as an assignment target, the assignment expression is equivalent to element insertion,
  309. * where the element is composed of the second argument of the \c operator[] as a key and the second argument of assignment
  310. * as a mapped value.
  311. * \li If the returned reference is used in context where a conversion to the mapped type is required,
  312. * the result of the conversion is equivalent to the mapped value found with the second argument of the \c operator[] as a key,
  313. * if such an element exists in the container, or a default-constructed mapped value, if an element does not exist in the
  314. * container.
  315. *
  316. * \param key Attribute name.
  317. * \return A smart reference object of unspecified type.
  318. */
  319. aux::attribute_set_reference_proxy operator[] (key_type key) BOOST_NOEXCEPT
  320. {
  321. return aux::attribute_set_reference_proxy(this, key);
  322. }
  323. /*!
  324. * Lookup operator
  325. *
  326. * \param key Attribute name.
  327. * \return If an element with the corresponding attribute name is found in the container, its mapped value
  328. * is returned. Otherwise a default-constructed mapped value is returned.
  329. */
  330. mapped_type operator[] (key_type key) const BOOST_NOEXCEPT
  331. {
  332. const_iterator it = this->find(key);
  333. if (it != end())
  334. return it->second;
  335. else
  336. return mapped_type();
  337. }
  338. /*!
  339. * Insertion method
  340. *
  341. * \param key Attribute name.
  342. * \param data Pointer to the attribute. Must not be NULL.
  343. * \returns A pair of values. If second is true, the insertion succeeded and the first component points to the
  344. * inserted element. Otherwise the first component points to the element that prevents insertion.
  345. */
  346. BOOST_LOG_API std::pair< iterator, bool > insert(key_type key, mapped_type const& data);
  347. /*!
  348. * Insertion method
  349. *
  350. * \param value An element to be inserted.
  351. * \returns A pair of values. If second is true, the insertion succeeded and the first component points to the
  352. * inserted element. Otherwise the first component points to the element that prevents insertion.
  353. */
  354. std::pair< iterator, bool > insert(const_reference value)
  355. {
  356. return this->insert(value.first, value.second);
  357. }
  358. /*!
  359. * Mass insertion method.
  360. *
  361. * \param begin A forward iterator that points to the first element to be inserted.
  362. * \param end A forward iterator that points to the after-the-last element to be inserted.
  363. */
  364. template< typename FwdIteratorT >
  365. void insert(FwdIteratorT begin, FwdIteratorT end)
  366. {
  367. for (; begin != end; ++begin)
  368. this->insert(*begin);
  369. }
  370. /*!
  371. * Mass insertion method with ability to acquire iterators to the inserted elements.
  372. *
  373. * \param begin A forward iterator that points to the first element to be inserted.
  374. * \param end A forward iterator that points to the after-the-last element to be inserted.
  375. * \param out An output iterator that receives results of insertion of the elements
  376. */
  377. template< typename FwdIteratorT, typename OutputIteratorT >
  378. void insert(FwdIteratorT begin, FwdIteratorT end, OutputIteratorT out)
  379. {
  380. for (; begin != end; ++begin, ++out)
  381. *out = this->insert(*begin);
  382. }
  383. /*!
  384. * The method erases all attributes with the specified name
  385. *
  386. * \post All iterators to the erased elements become invalid.
  387. * \param key Attribute name.
  388. * \return Tne number of erased elements
  389. */
  390. BOOST_LOG_API size_type erase(key_type key) BOOST_NOEXCEPT;
  391. /*!
  392. * The method erases the specified attribute
  393. *
  394. * \post All iterators to the erased element become invalid.
  395. * \param it A valid iterator to the element to be erased.
  396. * \return Tne number of erased elements
  397. */
  398. BOOST_LOG_API void erase(iterator it) BOOST_NOEXCEPT;
  399. /*!
  400. * The method erases all attributes within the specified range
  401. *
  402. * \pre \a end is reachable from \a begin with a finite number of increments.
  403. * \post All iterators to the erased elements become invalid.
  404. * \param begin An iterator that points to the first element to be erased.
  405. * \param end An iterator that points to the after-the-last element to be erased.
  406. */
  407. BOOST_LOG_API void erase(iterator begin, iterator end) BOOST_NOEXCEPT;
  408. /*!
  409. * The method removes all elements from the container
  410. *
  411. * \post <tt>empty() == true</tt>
  412. */
  413. BOOST_LOG_API void clear() BOOST_NOEXCEPT;
  414. };
  415. /*!
  416. * Free swap overload
  417. */
  418. inline void swap(attribute_set& left, attribute_set& right) BOOST_NOEXCEPT
  419. {
  420. left.swap(right);
  421. }
  422. namespace aux {
  423. //! Reads the referenced mapped value from the container
  424. inline attribute_set_reference_proxy::mapped_type attribute_set_reference_proxy::read_mapped_value() const BOOST_NOEXCEPT
  425. {
  426. attribute_set::iterator it = m_pContainer->find(m_key);
  427. if (it != m_pContainer->end())
  428. return it->second;
  429. else
  430. return mapped_type();
  431. }
  432. //! Assignment operator (would be invoked in case of writing to the container)
  433. inline attribute_set_reference_proxy::mapped_type& attribute_set_reference_proxy::operator= (mapped_type const& val) const
  434. {
  435. std::pair< attribute_set::iterator, bool > res = m_pContainer->insert(m_key, val);
  436. if (!res.second)
  437. res.first->second = val;
  438. return res.first->second;
  439. }
  440. } // namespace aux
  441. #ifndef BOOST_LOG_DOXYGEN_PASS
  442. inline attribute& attribute::operator= (aux::attribute_set_reference_proxy const& that) BOOST_NOEXCEPT
  443. {
  444. attribute attr = that;
  445. this->swap(attr);
  446. return *this;
  447. }
  448. #endif
  449. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  450. } // namespace boost
  451. #include <boost/log/detail/footer.hpp>
  452. #endif // BOOST_LOG_ATTRIBUTE_SET_HPP_INCLUDED_