1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
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. *
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. *
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 ******************************************************************************/
20 #include "base/object.hpp"
21 #include "base/value.hpp"
22 #include "base/dictionary.hpp"
23 #include "base/primitivetype.hpp"
24 #include "base/utility.hpp"
25 #include "base/timer.hpp"
26 #include "base/logger.hpp"
27 #include "base/exception.hpp"
28 #include <boost/lexical_cast.hpp>
29 #include <boost/thread/recursive_mutex.hpp>
31 using namespace icinga;
33 DEFINE_TYPE_INSTANCE(Object);
36 static boost::mutex l_ObjectCountLock;
37 static std::map<String, int> l_ObjectCounts;
38 static Timer::Ptr l_ObjectCountTimer;
39 #endif /* I2_LEAK_DEBUG */
42 * Destructor for the Object class.
46 delete reinterpret_cast<boost::recursive_mutex *>(m_Mutex);
50 * Returns a string representation for the object.
52 String Object::ToString() const
54 return "Object of type '" + GetReflectionType()->GetName() + "'";
59 * Checks if the calling thread owns the lock on this object.
61 * @returns True if the calling thread owns the lock, false otherwise.
63 bool Object::OwnsLock() const
66 DWORD tid = InterlockedExchangeAdd(&m_LockOwner, 0);
68 return (tid == GetCurrentThreadId());
70 pthread_t tid = __sync_fetch_and_add(&m_LockOwner, 0);
72 return (tid == pthread_self());
77 void Object::SetField(int id, const Value&, bool, const Value&)
80 BOOST_THROW_EXCEPTION(std::runtime_error("Type field cannot be set."));
82 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
85 Value Object::GetField(int id) const
88 return GetReflectionType()->GetName();
90 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
93 bool Object::HasOwnField(const String& field) const
95 Type::Ptr type = GetReflectionType();
100 return type->GetFieldId(field) != -1;
103 bool Object::GetOwnField(const String& field, Value *result) const
105 Type::Ptr type = GetReflectionType();
110 int tid = type->GetFieldId(field);
115 *result = GetField(tid);
119 Value Object::GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const
121 Type::Ptr type = GetReflectionType();
126 int fid = type->GetFieldId(field);
129 return GetPrototypeField(const_cast<Object *>(this), field, true, debugInfo);
132 Field fieldInfo = type->GetFieldInfo(fid);
134 if (fieldInfo.Attributes & FANoUserView)
135 BOOST_THROW_EXCEPTION(ScriptError("Accessing the field '" + field + "' for type '" + type->GetName() + "' is not allowed in sandbox mode.", debugInfo));
138 return GetField(fid);
141 void Object::SetFieldByName(const String& field, const Value& value, const DebugInfo& debugInfo)
143 Type::Ptr type = GetReflectionType();
146 BOOST_THROW_EXCEPTION(ScriptError("Cannot set field on object.", debugInfo));
148 int fid = type->GetFieldId(field);
151 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' does not exist.", debugInfo));
154 SetField(fid, value);
155 } catch (const boost::bad_lexical_cast&) {
156 Field fieldInfo = type->GetFieldInfo(fid);
157 Type::Ptr ftype = Type::GetByName(fieldInfo.TypeName);
158 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "', expected '" + ftype->GetName() + "'", debugInfo));
159 } catch (const std::bad_cast&) {
160 Field fieldInfo = type->GetFieldInfo(fid);
161 Type::Ptr ftype = Type::GetByName(fieldInfo.TypeName);
162 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "', expected '" + ftype->GetName() + "'", debugInfo));
166 void Object::Validate(int types, const ValidationUtils& utils)
168 /* Nothing to do here. */
171 void Object::ValidateField(int id, const Lazy<Value>& lvalue, const ValidationUtils& utils)
173 /* Nothing to do here. */
176 void Object::NotifyField(int id, const Value& cookie)
178 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
181 Object::Ptr Object::NavigateField(int id) const
183 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
186 Object::Ptr Object::Clone() const
188 BOOST_THROW_EXCEPTION(std::runtime_error("Object cannot be cloned."));
191 Type::Ptr Object::GetReflectionType() const
193 return Object::TypeInstance;
196 Value icinga::GetPrototypeField(const Value& context, const String& field, bool not_found_error, const DebugInfo& debugInfo)
198 Type::Ptr ctype = context.GetReflectionType();
199 Type::Ptr type = ctype;
202 Object::Ptr object = type->GetPrototype();
204 if (object && object->HasOwnField(field))
205 return object->GetFieldByName(field, false, debugInfo);
207 type = type->GetBaseType();
211 BOOST_THROW_EXCEPTION(ScriptError("Invalid field access (for value of type '" + ctype->GetName() + "'): '" + field + "'", debugInfo));
217 void icinga::TypeAddObject(Object *object)
219 boost::mutex::scoped_lock lock(l_ObjectCountLock);
220 String typeName = Utility::GetTypeName(typeid(*object));
221 l_ObjectCounts[typeName]++;
224 void icinga::TypeRemoveObject(Object *object)
226 boost::mutex::scoped_lock lock(l_ObjectCountLock);
227 String typeName = Utility::GetTypeName(typeid(*object));
228 l_ObjectCounts[typeName]--;
231 static void TypeInfoTimerHandler()
233 boost::mutex::scoped_lock lock(l_ObjectCountLock);
235 typedef std::map<String, int>::value_type kv_pair;
236 for (kv_pair& kv : l_ObjectCounts) {
240 Log(LogInformation, "TypeInfo")
241 << kv.second << " " << kv.first << " objects";
247 INITIALIZE_ONCE([]() {
248 l_ObjectCountTimer = new Timer();
249 l_ObjectCountTimer->SetInterval(10);
250 l_ObjectCountTimer->OnTimerExpired.connect(std::bind(TypeInfoTimerHandler));
251 l_ObjectCountTimer->Start();
253 #endif /* I2_LEAK_DEBUG */
255 void icinga::intrusive_ptr_add_ref(Object *object)
258 if (object->m_References == 0)
259 TypeAddObject(object);
260 #endif /* I2_LEAK_DEBUG */
263 InterlockedIncrement(&object->m_References);
265 __sync_add_and_fetch(&object->m_References, 1);
269 void icinga::intrusive_ptr_release(Object *object)
274 refs = InterlockedDecrement(&object->m_References);
276 refs = __sync_sub_and_fetch(&object->m_References, 1);
279 if (unlikely(refs == 0)) {
281 TypeRemoveObject(object);
282 #endif /* I2_LEAK_DEBUG */
288 void icinga::DefaultObjectFactoryCheckArgs(const std::vector<Value>& args)
291 BOOST_THROW_EXCEPTION(std::invalid_argument("Constructor does not take any arguments."));
294 void icinga::RequireNotNullInternal(const intrusive_ptr<Object>& object, const char *description)
297 BOOST_THROW_EXCEPTION(std::invalid_argument("Pointer must not be null: " + String(description)));