1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2 
3 #ifndef CONFIGTYPE_H
4 #define CONFIGTYPE_H
5 
6 #include "base/i2-base.hpp"
7 #include "base/object.hpp"
8 #include "base/type.hpp"
9 #include "base/dictionary.hpp"
10 #include <mutex>
11 
12 namespace icinga
13 {
14 
15 class ConfigObject;
16 
17 class ConfigType
18 {
19 public:
20 	virtual ~ConfigType();
21 
22 	intrusive_ptr<ConfigObject> GetObject(const String& name) const;
23 
24 	void RegisterObject(const intrusive_ptr<ConfigObject>& object);
25 	void UnregisterObject(const intrusive_ptr<ConfigObject>& object);
26 
27 	std::vector<intrusive_ptr<ConfigObject> > GetObjects() const;
28 
29 	template<typename T>
Get()30 	static TypeImpl<T> *Get()
31 	{
32 		typedef TypeImpl<T> ObjType;
33 		return static_cast<ObjType *>(T::TypeInstance.get());
34 	}
35 
36 	template<typename T>
GetObjectsByType()37 	static std::vector<intrusive_ptr<T> > GetObjectsByType()
38 	{
39 		std::vector<intrusive_ptr<ConfigObject> > objects = GetObjectsHelper(T::TypeInstance.get());
40 		std::vector<intrusive_ptr<T> > result;
41 		result.reserve(objects.size());
42 for (const auto& object : objects) {
43 			result.push_back(static_pointer_cast<T>(object));
44 		}
45 		return result;
46 	}
47 
48 	int GetObjectCount() const;
49 
50 private:
51 	typedef std::map<String, intrusive_ptr<ConfigObject> > ObjectMap;
52 	typedef std::vector<intrusive_ptr<ConfigObject> > ObjectVector;
53 
54 	mutable std::mutex m_Mutex;
55 	ObjectMap m_ObjectMap;
56 	ObjectVector m_ObjectVector;
57 
58 	static std::vector<intrusive_ptr<ConfigObject> > GetObjectsHelper(Type *type);
59 };
60 
61 }
62 
63 #endif /* CONFIGTYPE_H */
64