mutable_constant.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 mutable_constant.hpp
  9. * \author Andrey Semashev
  10. * \date 06.11.2007
  11. *
  12. * The header contains implementation of a mutable constant attribute.
  13. */
  14. #ifndef BOOST_LOG_ATTRIBUTES_MUTABLE_CONSTANT_HPP_INCLUDED_
  15. #define BOOST_LOG_ATTRIBUTES_MUTABLE_CONSTANT_HPP_INCLUDED_
  16. #include <boost/smart_ptr/intrusive_ptr.hpp>
  17. #include <boost/move/core.hpp>
  18. #include <boost/move/utility_core.hpp>
  19. #include <boost/type_traits/is_void.hpp>
  20. #include <boost/type_traits/conditional.hpp>
  21. #include <boost/log/detail/config.hpp>
  22. #include <boost/log/detail/locks.hpp>
  23. #include <boost/log/attributes/attribute.hpp>
  24. #include <boost/log/attributes/attribute_cast.hpp>
  25. #include <boost/log/attributes/attribute_value_impl.hpp>
  26. #include <boost/log/detail/header.hpp>
  27. #ifdef BOOST_HAS_PRAGMA_ONCE
  28. #pragma once
  29. #endif
  30. namespace boost {
  31. BOOST_LOG_OPEN_NAMESPACE
  32. namespace attributes {
  33. /*!
  34. * \brief A class of an attribute that holds a single constant value with ability to change it
  35. *
  36. * The mutable_constant attribute stores a single value of type, specified as the first template argument.
  37. * This value is returned on each attribute value acquisition.
  38. *
  39. * The attribute also allows to modify the stored value, even if the attribute is registered in an attribute set.
  40. * In order to ensure thread safety of such modifications the \c mutable_constant class is also parametrized
  41. * with three additional template arguments: mutex type, scoped write and scoped read lock types. If not specified,
  42. * the lock types are automatically deduced based on the mutex type.
  43. *
  44. * The implementation may avoid using these types to actually create and use the mutex, if a more efficient synchronization method is
  45. * available (such as atomic operations on the value type). By default no synchronization is done.
  46. */
  47. #ifdef BOOST_LOG_DOXYGEN_PASS
  48. template< typename T, typename MutexT = void, typename ScopedWriteLockT = auto, typename ScopedReadLockT = auto >
  49. #else // BOOST_LOG_DOXYGEN_PASS
  50. template<
  51. typename T,
  52. typename MutexT = void,
  53. typename ScopedWriteLockT =
  54. #ifndef BOOST_LOG_NO_THREADS
  55. typename boost::conditional<
  56. boost::log::aux::is_exclusively_lockable< MutexT >::value,
  57. boost::log::aux::exclusive_lock_guard< MutexT >,
  58. void
  59. >::type,
  60. #else
  61. void,
  62. #endif // BOOST_LOG_NO_THREADS
  63. typename ScopedReadLockT =
  64. #ifndef BOOST_LOG_NO_THREADS
  65. typename boost::conditional<
  66. boost::log::aux::is_shared_lockable< MutexT >::value,
  67. boost::log::aux::shared_lock_guard< MutexT >,
  68. ScopedWriteLockT
  69. >::type
  70. #else
  71. ScopedWriteLockT
  72. #endif // BOOST_LOG_NO_THREADS
  73. #endif // BOOST_LOG_DOXYGEN_PASS
  74. >
  75. class mutable_constant :
  76. public attribute
  77. {
  78. public:
  79. //! The attribute value type
  80. typedef T value_type;
  81. protected:
  82. //! Factory implementation
  83. class BOOST_SYMBOL_VISIBLE impl :
  84. public attribute::impl
  85. {
  86. private:
  87. //! Mutex type
  88. typedef MutexT mutex_type;
  89. //! Shared lock type
  90. typedef ScopedReadLockT scoped_read_lock;
  91. //! Exclusive lock type
  92. typedef ScopedWriteLockT scoped_write_lock;
  93. static_assert(!(is_void< mutex_type >::value || is_void< scoped_read_lock >::value || is_void< scoped_write_lock >::value), "Boost.Log: Mutex and both lock types either must not be void or must all be void");
  94. //! Attribute value wrapper
  95. typedef attribute_value_impl< value_type > attr_value;
  96. private:
  97. //! Thread protection mutex
  98. mutable mutex_type m_Mutex;
  99. //! Pointer to the actual attribute value
  100. intrusive_ptr< attr_value > m_Value;
  101. public:
  102. /*!
  103. * Initializing constructor
  104. */
  105. explicit impl(value_type const& value) : m_Value(new attr_value(value))
  106. {
  107. }
  108. /*!
  109. * Initializing constructor
  110. */
  111. explicit impl(BOOST_RV_REF(value_type) value) : m_Value(new attr_value(boost::move(value)))
  112. {
  113. }
  114. attribute_value get_value()
  115. {
  116. scoped_read_lock lock(m_Mutex);
  117. return attribute_value(m_Value);
  118. }
  119. void set(value_type const& value)
  120. {
  121. intrusive_ptr< attr_value > p = new attr_value(value);
  122. scoped_write_lock lock(m_Mutex);
  123. m_Value.swap(p);
  124. }
  125. void set(BOOST_RV_REF(value_type) value)
  126. {
  127. intrusive_ptr< attr_value > p = new attr_value(boost::move(value));
  128. scoped_write_lock lock(m_Mutex);
  129. m_Value.swap(p);
  130. }
  131. value_type get() const
  132. {
  133. scoped_read_lock lock(m_Mutex);
  134. return m_Value->get();
  135. }
  136. };
  137. public:
  138. /*!
  139. * Constructor with the stored value initialization
  140. */
  141. explicit mutable_constant(value_type const& value) : attribute(new impl(value))
  142. {
  143. }
  144. /*!
  145. * Constructor with the stored value initialization
  146. */
  147. explicit mutable_constant(BOOST_RV_REF(value_type) value) : attribute(new impl(boost::move(value)))
  148. {
  149. }
  150. /*!
  151. * Constructor for casting support
  152. */
  153. explicit mutable_constant(cast_source const& source) : attribute(source.as< impl >())
  154. {
  155. }
  156. /*!
  157. * The method sets a new attribute value. The implementation exclusively locks the mutex in order
  158. * to protect the value assignment.
  159. */
  160. void set(value_type const& value)
  161. {
  162. get_impl()->set(value);
  163. }
  164. /*!
  165. * The method sets a new attribute value.
  166. */
  167. void set(BOOST_RV_REF(value_type) value)
  168. {
  169. get_impl()->set(boost::move(value));
  170. }
  171. /*!
  172. * The method acquires the current attribute value. The implementation non-exclusively locks the mutex in order
  173. * to protect the value acquisition.
  174. */
  175. value_type get() const
  176. {
  177. return get_impl()->get();
  178. }
  179. protected:
  180. /*!
  181. * \returns Pointer to the factory implementation
  182. */
  183. impl* get_impl() const
  184. {
  185. return static_cast< impl* >(attribute::get_impl());
  186. }
  187. };
  188. /*!
  189. * \brief Specialization for unlocked case
  190. *
  191. * This version of attribute does not perform thread synchronization to access the stored value.
  192. */
  193. template< typename T >
  194. class mutable_constant< T, void, void, void > :
  195. public attribute
  196. {
  197. public:
  198. //! The attribute value type
  199. typedef T value_type;
  200. protected:
  201. //! Factory implementation
  202. class BOOST_SYMBOL_VISIBLE impl :
  203. public attribute::impl
  204. {
  205. private:
  206. //! Attribute value wrapper
  207. typedef attribute_value_impl< value_type > attr_value;
  208. private:
  209. //! The actual value
  210. intrusive_ptr< attr_value > m_Value;
  211. public:
  212. /*!
  213. * Initializing constructor
  214. */
  215. explicit impl(value_type const& value) : m_Value(new attr_value(value))
  216. {
  217. }
  218. /*!
  219. * Initializing constructor
  220. */
  221. explicit impl(BOOST_RV_REF(value_type) value) : m_Value(new attr_value(boost::move(value)))
  222. {
  223. }
  224. attribute_value get_value()
  225. {
  226. return attribute_value(m_Value);
  227. }
  228. void set(value_type const& value)
  229. {
  230. m_Value = new attr_value(value);
  231. }
  232. void set(BOOST_RV_REF(value_type) value)
  233. {
  234. m_Value = new attr_value(boost::move(value));
  235. }
  236. value_type get() const
  237. {
  238. return m_Value->get();
  239. }
  240. };
  241. public:
  242. /*!
  243. * Constructor with the stored value initialization
  244. */
  245. explicit mutable_constant(value_type const& value) : attribute(new impl(value))
  246. {
  247. }
  248. /*!
  249. * Constructor with the stored value initialization
  250. */
  251. explicit mutable_constant(BOOST_RV_REF(value_type) value) : attribute(new impl(boost::move(value)))
  252. {
  253. }
  254. /*!
  255. * Constructor for casting support
  256. */
  257. explicit mutable_constant(cast_source const& source) : attribute(source.as< impl >())
  258. {
  259. }
  260. /*!
  261. * The method sets a new attribute value.
  262. */
  263. void set(value_type const& value)
  264. {
  265. get_impl()->set(value);
  266. }
  267. /*!
  268. * The method sets a new attribute value.
  269. */
  270. void set(BOOST_RV_REF(value_type) value)
  271. {
  272. get_impl()->set(boost::move(value));
  273. }
  274. /*!
  275. * The method acquires the current attribute value.
  276. */
  277. value_type get() const
  278. {
  279. return get_impl()->get();
  280. }
  281. protected:
  282. /*!
  283. * \returns Pointer to the factory implementation
  284. */
  285. impl* get_impl() const
  286. {
  287. return static_cast< impl* >(attribute::get_impl());
  288. }
  289. };
  290. } // namespace attributes
  291. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  292. } // namespace boost
  293. #include <boost/log/detail/footer.hpp>
  294. #endif // BOOST_LOG_ATTRIBUTES_MUTABLE_CONSTANT_HPP_INCLUDED_