Developer documentation

shared_object.hpp
Go to the documentation of this file.
1 /********************************************************************
2  * This file is part of the source code of the realroot library.
3  * Author(s): B. Mourrain, GALAAD, INRIA
4  * $Id: shared_object.hpp,v 1.1 2005/07/11 11:23:12 mourrain Exp $
5  ********************************************************************/
6 #ifndef mmx_shared_object_hpp
7 #define mmx_shared_object_hpp
8 //--------------------------------------------------------------------
9 #include <cassert>
10 #include <memory>
11 //#include <utility>
12 //#include <iterator>
13 //#include <algorithm>
14 //#include <set>
15 //#include <typeinfo>
16 //======================================================================
17 namespace mmx {
18 
19 //template<typename C> inline void
20 //std__swap (C& x, C& y) {
21 //C temp= x;
22 //x= y;
23 //y= temp;
24 //}
25 
26 #define define_operator_rrr(parm0,parm1,parm2,opname,function) \
27 inline parm0 \
28 opname(const parm1& x, const parm2& y) { \
29  parm0 r; function(r.rep(),x.rep(),y.rep()); return r; \
30 }
31 
32 #define define_operator_rr_(parm0,parm1,parm2,opname,function) \
33 inline \
34 parm0 opname(const parm1& x, const parm2& y) { \
35  parm0 r; function(r.rep(),x.rep(),y); return r; \
36 }
37 #define define_operator_r_r(parm0,parm1,parm2,opname,function) \
38 inline \
39 parm0 opname(const parm1& x, const parm2& y) { \
40  parm0 r; function(r.rep(),x,y.rep()); return r; \
41 }
42 
43 #define define_operator__r_(parm0,parm1,parm2,opname,function) \
44 inline \
45 parm0 opname(const parm1& x, const parm2& y) { \
46  parm0 r; function(r,x.rep(),y); return r; \
47 }
48 
49 
50 #define define_function_from_inplace(parm0,parm1,parm2,function) \
51 inline \
52 void function(parm0& r, const parm1& x, const parm2& y) { \
53  r=x;function(r,y); \
54 }
55 
56 #ifndef assert
57 #define assert(expr,msg) if (!(expr)) assert (msg);
58 #endif
59 
60 //======================================================================
61 struct AsSize {};
62 //--------------------------------------------------------------------
63 template <class R>
65 
66 public:
67  typedef R element_type;
68  // typedef typename FUNC_ARG(element_type) func_arg;
69 
70 protected:
71  struct rep
72  // : generic_rep
73  {
74  int refc;
75  R obj;
76 
77  template<class P0>
78  rep(P0 a) : refc(1), obj(a) {};
79 
80  template<class P0, class P1>
81  rep(P0 a, P1 b ) : refc(1), obj(a,b) {};
82 
83  template<class P0, class P1, class P2>
84  rep(P0 a, P1 b, P2 c ) : refc(1), obj(a,b,c) {};
85 
86  template<class P0, class P1, class P2, class P3 >
87  rep(P0 a, P1 b, P2 c, P3 d) : refc(1), obj(a,b,c,d) {};
88 
89  rep() : refc(1) {//std::cout<<"build data "<< refc<<std::endl;
90  }
91 
92  rep(const rep& o) : refc(1), obj(o.obj) { }
93  ~rep() {}
94  void* operator new(size_t s)
95  {return std::allocator<R>().allocate(s); }
96  void operator delete(void* p)
97  {std::allocator<R>().deallocate((R*)p,sizeof(rep)); }
98 
99 // static const int TYPE=3000;
100 // int Type() {return TYPE;}
101  };
102 
103 public:
105 
106  void leave()
107  {assert(body->refc>0);
108  //std::cout<<"Leave "<<body<<" "<<body->refc<<std::endl;
109  if (! --body->refc) delete body;}
110  void divorce()
111  {assert(body->refc>1); --body->refc; body= new rep(*body);}
112 
113  typedef R object_type;
114  typedef R* iterator;
115  typedef const R* const_iterator;
116 
117  template<class P0>
118  shared_object(P0 a) : body(new rep(a)){};
119  template<class P0, class P1>
120  shared_object(P0 a, P1 b) : body(new rep(a,b)){};
121  template<class P0, class P1, class P2>
122  shared_object(P0 a, P1 b, P2 c) : body(new rep(a,b,c)){};
123  template<class P0, class P1, class P2, class P3>
124  shared_object(P0 a, P1 b, P2 c, P3 d) : body(new rep(a,b,c,d)){};
125  shared_object() : body(new rep()) {
126  //std::cout<<"body "<<body<<" "<<body->refc<<std::endl;
127  }
128  shared_object(const R& obj_arg) : body(new rep(obj_arg)) {}
129  shared_object(const shared_object& s) : body(s.body) {++body->refc;}
130  shared_object(rep * s) : body(s) {++body->refc;}
132 
134  {
135  if (this!=&s) {s.body->refc++; leave(); body=s.body;}
136  return *this;
137  }
138 
139  // void swap(shared_object& s2) { std__swap(body,s2.body); }
140 
141  iterator operator-> () { if (body->refc > 1) divorce(); return &body->obj; }
142  R& operator* () { if (body->refc > 1) divorce(); return body->obj; }
143 
144  const_iterator operator-> () const { return &body->obj; }
145  const R& operator* () const { return body->obj; }
146 
147  int ref() const {return body->refc;}
148  int ref() {return body->refc;}
149 
150 };
151 
152 // template<typename R> inline
153 // R view_as (generic g)
154 // {
155 // typedef typename R::rep_type REP;
156 // if (gentype(g) != shared_object<REP>::rep::TYPE)
157 // std::cerr<<"generic of type "<<gentype(g)
158 // <<"is not an obj of required type "
159 // << shared_object<REP>::rep::TYPE<<std::endl;
160 // shared_object<REP> x((typename shared_object<REP>::rep*)g.rep);
161 // return R(x);//(REP*)g.rep);
162 // }
163 template< class R>
164  struct rep_view
165  {
166 #define NOREFCOUNT
167 #ifdef NOREFCOUNT
168  R data;
169  R & rep() {return data;}
170  const R & rep() const {return data;}
171 #else
173  R & rep() {return *data;}
174  const R & rep() const {return *data;}
175 #endif
176  };
177 //----------------------------------------------------------------------
179 template<class R>
180 inline R & rep(R & r) {return r;}
181 
183 template<class R>
184 inline const R & rep(const R & r) {return r;}
185 
187 template<class R>
188 inline R & rep(shared_object<R> & r) {return *r;}
189 
191 template<class R>
192 inline const R & rep(const shared_object<R> & r) {return *r;}
193 
195 
201 template<class T>
202 struct ReferTo
203 {
205  typedef T value_type;
206 };
207 
209 
215 template<class T>
217 {
219  typedef T value_type;
220 };
221 
222 
223 //======================================================================
224 } //namespace mmx
225 //======================================================================
226 #endif // mmx_shared_object_hpp
227 
Definition: shared_object.hpp:71
shared_object(P0 a, P1 b)
Definition: shared_object.hpp:120
int refc
Definition: shared_object.hpp:74
T value_type
Definition: shared_object.hpp:219
const C & b
Definition: Interval_glue.hpp:25
shared_object(P0 a, P1 b, P2 c)
Definition: shared_object.hpp:122
R & rep()
Definition: shared_object.hpp:169
void divorce()
Definition: shared_object.hpp:110
rep(P0 a, P1 b, P2 c, P3 d)
Definition: shared_object.hpp:87
R & rep(R &r)
Definition: shared_object.hpp:180
rep(P0 a)
Definition: shared_object.hpp:78
R data
Definition: shared_object.hpp:168
int ref()
Definition: shared_object.hpp:148
shared_object(const shared_object &s)
Definition: shared_object.hpp:129
R object_type
Definition: shared_object.hpp:113
const R * const_iterator
Definition: shared_object.hpp:115
The structure for obtain the template parameter.
Definition: shared_object.hpp:202
rep(const rep &o)
Definition: shared_object.hpp:92
shared_object(rep *s)
Definition: shared_object.hpp:130
Definition: shared_object.hpp:64
shared_object & operator=(const shared_object &s)
Definition: shared_object.hpp:133
iterator operator->()
Definition: shared_object.hpp:141
R & operator*()
Definition: shared_object.hpp:142
rep(P0 a, P1 b)
Definition: shared_object.hpp:81
shared_object(const R &obj_arg)
Definition: shared_object.hpp:128
Definition: shared_object.hpp:61
int ref() const
Definition: shared_object.hpp:147
shared_object(P0 a)
Definition: shared_object.hpp:118
R * iterator
Definition: shared_object.hpp:114
rep(P0 a, P1 b, P2 c)
Definition: shared_object.hpp:84
shared_object()
Definition: shared_object.hpp:125
Definition: shared_object.hpp:164
R obj
Definition: shared_object.hpp:75
shared_object(P0 a, P1 b, P2 c, P3 d)
Definition: shared_object.hpp:124
const C & c
Definition: Interval_glue.hpp:45
T value_type
Definition: shared_object.hpp:205
~rep()
Definition: shared_object.hpp:93
const R & rep() const
Definition: shared_object.hpp:170
~shared_object()
Definition: shared_object.hpp:131
R element_type
Definition: shared_object.hpp:67
rep * body
Definition: shared_object.hpp:104
Definition: array.hpp:12
void leave()
Definition: shared_object.hpp:106
rep()
Definition: shared_object.hpp:89
#define assert(expr, msg)
Definition: shared_object.hpp:57
Home