~ai.tron/armagetronad/0.4-winlibs-updated

« back to all changes in this revision

Viewing changes to boost/includes/boost/log/attributes/mutable_constant.hpp

  • Committer: Nik K.
  • Date: 2013-11-07 16:58:35 UTC
  • Revision ID: nik.karbaum@gmail.com-20131107165835-kq99jz23drfj4dkh
Forgot to add some files; here they are

Show diffs side-by-side

added added

removed removed

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