~ubuntu-branches/ubuntu/wily/davix/wily

« back to all changes in this revision

Viewing changes to deps/boost_intern/boost/chrono/io/utility/ios_base_state_ptr.hpp

  • Committer: Package Import Robot
  • Author(s): Mattias Ellert
  • Date: 2015-07-31 13:17:55 UTC
  • mfrom: (5.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20150731131755-mizprbmn7ogv33te
Tags: 0.4.1-1
* Update to version 0.4.1
* Implement Multi-Arch support

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//  boost/chrono/utility/ios_base_pword_ptr.hpp  ------------------------------------------------------------//
2
 
 
3
 
//  Copyright 2011 Vicente J. Botet Escriba
4
 
 
5
 
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
6
 
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
 
 
8
 
//  See http://www.boost.org/libs/chrono for documentation.
9
 
 
10
 
#ifndef BOOST_CHRONO_UTILITY_IOS_BASE_STATE_PTR_HPP
11
 
#define BOOST_CHRONO_UTILITY_IOS_BASE_STATE_PTR_HPP
12
 
 
13
 
#include <ios>
14
 
#include <boost/assert.hpp>
15
 
 
16
 
/**
17
 
 *
18
 
 
19
 
 
20
 
 */
21
 
namespace boost
22
 
{
23
 
  namespace chrono
24
 
  {
25
 
    namespace detail
26
 
    {
27
 
 
28
 
      /**
29
 
       * xalloc key holder.
30
 
       */
31
 
      template <typename T>
32
 
      struct xalloc_key_holder
33
 
      {
34
 
        static int value; //< the xalloc value associated to T.
35
 
        static bool initialized; //< whether the value has been initialized or not.
36
 
      };
37
 
 
38
 
      template <typename T>
39
 
      int xalloc_key_holder<T>::value = 0;
40
 
 
41
 
      template <typename T>
42
 
      bool xalloc_key_holder<T>::initialized = false;
43
 
 
44
 
    }
45
 
 
46
 
    /**
47
 
     * xalloc key initialiazer.
48
 
     *
49
 
     * Declare a static variable of this type to ensure that the xalloc_key_holder<T> is initialized correctly.
50
 
     */
51
 
    template <typename T>
52
 
    struct xalloc_key_initializer
53
 
    {
54
 
      xalloc_key_initializer()
55
 
      {
56
 
        if (!detail::xalloc_key_holder<T>::initialized)
57
 
        {
58
 
          detail::xalloc_key_holder<T>::value = std::ios_base::xalloc();
59
 
          detail::xalloc_key_holder<T>::initialized = true;
60
 
        }
61
 
      }
62
 
    };
63
 
    /**
64
 
     * @c ios_state_ptr is a smart pointer to a ios_base specific state.
65
 
     */
66
 
    template <typename Final, typename T>
67
 
    class ios_state_ptr
68
 
    {
69
 
      ios_state_ptr& operator=(ios_state_ptr const& rhs) ;
70
 
 
71
 
    public:
72
 
      /**
73
 
       * The pointee type
74
 
       */
75
 
      typedef T element_type;
76
 
      /**
77
 
       * Explicit constructor.
78
 
       * @param ios the ios
79
 
       * @Effects Constructs a @c ios_state_ptr by storing the associated @c ios.
80
 
       */
81
 
      explicit ios_state_ptr(std::ios_base& ios) :
82
 
        ios_(ios)
83
 
      {
84
 
      }
85
 
      /**
86
 
       * Nothing to do as xalloc index can not be removed.
87
 
       */
88
 
      ~ios_state_ptr()
89
 
      {
90
 
      }
91
 
 
92
 
      /**
93
 
       * @Effects Allocates the index if not already done.
94
 
       * Registers the callback responsible of maintaining the state pointer coherency, if not already done.
95
 
       * Retrieves the associated ios pointer
96
 
       * @return the retrieved pointer statically casted to const.
97
 
       */
98
 
      T const* get() const BOOST_NOEXCEPT
99
 
      {
100
 
        register_once(index(), ios_);
101
 
        void* &pw = ios_.pword(index());
102
 
        if (pw == 0)
103
 
        {
104
 
          return 0;
105
 
        }
106
 
        return static_cast<const T*> (pw);
107
 
      }
108
 
      /**
109
 
       * @Effects Allocates the index if not already done.
110
 
       * Registers the callback responsible of maintaining the state pointer coherency, if not already done.
111
 
       * Retrieves the associated ios pointer
112
 
       * @return the retrieved pointer.
113
 
       */
114
 
      T * get() BOOST_NOEXCEPT
115
 
      {
116
 
        register_once(index(), ios_);
117
 
        void* &pw = ios_.pword(index());
118
 
        if (pw == 0)
119
 
        {
120
 
          return 0;
121
 
        }
122
 
        return static_cast<T*> (pw);
123
 
      }
124
 
      /**
125
 
       * @Effects as if @c return get();
126
 
       * @return the retrieved pointer.
127
 
       */
128
 
      T * operator->()BOOST_NOEXCEPT
129
 
      {
130
 
        return get();
131
 
      }
132
 
      /**
133
 
       * @Effects as if @c return get();
134
 
       * @return the retrieved pointer.
135
 
       */
136
 
      T const * operator->() const BOOST_NOEXCEPT
137
 
      {
138
 
        return get();
139
 
      }
140
 
 
141
 
      /**
142
 
       * @Effects as if @c return *get();
143
 
       * @return a reference to the retrieved state.
144
 
       * @Remark The behavior is undefined if @c get()==0.
145
 
       */
146
 
      T & operator*() BOOST_NOEXCEPT
147
 
      {
148
 
        return *get();
149
 
      }
150
 
      /**
151
 
       * @Effects as if @c return *get();
152
 
       * @return a reference to the retrieved state.
153
 
       * @Remark The behavior is undefined if @c get()==0.
154
 
       */
155
 
      T const & operator *() const BOOST_NOEXCEPT
156
 
      {
157
 
        return *get();
158
 
      }
159
 
 
160
 
      /**
161
 
       * @Effects reset the current pointer after storing in a temporary variable the pointer to the current state.
162
 
       * @return the stored state pointer.
163
 
       */
164
 
      T * release() BOOST_NOEXCEPT
165
 
      {
166
 
        T const* f = get();
167
 
        reset();
168
 
        return f;
169
 
      }
170
 
 
171
 
      /**
172
 
       *
173
 
       * @param new_ptr the new pointer.
174
 
       * @Effects deletes the current state and replace it with the new one.
175
 
       */
176
 
      void reset(T* new_ptr = 0)BOOST_NOEXCEPT
177
 
      {
178
 
        register_once(index(), ios_);
179
 
        void*& pw = ios_.pword(index());
180
 
        delete static_cast<T*> (pw);
181
 
        pw = new_ptr;
182
 
      }
183
 
 
184
 
#if defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
185
 
        typedef T* (ios_state_ptr::*bool_type)();
186
 
        operator bool_type() const BOOST_NOEXCEPT
187
 
        {
188
 
            return (get()!=0)?&ios_state_ptr::release:0;
189
 
        }
190
 
        bool operator!() const BOOST_NOEXCEPT
191
 
        {
192
 
          return (get()==0)?&ios_state_ptr::release:0;
193
 
        }
194
 
#else
195
 
        /**
196
 
         * Explicit conversion to bool.
197
 
         */
198
 
        explicit operator bool() const BOOST_NOEXCEPT
199
 
        {
200
 
          return get()!=0;
201
 
        }
202
 
#endif
203
 
 
204
 
      std::ios_base& getios()BOOST_NOEXCEPT
205
 
      {
206
 
        return ios_;
207
 
      }
208
 
      std::ios_base& getios() const BOOST_NOEXCEPT
209
 
      {
210
 
        return ios_;
211
 
      }
212
 
      /**
213
 
       * Implicit conversion to the ios_base
214
 
       */
215
 
      operator std::ios_base&() BOOST_NOEXCEPT
216
 
      {
217
 
        return ios_;
218
 
      }
219
 
      /**
220
 
       * Implicit conversion to the ios_base const
221
 
       */
222
 
      operator std::ios_base&() const BOOST_NOEXCEPT
223
 
      {
224
 
        return ios_;
225
 
      }
226
 
    private:
227
 
      static inline bool is_registerd(std::ios_base& ios)
228
 
      {
229
 
        long iw = ios.iword(index());
230
 
        return (iw == 1);
231
 
      }
232
 
      static inline void set_registered(std::ios_base& ios)
233
 
      {
234
 
        long& iw = ios.iword(index());
235
 
        iw = 1;
236
 
      }
237
 
      static inline void callback(std::ios_base::event evt, std::ios_base& ios, int index)
238
 
      {
239
 
        switch (evt)
240
 
        {
241
 
        case std::ios_base::erase_event:
242
 
        {
243
 
          void*& pw = ios.pword(index);
244
 
          if (pw != 0)
245
 
          {
246
 
            T* ptr = static_cast<T*> (pw);
247
 
            delete ptr;
248
 
            pw = 0;
249
 
          }
250
 
          break;
251
 
        }
252
 
        case std::ios_base::copyfmt_event:
253
 
        {
254
 
          void*& pw = ios.pword(index);
255
 
          if (pw != 0)
256
 
          {
257
 
            pw = new T(*static_cast<T*> (pw));
258
 
          }
259
 
          break;
260
 
        }
261
 
        default:
262
 
          break;
263
 
        }
264
 
      }
265
 
 
266
 
      static inline int index()
267
 
      {
268
 
        return detail::xalloc_key_holder<Final>::value;
269
 
      }
270
 
 
271
 
      static inline void register_once(int indx, std::ios_base& ios)
272
 
      {
273
 
        // needs a mask registered
274
 
        if (!is_registerd(ios))
275
 
        {
276
 
          set_registered(ios);
277
 
          ios.register_callback(callback, indx);
278
 
        }
279
 
      }
280
 
 
281
 
 
282
 
    protected:
283
 
      std::ios_base& ios_;
284
 
      //static detail::xalloc_key_initializer<Final> xalloc_key_initializer_;
285
 
 
286
 
    };
287
 
    //template <typename Final, typename T>
288
 
    //detail::xalloc_key_initializer<Final> ios_state_ptr<Final,T>::xalloc_key_initializer_;
289
 
 
290
 
 
291
 
    /**
292
 
     * @c ios_state_not_null_ptr is a non null variant of @c ios_state_ptr.
293
 
     * @tparm T
294
 
     * @Requires @c T must be @c DefaultConstructible and @c HeapAllocatable
295
 
     */
296
 
    template <typename Final, typename T>
297
 
    class ios_state_not_null_ptr: public ios_state_ptr<Final, T>
298
 
    {
299
 
      typedef ios_state_ptr<Final, T> base_type;
300
 
    public:
301
 
      explicit ios_state_not_null_ptr(std::ios_base& ios) :
302
 
      base_type(ios)
303
 
      {
304
 
        if (this->get() == 0)
305
 
        {
306
 
          this->base_type::reset(new T());
307
 
        }
308
 
      }
309
 
      ~ios_state_not_null_ptr()
310
 
      {
311
 
      }
312
 
 
313
 
      void reset(T* new_value) BOOST_NOEXCEPT
314
 
      {
315
 
        BOOST_ASSERT(new_value!=0);
316
 
        this->base_type::reset(new_value);
317
 
      }
318
 
 
319
 
    };
320
 
 
321
 
    /**
322
 
     * This class is useful to associate some flags to an std::ios_base.
323
 
     */
324
 
    template <typename Final>
325
 
    class ios_flags
326
 
    {
327
 
    public:
328
 
      /**
329
 
       *
330
 
       * @param ios the associated std::ios_base.
331
 
       * @Postcondition <c>flags()==0</c>
332
 
       */
333
 
      explicit ios_flags(std::ios_base& ios) :
334
 
        ios_(ios)
335
 
      {
336
 
      }
337
 
      ~ios_flags()
338
 
      {
339
 
      }
340
 
      /**
341
 
       * @Returns The format control information.
342
 
       */
343
 
      long flags() const BOOST_NOEXCEPT
344
 
      {
345
 
        return value();
346
 
      }
347
 
 
348
 
      /**
349
 
       * @param v the new bit mask.
350
 
       * @Postcondition <c>v == flags()</c>.
351
 
       * @Returns The previous value of @c flags().
352
 
       */
353
 
      long flags(long v)BOOST_NOEXCEPT
354
 
      {
355
 
        long tmp = flags();
356
 
        ref() = v;
357
 
        return tmp;
358
 
      }
359
 
 
360
 
      /**
361
 
       * @param v the new value
362
 
       * @Effects: Sets @c v in @c flags().
363
 
       * @Returns: The previous value of @c flags().
364
 
       */
365
 
      long setf(long v)
366
 
      {
367
 
        long tmp = value();
368
 
        ref() |= v;
369
 
        return tmp;
370
 
      }
371
 
 
372
 
      /**
373
 
       * @param mask the bit mask to clear.
374
 
       * @Effects: Clears @c mask in @c flags().
375
 
       */
376
 
      void unsetf(long mask)
377
 
      {
378
 
        ref() &= ~mask;
379
 
      }
380
 
 
381
 
      /**
382
 
       *
383
 
       * @param v
384
 
       * @param mask
385
 
       * @Effects: Clears @c mask in @c flags(), sets <c>v & mask</c> in @c flags().
386
 
       * @Returns: The previous value of flags().
387
 
       */
388
 
      long setf(long v, long mask)
389
 
      {
390
 
        long tmp = value();
391
 
        unsetf(mask);
392
 
        ref() |= v & mask;
393
 
        return tmp;
394
 
      }
395
 
 
396
 
      /**
397
 
       * implicit conversion to the @c ios_base
398
 
       */
399
 
      operator std::ios_base&()BOOST_NOEXCEPT
400
 
      {
401
 
        return ios_;
402
 
      }
403
 
      /**
404
 
       * implicit conversion to the @c ios_base const
405
 
       */
406
 
      operator std::ios_base const&() const BOOST_NOEXCEPT
407
 
      {
408
 
        return ios_;
409
 
      }
410
 
    private:
411
 
      long value() const BOOST_NOEXCEPT
412
 
      {
413
 
        return ios_.iword(index());
414
 
      }
415
 
      long& ref()BOOST_NOEXCEPT
416
 
      {
417
 
        return ios_.iword(index());
418
 
      }
419
 
      static inline int index()
420
 
      {
421
 
        return detail::xalloc_key_holder<Final>::value;
422
 
      }
423
 
      ios_flags& operator=(ios_flags const& rhs) ;
424
 
 
425
 
      std::ios_base& ios_;
426
 
      //static detail::xalloc_key_initializer<Final> xalloc_key_initializer_;
427
 
 
428
 
    };
429
 
    //template <typename Final>
430
 
    //detail::xalloc_key_initializer<Final> ios_flags<Final>::xalloc_key_initializer_;
431
 
 
432
 
  } // namespace chrono
433
 
} // namespace boost
434
 
 
435
 
#endif // header