3 * Copyright 1999 Karl Nelson <kenelson@ece.ucdavis.edu>
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.
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.
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.
19 #ifndef SIGCXX_SCOPE_H
20 #define SIGCXX_SCOPE_H
21 #include <sigc++/sigc++config.h>
23 #ifdef SIGC_CXX_NAMESPACES
29 struct LIBSIGC_API ScopeNode
31 mutable ScopeNode *prev_;
32 mutable ScopeNode *next_;
34 // removes self from list
37 // Called to inform the item that it is erased
40 // inform scopes that invalid requested.
41 virtual void disconnect(bool destroy=0);
47 :prev_(this),next_(this) {}
53 ScopeNode& operator=(const ScopeNode&);
54 ScopeNode(const ScopeNode&);
57 struct LIBSIGC_API DataNode: public ScopeNode
63 /*******************************************************************
65 *******************************************************************/
67 class ObjectReferenced;
71 class LIBSIGC_API Reference
74 mutable ObjectReferenced* obj_;
80 void init(ObjectReferenced* obj);
81 void set(ObjectReferenced* obj,void* cache=0,bool ptr=false);
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; };
87 ObjectReferenced* object() const {return obj_;}
88 void* cache() const {return cache_;}
90 Reference():obj_(0) {}
91 Reference(ObjectReferenced &obj)
93 Reference(const Reference& ref)
98 class LIBSIGC_API Scope:public ScopeNode
100 friend class ObjectScoped;
102 Scope& operator=(const Scope& scope);
103 Scope(const Scope& scope);
106 void set(ObjectScoped* obj,void* cache,bool ptr);
107 mutable ObjectScoped* obj_;
108 mutable void* cache_;
110 virtual void on_connect()=0;
111 virtual void erase();
113 void register_scope(ObjectScoped *);
114 void register_scope(const Scope *parent=0);
115 void unregister_scope();
123 ObjectScoped* object() const {return (ObjectScoped*)(obj_);}
124 void* cache() const {return cache_;}
126 // Inform object it should invalidate its list.
129 Scope():obj_(0),cache_(0) {}
134 /******************************************************
136 *******************************************************
138 Uncounted - non-reference
139 Limit - Limits the lifetime of object to this scope
141 Extend - Extends the lifetime of the object to this scope
143 LimitOwned - Conditionally limits the lifetime of object
145 FuncRef - Extends the lifetime, without sink
146 (intended for functions)
147 Reference - Extends the lifetime, with sink
149 AutoPtr - Shorthand for auto_ptr like scope.
150 RefCount - Shorthand for ref_ptr like scope.
152 ******************************************************/
156 class LIBSIGC_API Uncounted:public Scope
158 Uncounted& operator=(const Uncounted&);
159 Uncounted(const Uncounted&);
161 virtual void disconnect(bool level=0);
162 Uncounted():Scope() {}
163 virtual ~Uncounted();
166 class LIBSIGC_API Extend:public Scope
168 Extend& operator=(const Extend&);
169 Extend(const Extend&);
171 virtual void on_connect();
172 virtual void erase();
174 virtual void disconnect(bool level=0);
175 void set(ObjectScoped* obj,void* cache,bool ptr);
180 class LIBSIGC_API Limit:public Scope
182 Limit& operator=(const Limit&);
185 virtual void on_connect();
186 virtual void erase();
188 virtual void disconnect(bool level=0);
189 void set(ObjectScoped* obj,void* cache,bool ptr);
194 typedef Extend RefCount;
195 typedef Reference Lock;
198 /*************************************************************
200 *************************************************************/
201 // Stub for building polylists
205 struct LIBSIGC_API ScopeIterator_
207 typedef ScopeNode NodeType;
212 inline NodeType* node() {return node_;}
213 inline const NodeType* node() const {return node_;}
215 inline NodeType& operator*()
218 inline const NodeType& operator*() const
222 inline bool operator==(const ScopeIterator_& i) const
223 {return node_==i.node_;
225 inline bool operator!=(const ScopeIterator_& i) const
226 {return node_!=i.node_;
229 inline ScopeIterator_& operator++()
232 node_=(NodeType*)node_->next_;
236 ScopeIterator_ operator++(int)
238 ScopeIterator_ tmp=*this;
243 ScopeIterator_& operator= (const ScopeIterator_& i)
249 ScopeIterator_(const ScopeIterator_ &n):node_(n.node_) {}
250 ScopeIterator_(NodeType *n):node_(n) {}
251 ScopeIterator_():node_(0) {}
254 class LIBSIGC_API ScopeList
257 typedef ScopeNode NodeType;
258 typedef ScopeIterator_ Iterator;
262 inline Iterator begin() {return Iterator(node_.next_);}
263 inline Iterator end() {return Iterator(&node_);}
265 // insert item directly on list
266 Iterator insert_direct(Iterator pos,NodeType *n);
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);
274 { erase(begin(),end()); }
276 bool empty() const {return node_.next_==&node_;}
278 ScopeList():node_() {}
279 ~ScopeList() { clear(); }
282 ScopeList(const ScopeList&);
287 #ifdef SIGC_CXX_NAMESPACES