1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/object.hpp"
4 #include "base/value.hpp"
5 #include "base/dictionary.hpp"
6 #include "base/primitivetype.hpp"
7 #include "base/utility.hpp"
8 #include "base/timer.hpp"
9 #include "base/logger.hpp"
10 #include "base/exception.hpp"
11 #include <boost/lexical_cast.hpp>
12 #include <boost/thread/recursive_mutex.hpp>
15 using namespace icinga;
17 DEFINE_TYPE_INSTANCE(Object);
20 static boost::mutex l_ObjectCountLock;
21 static std::map<String, int> l_ObjectCounts;
22 static Timer::Ptr l_ObjectCountTimer;
23 #endif /* I2_LEAK_DEBUG */
26 * Constructor for the Object class.
30 m_References.store(0);
33 m_LockOwner.store(decltype(m_LockOwner.load())());
38 * Destructor for the Object class.
45 * Returns a string representation for the object.
47 String Object::ToString() const
49 return "Object of type '" + GetReflectionType()->GetName() + "'";
54 * Checks if the calling thread owns the lock on this object.
56 * @returns True if the calling thread owns the lock, false otherwise.
58 bool Object::OwnsLock() const
60 return m_LockOwner.load() == std::this_thread::get_id();
64 void Object::SetField(int id, const Value&, bool, const Value&)
67 BOOST_THROW_EXCEPTION(std::runtime_error("Type field cannot be set."));
69 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
72 Value Object::GetField(int id) const
75 return GetReflectionType()->GetName();
77 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
80 bool Object::HasOwnField(const String& field) const
82 Type::Ptr type = GetReflectionType();
87 return type->GetFieldId(field) != -1;
90 bool Object::GetOwnField(const String& field, Value *result) const
92 Type::Ptr type = GetReflectionType();
97 int tid = type->GetFieldId(field);
102 *result = GetField(tid);
106 Value Object::GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const
108 Type::Ptr type = GetReflectionType();
113 int fid = type->GetFieldId(field);
116 return GetPrototypeField(const_cast<Object *>(this), field, true, debugInfo);
119 Field fieldInfo = type->GetFieldInfo(fid);
121 if (fieldInfo.Attributes & FANoUserView)
122 BOOST_THROW_EXCEPTION(ScriptError("Accessing the field '" + field + "' for type '" + type->GetName() + "' is not allowed in sandbox mode.", debugInfo));
125 return GetField(fid);
128 void Object::SetFieldByName(const String& field, const Value& value, bool overrideFrozen, const DebugInfo& debugInfo)
130 Type::Ptr type = GetReflectionType();
133 BOOST_THROW_EXCEPTION(ScriptError("Cannot set field on object.", debugInfo));
135 int fid = type->GetFieldId(field);
138 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' does not exist.", debugInfo));
141 SetField(fid, value);
142 } catch (const boost::bad_lexical_cast&) {
143 Field fieldInfo = type->GetFieldInfo(fid);
144 Type::Ptr ftype = Type::GetByName(fieldInfo.TypeName);
145 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "', expected '" + ftype->GetName() + "'", debugInfo));
146 } catch (const std::bad_cast&) {
147 Field fieldInfo = type->GetFieldInfo(fid);
148 Type::Ptr ftype = Type::GetByName(fieldInfo.TypeName);
149 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "', expected '" + ftype->GetName() + "'", debugInfo));
153 void Object::Validate(int types, const ValidationUtils& utils)
155 /* Nothing to do here. */
158 void Object::ValidateField(int id, const Lazy<Value>& lvalue, const ValidationUtils& utils)
160 /* Nothing to do here. */
163 void Object::NotifyField(int id, const Value& cookie)
165 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
168 Object::Ptr Object::NavigateField(int id) const
170 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
173 Object::Ptr Object::Clone() const
175 BOOST_THROW_EXCEPTION(std::runtime_error("Object cannot be cloned."));
178 Type::Ptr Object::GetReflectionType() const
180 return Object::TypeInstance;
183 Value icinga::GetPrototypeField(const Value& context, const String& field, bool not_found_error, const DebugInfo& debugInfo)
185 Type::Ptr ctype = context.GetReflectionType();
186 Type::Ptr type = ctype;
189 Object::Ptr object = type->GetPrototype();
191 if (object && object->HasOwnField(field))
192 return object->GetFieldByName(field, false, debugInfo);
194 type = type->GetBaseType();
198 BOOST_THROW_EXCEPTION(ScriptError("Invalid field access (for value of type '" + ctype->GetName() + "'): '" + field + "'", debugInfo));
204 void icinga::TypeAddObject(Object *object)
206 boost::mutex::scoped_lock lock(l_ObjectCountLock);
207 String typeName = Utility::GetTypeName(typeid(*object));
208 l_ObjectCounts[typeName]++;
211 void icinga::TypeRemoveObject(Object *object)
213 boost::mutex::scoped_lock lock(l_ObjectCountLock);
214 String typeName = Utility::GetTypeName(typeid(*object));
215 l_ObjectCounts[typeName]--;
218 static void TypeInfoTimerHandler()
220 boost::mutex::scoped_lock lock(l_ObjectCountLock);
222 typedef std::map<String, int>::value_type kv_pair;
223 for (kv_pair& kv : l_ObjectCounts) {
227 Log(LogInformation, "TypeInfo")
228 << kv.second << " " << kv.first << " objects";
234 INITIALIZE_ONCE([]() {
235 l_ObjectCountTimer = new Timer();
236 l_ObjectCountTimer->SetInterval(10);
237 l_ObjectCountTimer->OnTimerExpired.connect(std::bind(TypeInfoTimerHandler));
238 l_ObjectCountTimer->Start();
240 #endif /* I2_LEAK_DEBUG */
242 void icinga::intrusive_ptr_add_ref(Object *object)
245 if (object->m_References.fetch_add(1) == 0u)
246 TypeAddObject(object);
247 #else /* I2_LEAK_DEBUG */
248 object->m_References.fetch_add(1);
249 #endif /* I2_LEAK_DEBUG */
252 void icinga::intrusive_ptr_release(Object *object)
254 auto previous (object->m_References.fetch_sub(1));
256 if (previous == 1u) {
258 TypeRemoveObject(object);
259 #endif /* I2_LEAK_DEBUG */
265 void icinga::DefaultObjectFactoryCheckArgs(const std::vector<Value>& args)
268 BOOST_THROW_EXCEPTION(std::invalid_argument("Constructor does not take any arguments."));
271 void icinga::RequireNotNullInternal(const intrusive_ptr<Object>& object, const char *description)
274 BOOST_THROW_EXCEPTION(std::invalid_argument("Pointer must not be null: " + String(description)));