2013-10-26 03:41:45 -04:00
|
|
|
/******************************************************************************
|
|
|
|
|
* Icinga 2 *
|
2016-01-12 02:29:59 -05:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2013-10-26 03:41:45 -04: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 17:14:34 -05:00
|
|
|
#ifndef TYPE_H
|
|
|
|
|
#define TYPE_H
|
2013-10-26 03:41:45 -04:00
|
|
|
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/i2-base.hpp"
|
2014-10-19 08:48:19 -04:00
|
|
|
#include "base/string.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/object.hpp"
|
|
|
|
|
#include "base/initialize.hpp"
|
2015-08-04 08:47:44 -04:00
|
|
|
#include <boost/function.hpp>
|
2015-02-25 08:03:15 -05:00
|
|
|
#include <vector>
|
2013-10-26 03:41:45 -04:00
|
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
|
{
|
|
|
|
|
|
2015-03-11 11:06:58 -04:00
|
|
|
/* keep this in sync with tools/mkclass/classcompiler.hpp */
|
2014-10-26 14:59:49 -04:00
|
|
|
enum FieldAttribute
|
|
|
|
|
{
|
2015-03-11 11:06:58 -04:00
|
|
|
FAEphemeral = 1,
|
|
|
|
|
FAConfig = 2,
|
|
|
|
|
FAState = 4,
|
2015-10-20 02:20:35 -04:00
|
|
|
FARequired = 256,
|
|
|
|
|
FANavigation = 512,
|
|
|
|
|
FANoUserModify = 1024,
|
|
|
|
|
FANoUserView = 2048
|
2015-09-22 03:42:30 -04:00
|
|
|
};
|
2014-10-31 03:49:14 -04:00
|
|
|
|
|
|
|
|
class Type;
|
|
|
|
|
|
2013-11-04 17:14:34 -05:00
|
|
|
struct Field
|
2013-10-26 03:41:45 -04:00
|
|
|
{
|
|
|
|
|
int ID;
|
2014-11-10 14:06:28 -05:00
|
|
|
const char *TypeName;
|
2013-12-03 03:59:21 -05:00
|
|
|
const char *Name;
|
2015-09-22 03:42:30 -04:00
|
|
|
const char *NavigationName;
|
2014-11-30 17:32:13 -05:00
|
|
|
const char *RefTypeName;
|
2013-10-26 03:41:45 -04:00
|
|
|
int Attributes;
|
2015-08-25 07:53:43 -04:00
|
|
|
int ArrayRank;
|
2013-10-26 03:41:45 -04:00
|
|
|
|
2015-09-22 03:42:30 -04: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 03:41:45 -04:00
|
|
|
{ }
|
|
|
|
|
};
|
|
|
|
|
|
2013-12-18 04:18:57 -05:00
|
|
|
enum TypeAttribute
|
|
|
|
|
{
|
2014-10-25 03:14:56 -04:00
|
|
|
TAAbstract = 1
|
2013-12-18 04:18:57 -05:00
|
|
|
};
|
|
|
|
|
|
2014-11-30 17:32:13 -05:00
|
|
|
class ValidationUtils
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual bool ValidateName(const String& type, const String& name) const = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-02 18:44:04 -05:00
|
|
|
class I2_BASE_API Type : public Object
|
2013-10-26 03:41:45 -04:00
|
|
|
{
|
|
|
|
|
public:
|
2015-08-04 08:47:44 -04:00
|
|
|
DECLARE_OBJECT(Type);
|
2013-11-08 05:17:46 -05:00
|
|
|
|
2015-08-18 01:46:04 -04:00
|
|
|
virtual String ToString(void) const override;
|
2014-12-08 03:12:40 -05:00
|
|
|
|
2013-11-08 05:17:46 -05:00
|
|
|
virtual String GetName(void) const = 0;
|
2014-11-02 18:44:04 -05:00
|
|
|
virtual Type::Ptr GetBaseType(void) const = 0;
|
2013-12-18 04:18:57 -05:00
|
|
|
virtual int GetAttributes(void) const = 0;
|
2013-10-26 03:41:45 -04:00
|
|
|
virtual int GetFieldId(const String& name) const = 0;
|
2013-11-04 17:14:34 -05:00
|
|
|
virtual Field GetFieldInfo(int id) const = 0;
|
2013-10-26 03:41:45 -04:00
|
|
|
virtual int GetFieldCount(void) const = 0;
|
2013-11-08 05:17:46 -05:00
|
|
|
|
2015-08-13 03:02:52 -04:00
|
|
|
String GetPluralName(void) const;
|
|
|
|
|
|
2016-03-29 08:42:32 -04:00
|
|
|
Object::Ptr Instantiate(const std::vector<Value>& args) const;
|
2013-11-08 05:17:46 -05:00
|
|
|
|
2014-11-02 18:44:04 -05:00
|
|
|
bool IsAssignableFrom(const Type::Ptr& other) const;
|
2013-11-08 05:17:46 -05:00
|
|
|
|
2013-12-18 04:18:57 -05:00
|
|
|
bool IsAbstract(void) const;
|
|
|
|
|
|
2014-12-12 09:19:23 -05:00
|
|
|
Object::Ptr GetPrototype(void) const;
|
|
|
|
|
void SetPrototype(const Object::Ptr& object);
|
|
|
|
|
|
2014-11-02 18:44:04 -05:00
|
|
|
static void Register(const Type::Ptr& type);
|
|
|
|
|
static Type::Ptr GetByName(const String& name);
|
2013-11-08 05:17:46 -05:00
|
|
|
|
2015-08-18 01:46:04 -04: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 03:51:44 -05:00
|
|
|
|
2015-02-25 06:43:03 -05:00
|
|
|
virtual std::vector<String> GetLoadDependencies(void) const;
|
2015-08-04 08:47:44 -04:00
|
|
|
|
|
|
|
|
typedef boost::function<void (const Object::Ptr&, const Value&)> AttributeHandler;
|
|
|
|
|
virtual void RegisterAttributeHandler(int fieldId, const AttributeHandler& callback);
|
2015-02-25 06:43:03 -05:00
|
|
|
|
2014-11-07 06:32:25 -05:00
|
|
|
protected:
|
|
|
|
|
virtual ObjectFactory GetFactory(void) const = 0;
|
2014-12-12 09:19:23 -05:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Object::Ptr m_Prototype;
|
2013-11-08 05:17:46 -05:00
|
|
|
};
|
|
|
|
|
|
2015-01-14 03:51:44 -05:00
|
|
|
class I2_BASE_API TypeType : public Type
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
DECLARE_PTR_TYPEDEFS(Type);
|
|
|
|
|
|
2015-08-18 03:12:49 -04: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 08:47:44 -04:00
|
|
|
|
|
|
|
|
static Object::Ptr GetPrototype(void);
|
2015-01-14 03:51:44 -05:00
|
|
|
|
|
|
|
|
protected:
|
2015-08-18 03:12:49 -04:00
|
|
|
virtual ObjectFactory GetFactory(void) const override;
|
2015-01-14 03:51:44 -05:00
|
|
|
};
|
|
|
|
|
|
2013-11-08 05:17:46 -05:00
|
|
|
template<typename T>
|
|
|
|
|
class TypeImpl
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
2013-11-08 10:07:21 -05:00
|
|
|
#define REGISTER_TYPE(type) \
|
2014-08-31 05:01:37 -04:00
|
|
|
namespace { namespace UNIQUE_NAME(rt) { \
|
2014-10-31 03:49:14 -04:00
|
|
|
void RegisterType ## type(void) \
|
2013-11-08 05:17:46 -05:00
|
|
|
{ \
|
2014-11-08 15:17:16 -05:00
|
|
|
icinga::Type::Ptr t = new TypeImpl<type>(); \
|
2014-11-07 06:32:25 -05:00
|
|
|
type::TypeInstance = t; \
|
2013-11-08 15:37:21 -05:00
|
|
|
icinga::Type::Register(t); \
|
2013-11-08 05:17:46 -05:00
|
|
|
} \
|
|
|
|
|
\
|
2015-03-18 08:24:31 -04:00
|
|
|
INITIALIZE_ONCE_WITH_PRIORITY(RegisterType ## type, 10); \
|
2014-11-07 06:32:25 -05:00
|
|
|
} } \
|
|
|
|
|
DEFINE_TYPE_INSTANCE(type)
|
|
|
|
|
|
2015-08-04 08:47:44 -04:00
|
|
|
#define REGISTER_TYPE_WITH_PROTOTYPE(type, prototype) \
|
|
|
|
|
namespace { namespace UNIQUE_NAME(rt) { \
|
|
|
|
|
void RegisterType ## type(void) \
|
|
|
|
|
{ \
|
|
|
|
|
icinga::Type::Ptr t = new TypeImpl<type>(); \
|
|
|
|
|
t->SetPrototype(prototype); \
|
|
|
|
|
type::TypeInstance = t; \
|
|
|
|
|
icinga::Type::Register(t); \
|
|
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
INITIALIZE_ONCE_WITH_PRIORITY(RegisterType ## type, 10); \
|
|
|
|
|
} } \
|
|
|
|
|
DEFINE_TYPE_INSTANCE(type)
|
|
|
|
|
|
2014-11-07 06:32:25 -05:00
|
|
|
#define DEFINE_TYPE_INSTANCE(type) \
|
2014-11-08 15:17:16 -05:00
|
|
|
Type::Ptr type::TypeInstance
|
2013-11-08 05:17:46 -05:00
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
}
|
|
|
|
|
|
2013-11-04 17:14:34 -05:00
|
|
|
#endif /* TYPE_H */
|