~e7appew/ubuntu/vivid/bombono-dvd/mux-files-with-spaces-vivid

« back to all changes in this revision

Viewing changes to .pc/c8490d754d2e3936b1ee5c996b6bd662a36d2aeb.patch/src/mlib/ptr.h

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2013-05-02 22:09:31 UTC
  • Revision ID: package-import@ubuntu.com-20130502220931-pui7uqs6b468z8sb
Tags: 1.2.1-0ubuntu5
Cherry-pick patches from upstream to build against boost1.53.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// mlib/ptr.h
 
3
// This file is part of Bombono DVD project.
 
4
//
 
5
// Copyright (c) 2007-2008, 2010 Ilya Murav'jov
 
6
//
 
7
// This program is free software; you can redistribute it and/or modify
 
8
// it under the terms of the GNU General Public License as published by
 
9
// the Free Software Foundation; either version 2 of the License, or
 
10
// (at your option) any later version.
 
11
//
 
12
// This program is distributed in the hope that it will be useful,
 
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
// GNU General Public License for more details.
 
16
//
 
17
// You should have received a copy of the GNU General Public License
 
18
// along with this program; if not, write to the Free Software
 
19
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
20
// 
 
21
 
 
22
#ifndef __MLIB_PTR_H__
 
23
#define __MLIB_PTR_H__
 
24
 
 
25
#include <memory>
 
26
#include <boost/smart_ptr.hpp>
 
27
 
 
28
//
 
29
//  Умные указатели: ptr::one и ptr::shared
 
30
//
 
31
// В силу нескольких причин, указанных ниже, использовать
 
32
// общеизвестные std::auto_ptr и boost::shared_ptr, неудобно;
 
33
// потому здесь определены их замены, ptr::one и ptr::shared.
 
34
// Однако синтаксис их api точно такой, же что и оригиналов,- 
 
35
//      get(), reset(), release(), use_count(), ...
 
36
// 
 
37
// Причины:
 
38
// 1) std::auto_ptr неудобно использовать, потому что конструкция
 
39
//      if( ptr ) ...
 
40
//    не скомпилируется; то же самое со boost::scoped_ptr - бедный интерфейс
 
41
// 2) почему ptr::one, а не ptr::auto? - Слово "auto" зарезервировано
 
42
//    в языке C, хотя сейчас и не имеет никакого значения (исторически
 
43
//    сложилось). Потому подобрано (короткое) слово "one" - "владеть одному, 
 
44
//    единолично", в противовес "shared" - "общее владение".
 
45
// 3) boost::shared_ptr "не хватает" возможности присваивать обычный указатель,-
 
46
//      boost::shared_ptr<obj> s_ptr;
 
47
//      s_ptr = (obj*)0;                   // ошибка компиляции
 
48
// 
 
