]> Creatis software - bbtk.git/blob - kernel/src/bbtkObject.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkObject.cxx
1 #include "bbtkObject.h"
2 #include "bbtkMessageManager.h"
3
4 namespace bbtk
5
6   
7
8   //=======================================================================
9   Object::ObjectListType Object::mgObjectList;
10   //=======================================================================
11
12   //=======================================================================
13   Object::Object()
14   { 
15   }
16   //=======================================================================
17
18
19   //=======================================================================
20   Object::~Object()
21   { 
22   }
23   //=======================================================================
24
25   //=======================================================================
26   void Object::InsertInObjectList(Pointer p)
27   { 
28     bbtkDebugMessage("object",9,"##> Object::InsertInObjectList(\""
29                      <<p->GetObjectName()<<"\" ["<<p<<"])"<<std::endl);
30     boost::weak_ptr<Object> w(p);
31     mgObjectList.insert(w); 
32   }
33   //=======================================================================
34
35
36   //=======================================================================
37   void Object::RemoveFromObjectList(WeakPointer p)
38   { 
39     bbtkDebugMessage("object",9,"##> Object::RemoveFromObjectList()"
40                      <<std::endl);
41     mgObjectList.erase(p);
42
43   }
44   //=======================================================================
45
46
47   //=======================================================================
48   std::string Object::GetObjectName() const
49   {
50     return std::string("**Unknown object**");
51   }
52   //=======================================================================
53
54   //=======================================================================
55   std::string Object::GetObjectInfo() const
56   {
57     return std::string("");
58   }
59   //=======================================================================
60   
61   //=======================================================================
62   void Object::PrintObjectListInfo(const std::string& name)
63   {
64     
65     std::cout 
66       << "=============== Living bbtk::Object pointers ========="<<std::endl;
67
68     long n = 0;
69     long u = 0;
70     size_t m = 0;
71     ObjectListType::iterator i;
72     for (i = mgObjectList.begin();
73          i!=mgObjectList.end();
74          ++i)
75       {
76         if (i->use_count() == 0) 
77           {
78             u++;
79           }
80         else 
81           { 
82             Object::Pointer p(i->lock());
83             if (p->GetObjectName().find(name) != std::string::npos ) 
84               {
85                 std::cout << n << "/" << mgObjectList.size() << " ";
86                 PrintObjectInfo(p);
87                 m += p->GetObjectSize();
88                 n++;
89               }
90           }
91       }
92     std::cout 
93       << "------------------------------------------------------"<<std::endl; 
94
95     std::cout << " Total : "<<n<<" objects - "<<m<<" b"<<std::endl;
96     if (u==1)
97       {
98         std::cout<<"* Note : "<<u
99                  <<" object in list has 0 ref count, "
100                  <<"i.e. destroyed without removing itself from the living objects list ! (this is just an implementation error not a memory leak)"<<std::endl;
101       }
102     else if (u>1)
103       {
104         std::cout<<"* Note : "<<u
105                  <<" objects in list have 0 ref count, "
106                  <<"i.e. destroyed without removing themselves from the living objects list ! (this is just an implementation error not a memory leak)"<<std::endl;
107       }
108     std::cout
109       << "============ EO Living bbtk::Object pointers ========="<<std::endl;
110         
111   }
112   //=======================================================================
113
114   /*
115   //=======================================================================
116   void Object::PrintObjectInfo(const std::string& name)
117   {
118     long n = 0;
119     ObjectListType::iterator i;
120     for (i = mgObjectList.begin();
121          i!=mgObjectList.end();
122          ++i)
123       {
124         n++;
125         if (i->use_count() == 0) continue;
126         Object::Pointer p(i->lock());   
127         if (p->GetObjectName().find(name) != std::string::npos ) 
128           {
129             std::cout << n << "/" << mgObjectList.size() << " ";
130             PrintObjectInfo(p);
131           }
132       }
133   }
134   //=======================================================================
135   */
136   //=======================================================================
137   void Object::PrintObjectInfo(const Object::Pointer& p)
138   {
139     std::cout << "* [" << p << "] \t" 
140               << p.use_count()-1 << " r \t"
141               << p->GetObjectRecursiveSize() << " ("
142               << p->GetObjectSize() << ") b \t"
143               << p->GetObjectName() 
144               << std::endl;
145     std::cout << p->GetObjectInfo();
146   }
147   //=======================================================================
148
149   //=======================================================================
150   /// Objects deleter
151   void Object::Deleter::operator() (Object* p)
152   {
153     std::string name = p->GetObjectName();
154     void* adr = (void*)p;
155     bbtkDebugMessage("object",1,"##> Object::Deleter : deleting \""
156                      <<name<<"\" ["<<adr<<"]"<<std::endl);
157     Object::RemoveFromObjectList(mPointer);
158     this->Delete(p);
159     bbtkDebugMessage("object",2,"<## Object::Deleter : \""<<name<<"\" ["
160                      <<adr<<"] deleted"<<std::endl);
161   }
162   //=======================================================================
163
164
165
166
167
168
169
170
171
172   //=======================================================================
173   bool StaticInitTime::PrintObjectListInfo = false;
174   //=======================================================================
175   //=======================================================================
176   StaticInitTime::StaticInitTime() 
177   {
178   }
179   //=======================================================================
180   //=======================================================================
181   StaticInitTime::~StaticInitTime()
182   {
183     if (PrintObjectListInfo) 
184       {
185         std::cout << std::endl
186                   << "************************** main ended *******************"
187                   << std::endl << std::endl;
188         Object::PrintObjectListInfo("");
189       }
190     if (Object::GetObjectsCount()>0)
191       {
192         std::cout << std::endl;
193         std::cout << "**************************** WARNING *************************"
194                   <<std::endl; 
195         std::cout << "**** "<< Object::GetObjectsCount()
196                   <<" bbtk objects still alive after main ended"<<std::endl;
197         if (!PrintObjectListInfo) 
198           {
199             
200             std::cout << "**** "
201                       <<"Rerun bbi with -D option or put 'debug -D' in bbs file"
202                   <<std::endl;
203             std::cout << "**** "
204                       <<"to view the objects list "
205                       <<std::endl;
206           }
207         std::cout << "**************************************************************"
208                   <<std::endl
209                   <<std::endl;
210
211      }
212   }
213   //=======================================================================
214   //=======================================================================
215   //  The static instance
216   static StaticInitTime i;
217   //=======================================================================
218
219 }