]> git.lyx.org Git - lyx.git/blob - sigc++/object.cc
dont use threads, add inquiv operator for Aux_info
[lyx.git] / sigc++ / object.cc
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 #include <sigc++/object.h>
20
21 #ifdef SIGC_CXX_NAMESPACES
22 namespace SigC
23 {
24 #endif // SIGC_CXX_NAMESPACES
25
26 int sigc_major_version=SIGC_MAJOR_VERSION;
27 int sigc_minor_version=SIGC_MINOR_VERSION;
28 int sigc_micro_version=SIGC_MICRO_VERSION;
29
30 ObjectReferenced::ObjectReferenced():
31   obj_count_(0),
32   obj_dynamic_(0),obj_owned_(1),obj_floating_(1),obj_transfer_(0),
33   obj_invalid_(0),obj_destroyed_(0),obj_weak_(0)
34   {}
35
36 ObjectScoped::ObjectScoped():list_()
37   {}
38
39 ObjectReferenced::~ObjectReferenced()
40   {}
41
42 ObjectScoped::~ObjectScoped()
43   {
44    // we can't be destroyed again.
45    obj_destroyed_=1;
46
47    // trash the list.
48    invalid(1);
49   }
50
51 void ObjectReferenced::reference()
52   {
53    // if we exceed the int limit,  we should unset dynamic_
54    if (!(++obj_count_))
55      obj_dynamic_=0;
56   }
57
58 void ObjectReferenced::unreference()
59   {
60    if (obj_count_
61        && (!--obj_count_)
62        && obj_dynamic_
63        && !obj_floating_
64        && !obj_destroyed_
65       )
66       { 
67         obj_destroyed_=1; 
68         delete this; 
69       }
70   }
71
72 void ObjectScoped::register_data(ScopeNode *data)
73   {
74    list_.insert_direct(list_.end(),data);
75   }
76
77 void ObjectScoped::register_scope(Scope *scope,const Scope *parent)
78   {
79    if (!scope) return;
80
81    // check for invalid in progress
82    if (obj_invalid_)
83      return;
84
85    // reregistering a scope
86    if (scope->obj_==this) 
87      {
88       if (obj_transfer_&&(ScopeNode*)parent==list_.begin().node())
89         {list_.swap_elements(const_cast<Scope*>(parent),scope);    
90          obj_transfer_=0;
91         }
92       return;
93      }
94
95    if (obj_transfer_&&(ScopeNode*)parent==list_.begin().node())
96      {
97       list_.insert_direct(list_.begin(),scope);
98       obj_transfer_=0;
99      }
100    else
101      list_.insert_direct(list_.end(),scope);
102
103    // complete connection
104    scope->obj_=this;
105    scope->on_connect();
106   }
107
108 void ObjectScoped::unregister_scope(Scope *scope)
109   {
110    if (!scope) return;
111
112    // Check for loss of ownership
113    if (obj_owned_&&(ScopeNode*)scope==list_.begin().node())
114      obj_owned_=0;
115
116    list_.erase(scope);
117    scope->obj_=0;
118   }
119
120 void ObjectScoped::set_weak()
121   {
122     if (obj_weak_) return;
123     obj_weak_=1;
124     reference();
125   }
126
127 struct Invalid_
128   {
129     ObjectReferenced* r_;
130     Invalid_(ObjectReferenced& r): r_(&r) 
131       {
132         r_->obj_invalid_=1;
133         r_->reference();
134       }
135     ~Invalid_() 
136       {
137         r_->obj_invalid_=0;
138         r_->unreference();
139       }
140   };
141
142 void ObjectScoped::invalid(bool level)
143   {
144    if (!level&&(obj_invalid_||!obj_dynamic_))
145      return;
146
147    List_::Iterator current=list_.begin();
148    List_::Iterator next=current;
149    if (current==list_.end()&&!obj_weak_)
150      return;
151
152    Invalid_ r(*this);
153
154    if (obj_weak_) 
155      {
156        obj_weak_=0;
157        unreference();
158      }
159
160    while (current!=(list_.end()))
161      {
162       ++next;
163       (*current).disconnect(level);
164       current=next;
165      }
166   }
167
168 Object::~Object()
169   {}
170
171 void ObjectReferenced::set_dynamic() {obj_dynamic_=1;}
172
173
174 #ifdef SIGC_CXX_NAMESPACES
175 } // namespace
176 #endif // SIGC_CXX_NAMESPACES