49
 
 
50
namespace ptr {
 
51
 
 
52
// std::auto_ptr<T> без изъянов
 
53
template<typename T>
 
54
class one
 
55
{
 
56
    private:
 
57
        T* ptr;
 
58
 
 
59
        one(const one& a);
 
60
    
 
61
    public:
 
62
 
 
63
            one() : ptr(0) {}
 
64
            // не explicit
 
65
            template<class Y>
 
66
            one(Y* p) : ptr(p) {}
 
67
 
 
68
            template<class Y>
 
69
            one(one<Y>& a): ptr(a.release()) {}
 
70
 
 
71
           ~one() { /*delete ptr*/ boost::checked_delete(ptr); }
 
72
            
 
73
            // 1 присваивание указателя
 
74
       one& operator =(T* p)
 
75
            {
 
76
                reset(p);
 
77
                return *this;
 
78
            }
 
79
            
 
80
            template<class Y>
 
81
       one& operator=(one<Y>& a)
 
82
            {
 
83
                reset(a.release());
 
84
                return *this;
 
85
            }
 
86
            
 
87
         T& operator*() const
 
88
            {
 
89
                return *ptr;
 
90
            }
 
91
            
 
92
         T* operator->() const
 
93
            {
 
94
                return ptr;
 
95
            }
 
96
            
 
97
         T* get() const { return ptr;}
 
98
          
 
99
      void  reset(T* p = 0)
 
100
            {
 
101
                if(p != ptr)
 
102
                {
 
103
                    delete ptr;
 
104
                    ptr = p;
 
105
                }
 
106
            }
 
107
            
 
108
         T* release()
 
109
            {
 
110
                T* tmp = ptr;
 
111
                ptr = 0;
 
112
                return tmp;
 
113
            }
 
114
 
 
115
            // 2 для конструкции "if( ptr ) ..."
 
116
            typedef T* (one::*ptr_to_bool_type)() const;
 
117
   
 
118
            operator ptr_to_bool_type() const
 
119
            {
 
120
                return get() == 0 ? 0: &one::get;
 
121
            }
 
122
};
 
123
 
 
124
// = boost::shared_ptr<T>
 
125
template<class T>
 
126
class shared: public boost::shared_ptr<T>
 
127
{
 
128
    typedef boost::shared_ptr<T> my_parent;
 
129
    public:
 
130
    
 
131
        // 0 Конструкторы, как у базы
 
132
        // сгенерированные конструктор копирования и присваивания подходят
 
133
        shared(): my_parent() {}
 
134
    
 
135
        // не explicit
 
136
        template<class Y>
 
137
        shared(Y* p): my_parent(p) {}
 
138
    
 
139
        // will release p by calling d(p)
 
140
        template<class Y, class D> shared(Y* p, D d): my_parent(p, d) {}
 
141
    
 
142
        template<class Y>
 
143
        explicit shared(one<Y> & r): my_parent(r.release()) {}
 
144
 
 
145
        template<class Y>
 
146
        shared(const shared<Y>& r)
 
147
            : my_parent(r) {}
 
148
 
 
149
        // 1 присваивание указателя
 
150
        shared& operator =(T* ptr)
 
151
        {
 
152
            this->reset(ptr);
 
153
            return *this;
 
154
        }
 
155
 
 
156
        // кострукторы приведения
 
157
        template<class Y>
 
158
        shared(const shared<Y>& r, boost::detail::static_cast_tag t)
 
159
            : my_parent(r, t) {}
 
160
 
 
161
        template<class Y>
 
162
        shared(const shared<Y>& r, boost::detail::const_cast_tag t)
 
163
            : my_parent(r, t) {}
 
164
 
 
165
        template<class Y>
 
166
        shared(const shared<Y>& r, boost::detail::dynamic_cast_tag t)
 
167
            : my_parent(r, t) {}
 
168
 
 
169
};
 
170
 
 
171
template<class T, class U> shared<T> static_pointer_cast(shared<U> const & r)
 
172
{
 
173
    return shared<T>(r, boost::detail::static_cast_tag());
 
174
}
 
175
 
 
176
template<class T, class U> shared<T> const_pointer_cast(shared<U> const & r)
 
177
{
 
178
    return shared<T>(r, boost::detail::const_cast_tag());
 
179
}
 
180
 
 
181
template<class T, class U> shared<T> dynamic_pointer_cast(shared<U> const & r)
 
182
{
 
183
    return shared<T>(r, boost::detail::dynamic_cast_tag());
 
184
}
 
185
 
 
186
// Базовый класс для объектов с ссылками (=> intrusive_ptr)
 
187
class base
 
188
{
 
189
    typedef boost::shared_ptr<bool> shared_bool;
 
190
    public:
 
191
 
 
192
    long  use_count() const
 
193
          {
 
194
              return use_count_;
 
195
          }
 
196
 
 
197
    inline friend 
 
198
    void  intrusive_ptr_add_ref(base * p)
 
199
          {
 
200
              ++p->use_count_;
 
201
          }
 
202
 
 
203
    inline friend 
 
204
    void  intrusive_ptr_release(base * p)
 
205
          {
 
206
              if(--p->use_count_ == 0) delete p;
 
207
          }
 
208
 
 
209
          // для weak
 
210
    shared_bool  weak_indicator() { return is_exist_; }
 
211
          
 
212
 
 
213
    protected:
 
214
 
 
215
             base(): use_count_(0), is_exist_(new bool(true)) {}
 
216
    virtual ~base() 
 
217
             {
 
218
                 *is_exist_ = false;
 
219
             }
 
220
 
 
221
    private:
 
222
 
 
223
    //boost::detail::atomic_count use_count_;
 
224
        int use_count_;
 
225
        shared_bool is_exist_;
 
226
 
 
227
             base(base const &);
 
228
             base & operator=(base const &);
 
229
};
 
230
 
 
231
//
 
232
// ptr::weak_intrusive<>
 
233
// В связке с ptr::base дает аналог boost::weak_ptr<>, только 
 
234
// для boost::intrusive_ptr<>
 
235
// 
 
236
template<class T>
 
237
class weak_intrusive: public std::pair<T*, boost::shared_ptr<const bool> >
 
238
{
 
239
    typedef  boost::shared_ptr<const bool> bool_type;
 
240
    typedef std::pair<T*, bool_type> my_parent;
 
241
    typedef boost::intrusive_ptr<T> intrusive_ptr;
 
242
 
 
243
    public:
 
244
                    weak_intrusive(T* t = 0)
 
245
                    {
 
246
                        sync(t);
 
247
                    }
 
248
 
 
249
                    template<class Y>
 
250
                    weak_intrusive(const weak_intrusive<Y> & r): my_parent(r) {}
 
251
    
 
252
                    template<class Y>
 
253
    weak_intrusive& operator = (const weak_intrusive<Y> & r)
 
254
                    {
 
255
                        this->first  = r.first;
 
256
                        this->second = r.second;
 
257
                        return *this;
 
258
                    }
 
259
 
 
260
                    template<class Y>
 
261
                    weak_intrusive(const boost::intrusive_ptr<Y> & r)
 
262
                    {
 
263
                        sync(r.get());
 
264
                    }
 
265
 
 
266
     intrusive_ptr  lock()    const { return intrusive_ptr( expired() ? 0 : this->first ); }
 
267
              bool  expired() const 
 
268
                    { 
 
269
                        return !(*this->second); 
 
270
                    }
 
271
 
 
272
    protected:
 
273
              void  sync(T* t)
 
274
                    {
 
275
                        this->first = t;
 
276
                        if( t )
 
277
                            this->second = this->first->weak_indicator();
 
278
                        else
 
279
                            this->second = bool_type(new const bool(false));
 
280
                    }
 
281
};
 
282
 
 
283
using boost::static_pointer_cast;
 
284
using boost::const_pointer_cast;
 
285
using boost::dynamic_pointer_cast;
 
286
 
 
287
} // namespace ptr
 
288
 
 
289
 
 
290
#endif // __MLIB_PTR_H__