2013-10-26 09:41:45 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2017-01-10 15:54:22 +01:00
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
2013-10-26 09:41:45 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-11-04 23:14:34 +01:00
|
|
|
#ifndef TYPE_H
|
|
|
|
#define TYPE_H
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/i2-base.hpp"
|
2014-10-19 14:48:19 +02:00
|
|
|
#include "base/string.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/object.hpp"
|
|
|
|
#include "base/initialize.hpp"
|
2015-08-04 14:47:44 +02:00
|
|
|
#include <boost/function.hpp>
|
2015-02-25 14:03:15 +01:00
|
|
|
#include <vector>
|
2013-10-26 09:41:45 +02:00
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2015-03-11 16:06:58 +01:00
|
|
|
/* keep this in sync with tools/mkclass/classcompiler.hpp */
|
2014-10-26 19:59:49 +01:00
|
|
|
enum FieldAttribute
|
|
|
|
{
|
2015-03-11 16:06:58 +01:00
|
|
|
FAEphemeral = 1,
|
|
|
|
FAConfig = 2,
|
|
|
|
FAState = 4,
|
2015-10-20 08:20:35 +02:00
|
|
|
FARequired = 256,
|
|
|
|
FANavigation = 512,
|
|
|
|
FANoUserModify = 1024,
|
|
|
|
FANoUserView = 2048
|
2015-09-22 09:42:30 +02:00
|
|
|
};
|
2014-10-31 08:49:14 +01:00
|
|
|
|
|
|
|
class Type;
|
|
|
|
|
2013-11-04 23:14:34 +01:00
|
|
|
struct Field
|
2013-10-26 09:41:45 +02:00
|
|
|
{
|
|
|
|
int ID;
|
2014-11-10 20:06:28 +01:00
|
|
|
const char *TypeName;
|
2013-12-03 09:59:21 +01:00
|
|
|
const char *Name;
|
2015-09-22 09:42:30 +02:00
|
|
|
const char *NavigationName;
|
2014-11-30 23:32:13 +01:00
|
|
|
const char *RefTypeName;
|
2013-10-26 09:41:45 +02:00
|
|
|
int Attributes;
|
2015-08-25 13:53:43 +02:00
|
|
|
int ArrayRank;
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2015-09-22 09:42:30 +02:00
|
|
|
Field(int id, const char *type, const char *name, const char *navigationName, const char *reftype, int attributes, int arrayRank)
|
|
|
|
: ID(id), TypeName(type), Name(name), NavigationName(navigationName), RefTypeName(reftype), Attributes(attributes), ArrayRank(arrayRank)
|
2013-10-26 09:41:45 +02:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2013-12-18 10:18:57 +01:00
|
|
|
enum TypeAttribute
|
|
|
|
{
|
2014-10-25 09:14:56 +02:00
|
|
|
TAAbstract = 1
|
2013-12-18 10:18:57 +01:00
|
|
|
};
|
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
class ValidationUtils
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool ValidateName(const String& type, const String& name) const = 0;
|
|
|
|
};
|
|
|
|
|
2014-11-03 00:44:04 +01:00
|
|
|
class I2_BASE_API Type : public Object
|
2013-10-26 09:41:45 +02:00
|
|
|
{
|
|
|
|
public:
|
2015-08-04 14:47:44 +02:00
|
|
|
DECLARE_OBJECT(Type);
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2015-08-18 07:46:04 +02:00
|
|
|
virtual String ToString(void) const override;
|
2014-12-08 09:12:40 +01:00
|
|
|
|
2013-11-08 11:17:46 +01:00
|
|
|
virtual String GetName(void) const = 0;
|
2014-11-03 00:44:04 +01:00
|
|
|
virtual Type::Ptr GetBaseType(void) const = 0;
|
2013-12-18 10:18:57 +01:00
|
|
|
virtual int GetAttributes(void) const = 0;
|
2013-10-26 09:41:45 +02:00
|
|
|
virtual int GetFieldId(const String& name) const = 0;
|
2013-11-04 23:14:34 +01:00
|
|
|
virtual Field GetFieldInfo(int id) const = 0;
|
2013-10-26 09:41:45 +02:00
|
|
|
virtual int GetFieldCount(void) const = 0;
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2015-08-13 09:02:52 +02:00
|
|
|
String GetPluralName(void) const;
|
|
|
|
|
2016-03-29 14:42:32 +02:00
|
|
|
Object::Ptr Instantiate(const std::vector<Value>& args) const;
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2014-11-03 00:44:04 +01:00
|
|
|
bool IsAssignableFrom(const Type::Ptr& other) const;
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2013-12-18 10:18:57 +01:00
|
|
|
bool IsAbstract(void) const;
|
|
|
|
|
2014-12-12 15:19:23 +01:00
|
|
|
Object::Ptr GetPrototype(void) const;
|
|
|
|
void SetPrototype(const Object::Ptr& object);
|
|
|
|
|
2014-11-03 00:44:04 +01:00
|
|
|
static void Register(const Type::Ptr& type);
|
|
|
|
static Type::Ptr GetByName(const String& name);
|
2016-08-12 16:53:44 +02:00
|
|
|
static std::vector<Type::Ptr> GetAllTypes(void);
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2015-08-18 07:46:04 +02:00
|
|
|
virtual void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty) override;
|
|
|
|
virtual Value GetField(int id) const override;
|
2015-01-14 09:51:44 +01:00
|
|
|
|
2015-02-25 12:43:03 +01:00
|
|
|
virtual std::vector<String> GetLoadDependencies(void) const;
|
2015-08-04 14:47:44 +02:00
|
|
|
|
|
|
|
typedef boost::function<void (const Object::Ptr&, const Value&)> AttributeHandler;
|
|
|
|
virtual void RegisterAttributeHandler(int fieldId, const AttributeHandler& callback);
|
2015-02-25 12:43:03 +01:00
|
|
|
|
2014-11-07 12:32:25 +01:00
|
|
|
protected:
|
|
|
|
virtual ObjectFactory GetFactory(void) const = 0;
|
2014-12-12 15:19:23 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Object::Ptr m_Prototype;
|
2013-11-08 11:17:46 +01:00
|
|
|
};
|
|
|
|
|
2015-01-14 09:51:44 +01:00
|
|
|
class I2_BASE_API TypeType : public Type
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DECLARE_PTR_TYPEDEFS(Type);
|
|
|
|
|
2015-08-18 09:12:49 +02:00
|
|
|
virtual String GetName(void) const override;
|
|
|
|
virtual Type::Ptr GetBaseType(void) const override;
|
|
|
|
virtual int GetAttributes(void) const override;
|
|
|
|
virtual int GetFieldId(const String& name) const override;
|
|
|
|
virtual Field GetFieldInfo(int id) const override;
|
|
|
|
virtual int GetFieldCount(void) const override;
|
2015-08-04 14:47:44 +02:00
|
|
|
|
|
|
|
static Object::Ptr GetPrototype(void);
|
2015-01-14 09:51:44 +01:00
|
|
|
|
|
|
|
protected:
|
2015-08-18 09:12:49 +02:00
|
|
|
virtual ObjectFactory GetFactory(void) const override;
|
2015-01-14 09:51:44 +01:00
|
|
|
};
|
|
|
|
|
2013-11-08 11:17:46 +01:00
|
|
|
template<typename T>
|
2016-08-09 09:00:19 +02:00
|
|
|
class I2_BASE_API TypeImpl
|
2013-11-08 11:17:46 +01:00
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2013-11-08 16:07:21 +01:00
|
|
|
#define REGISTER_TYPE(type) \
|
2016-08-27 09:35:08 +02:00
|
|
|
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
|
|
|
|
icinga::Type::Ptr t = new TypeImpl<type>(); \
|
|
|
|
type::TypeInstance = t; \
|
|
|
|
icinga::Type::Register(t); \
|
|
|
|
}, 10); \
|
2014-11-07 12:32:25 +01:00
|
|
|
DEFINE_TYPE_INSTANCE(type)
|
|
|
|
|
2015-08-04 14:47:44 +02:00
|
|
|
#define REGISTER_TYPE_WITH_PROTOTYPE(type, prototype) \
|
2016-08-27 09:35:08 +02:00
|
|
|
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
|
|
|
|
icinga::Type::Ptr t = new TypeImpl<type>(); \
|
|
|
|
t->SetPrototype(prototype); \
|
|
|
|
type::TypeInstance = t; \
|
|
|
|
icinga::Type::Register(t); \
|
|
|
|
}, 10); \
|
2015-08-04 14:47:44 +02:00
|
|
|
DEFINE_TYPE_INSTANCE(type)
|
|
|
|
|
2014-11-07 12:32:25 +01:00
|
|
|
#define DEFINE_TYPE_INSTANCE(type) \
|
2014-11-08 21:17:16 +01:00
|
|
|
Type::Ptr type::TypeInstance
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
}
|
|
|
|
|
2013-11-04 23:14:34 +01:00
|
|
|
#endif /* TYPE_H */
|