]> git.lyx.org Git - lyx.git/blob - sigc++/scope.h
last try ... sigh
[lyx.git] / sigc++ / scope.h
1 // -*- c++ -*-
2 /* 
3  * Copyright 1999 Karl Nelson <kenelson@ece.ucdavis.edu>
4  * 
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  * 
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  * 
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the Free
17  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 #ifndef SIGCXX_SCOPE_H
20 #define SIGCXX_SCOPE_H
21 #include <sigc++/sigc++config.h>
22
23 #ifdef SIGC_CXX_NAMESPACES
24 namespace SigC
25 {
26 #endif 
27
28
29 struct LIBSIGC_API ScopeNode 
30   {
31    mutable ScopeNode *prev_;
32    mutable ScopeNode *next_;
33
34    // removes self from list
35    void remove_self();
36
37    // Called to inform the item that it is erased
38    virtual void erase();
39
40    // inform scopes that invalid requested.
41    virtual void disconnect(bool destroy=0);
42
43    ScopeNode()
44 #ifdef LIBSIGC_WIN32
45    {prev_=next_=this;}
46 #else
47    :prev_(this),next_(this) {}
48 #endif
49
50    virtual ~ScopeNode();
51
52    private:
53      ScopeNode& operator=(const ScopeNode&);
54      ScopeNode(const ScopeNode&);
55   };
56
57 struct LIBSIGC_API DataNode: public ScopeNode
58   {
59    virtual void erase();
60    virtual ~DataNode();
61   };
62
63 /*******************************************************************
64 ***** Basis Scope
65 *******************************************************************/
66 class ObjectScoped;
67 class ObjectReferenced;
68 class Object;
69 class Scope;
70
71 class LIBSIGC_API Reference
72   {
73    protected:
74      mutable ObjectReferenced* obj_;
75      mutable void* cache_;
76
77    public:
78      void set_sink();
79
80      void init(ObjectReferenced* obj);
81      void set(ObjectReferenced* obj,void* cache=0,bool ptr=false);
82
83      Reference& operator=(ObjectReferenced *obj) { set(obj); return *this; }
84      Reference& operator=(ObjectReferenced &obj) { set(&obj); return *this; }
85      Reference& operator=(const Reference& ref)  { set(ref.obj_); return *this; };
86
87      ObjectReferenced* object() const {return obj_;}
88      void* cache() const {return cache_;}
89
90      Reference():obj_(0) {}
91      Reference(ObjectReferenced &obj)
92        {init(&obj);}
93      Reference(const Reference& ref)
94        {init(ref.obj_);}
95      ~Reference();
96   };
97      
98 class LIBSIGC_API Scope:public ScopeNode
99   {
100    friend class ObjectScoped;
101
102      Scope& operator=(const Scope& scope);
103      Scope(const Scope& scope);
104
105    protected:
106      void set(ObjectScoped* obj,void* cache,bool ptr);
107      mutable ObjectScoped* obj_;
108      mutable void* cache_;
109
110      virtual void on_connect()=0;
111      virtual void erase();
112
113      void register_scope(ObjectScoped *);
114      void register_scope(const Scope *parent=0);
115      void unregister_scope();
116
117    public:
118
119      void reference();
120      void unreference();
121      void set_sink();
122
123      ObjectScoped* object() const {return (ObjectScoped*)(obj_);}
124      void* cache() const {return cache_;}
125
126      // Inform object it should invalidate its list.
127      void invalid();
128
129      Scope():obj_(0),cache_(0) {}
130      virtual ~Scope();
131   };
132
133
134 /****************************************************** 
135 **** Common Scopes
136 *******************************************************
137   Available Scopes:
138     Uncounted  - non-reference
139     Limit      - Limits the lifetime of object to this scope
140                  Sinks object.
141     Extend     - Extends the lifetime of the object to this scope
142                  Sinks object.
143     LimitOwned - Conditionally limits the lifetime of object
144                  Sinks object.
145     FuncRef    - Extends the lifetime, without sink
146                  (intended for functions)
147     Reference  - Extends the lifetime, with sink
148     
149     AutoPtr    - Shorthand for auto_ptr like scope.
150     RefCount   - Shorthand for ref_ptr like scope.
151         
152 ******************************************************/
153 struct Scopes
154 {
155
156 class LIBSIGC_API Uncounted:public Scope
157   {
158      Uncounted& operator=(const Uncounted&);
159      Uncounted(const Uncounted&);
160    public:
161      virtual void disconnect(bool level=0);
162      Uncounted():Scope() {}
163      virtual ~Uncounted();
164   };
165
166 class LIBSIGC_API Extend:public Scope
167   {
168      Extend& operator=(const Extend&);
169      Extend(const Extend&);
170    protected:
171      virtual void on_connect();
172      virtual void erase();
173    public:
174      virtual void disconnect(bool level=0);
175      void set(ObjectScoped* obj,void* cache,bool ptr);
176      Extend():Scope() {}
177      virtual ~Extend();
178   };
179
180 class LIBSIGC_API Limit:public Scope
181   {
182      Limit& operator=(const Limit&);
183      Limit(const Limit&);
184    protected:
185      virtual void on_connect();
186      virtual void erase();
187    public:
188      virtual void disconnect(bool level=0);
189      void set(ObjectScoped* obj,void* cache,bool ptr);
190      Limit():Scope() {}
191      virtual ~Limit();
192   };
193
194 typedef Extend RefCount;
195 typedef Reference Lock;
196 };
197
198 /*************************************************************
199 ***** Lists 
200 *************************************************************/
201 // Stub for building polylists
202
203
204 // Iterator skeleton
205 struct LIBSIGC_API ScopeIterator_
206   {
207    typedef ScopeNode NodeType;
208    private:
209      NodeType *node_;
210    public:
211
212    inline NodeType* node()             {return node_;}
213    inline const NodeType* node() const {return node_;}
214
215    inline NodeType& operator*()
216      {return *node_;
217      }
218    inline const NodeType& operator*() const
219      {return *node_;
220      }
221    
222    inline bool operator==(const ScopeIterator_& i) const 
223      {return node_==i.node_;
224      }
225    inline bool operator!=(const ScopeIterator_& i) const
226      {return node_!=i.node_;
227      }
228
229    inline ScopeIterator_& operator++()
230      {
231       if (node_) 
232         node_=(NodeType*)node_->next_;
233       return *this;
234      }
235
236    ScopeIterator_ operator++(int)
237      {
238       ScopeIterator_ tmp=*this;
239       ++*this;
240       return tmp;
241      }
242
243    ScopeIterator_& operator= (const ScopeIterator_& i)
244      {
245       node_=i.node_;
246       return *this;
247      }
248
249    ScopeIterator_(const ScopeIterator_ &n):node_(n.node_) {}
250    ScopeIterator_(NodeType *n):node_(n) {}
251    ScopeIterator_():node_(0) {}
252   };
253
254 class LIBSIGC_API ScopeList
255   {
256    public:
257    typedef ScopeNode NodeType; 
258    typedef ScopeIterator_ Iterator;
259
260    ScopeNode node_;   
261
262    inline Iterator begin()             {return Iterator(node_.next_);}
263    inline Iterator end()               {return Iterator(&node_);}
264
265    // insert item directly on list
266    Iterator insert_direct(Iterator pos,NodeType *n);
267
268    Iterator erase(Iterator pos);
269    void erase(Iterator start,Iterator stop)
270      { while (start!=stop) start=erase(start); }
271    void swap_elements(Iterator p1,Iterator p2);
272
273    void clear()
274      { erase(begin(),end()); }
275
276    bool empty() const {return node_.next_==&node_;} 
277    
278    ScopeList():node_() {}
279    ~ScopeList() { clear(); }
280
281    private:
282      ScopeList(const ScopeList&);
283   };
284
285
286
287 #ifdef SIGC_CXX_NAMESPACES
288 } // namespace sigc
289 #endif
290
291 #endif