]> granicus.if.org Git - icinga2/blob - lib/base/object.cpp
Use BOOST_THROW_EXCEPTION instead of boost::throw_exception()
[icinga2] / lib / base / object.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program 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              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "i2-base.h"
21
22 using namespace icinga;
23
24 /**
25  * Default constructor for the Object class.
26  */
27 Object::Object(void)
28 {
29 #ifdef _DEBUG
30         boost::mutex::scoped_lock lock(GetMutex());
31         GetAliveObjects().insert(this);
32 #endif /* _DEBUG */
33 }
34
35 /**
36  * Destructor for the Object class.
37  */
38 Object::~Object(void)
39 {
40 #ifdef _DEBUG
41         boost::mutex::scoped_lock lock(GetMutex());
42         GetAliveObjects().erase(this);
43 #endif /* _DEBUG */
44 }
45
46 /**
47  * Temporarily holds onto a reference for an object. This can
48  * be used to safely clear the last reference to an object
49  * in an event handler.
50  */
51 void Object::Hold(void)
52 {
53         boost::mutex::scoped_lock lock(GetMutex());
54         GetHeldObjects().push_back(GetSelf());
55 }
56
57 /**
58  * Clears all temporarily held objects.
59  */
60 void Object::ClearHeldObjects(void)
61 {
62         boost::mutex::scoped_lock lock(GetMutex());
63         GetHeldObjects().clear();
64 }
65
66 /**
67  * Returns a reference-counted pointer to this object.
68  *
69  * @returns A shared_ptr object that points to this object
70  */
71 Object::SharedPtrHolder Object::GetSelf(void)
72 {
73         return Object::SharedPtrHolder(shared_from_this());
74 }
75
76 #ifdef _DEBUG
77 /**
78  * Retrieves the number of currently alive objects.
79  *
80  * @returns The number of alive objects.
81  */
82 int Object::GetAliveObjectsCount(void)
83 {
84         boost::mutex::scoped_lock lock(GetMutex());
85         return GetAliveObjects().size();
86 }
87
88 /**
89  * Dumps a memory histogram to the "dictionaries.dump" file.
90  */
91 void Object::PrintMemoryProfile(void)
92 {
93         map<String, int> types;
94
95         ofstream dictfp("dictionaries.dump.tmp");
96
97         {
98                 boost::mutex::scoped_lock lock(GetMutex());
99                 set<Object *>::iterator it;
100                 BOOST_FOREACH(Object *obj, GetAliveObjects()) {
101                         pair<map<String, int>::iterator, bool> tt;
102                         tt = types.insert(make_pair(Utility::GetTypeName(typeid(*obj)), 1));
103                         if (!tt.second)
104                                 tt.first->second++;
105
106                         if (typeid(*obj) == typeid(Dictionary)) {
107                                 Dictionary::Ptr dict = obj->GetSelf();
108                                 dictfp << Value(dict).Serialize() << std::endl;
109                         }
110                 }
111         }
112
113 #ifdef _WIN32
114         _unlink("dictionaries.dump");
115 #endif /* _WIN32 */
116
117         dictfp.close();
118         if (rename("dictionaries.dump.tmp", "dictionaries.dump") < 0)
119                 BOOST_THROW_EXCEPTION(PosixException("rename() failed", errno));
120
121         String type;
122         int count;
123         BOOST_FOREACH(tie(type, count), types) {
124                 std::cerr << type << ": " << count << std::endl;
125         }
126 }
127
128 /**
129  * Returns currently active objects.
130  *
131  * @returns currently active objects
132  */
133 set<Object *>& Object::GetAliveObjects(void)
134 {
135         static set<Object *> aliveObjects;
136         return aliveObjects;
137 }
138 #endif /* _DEBUG */
139
140 /**
141  * Returns the mutex used for accessing static members.
142  *
143  * @returns a mutex
144  */
145 boost::mutex& Object::GetMutex(void)
146 {
147         static boost::mutex mutex;
148         return mutex;
149 }
150
151 /**
152  * Returns currently held objects. The caller must be
153  * holding the mutex returned by GetMutex().
154  *
155  * @returns currently held objects
156  */
157 vector<Object::Ptr>& Object::GetHeldObjects(void)
158 {
159         static vector<Object::Ptr> heldObjects;
160         return heldObjects;
161 }
162
163