icinga2/lib/base/type.hpp

165 lines
4.3 KiB
C++
Raw Permalink Normal View History

Replace all existing copyright headers with `SPDX` headers I've used the following command to replace the original copyright header lines in a C-style comment block: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{/\*[^*]*\(\s*c\s*\)\s*(\d{4})\s*Icinga\s+GmbH[^*]*\*/}{// SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n// SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ``` For files that use shell-style comments (#) like CMakeLists.txt, I've used this command: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{#.*\(\s*c\s*\)\s(\d{4})\sIcinga\s+GmbH.*}{# SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n# SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ``` And for SQL files: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{--.*\(c\)\s(\d{4})\sIcinga\sGmbH.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{-- Copyright \(c\)\s(\d{4})\sIcinga\s+Development\sTeam.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ```
2026-01-27 09:06:40 -05:00
// SPDX-FileCopyrightText: 2012 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
2013-10-26 03:41:45 -04: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"
#include "base/string.hpp"
2014-05-25 10:23:35 -04:00
#include "base/object.hpp"
#include "base/initialize.hpp"
#include <unordered_set>
2015-02-25 08:03:15 -05:00
#include <vector>
2013-10-26 03:41:45 -04:00
namespace icinga
{
/* keep this in sync with tools/mkclass/classcompiler.hpp */
2014-10-26 14:59:49 -04:00
enum FieldAttribute
{
FAEphemeral = 1,
FAConfig = 2,
FAState = 4,
FARequired = 256,
FANavigation = 512,
FANoUserModify = 1024,
FANoUserView = 2048,
FADeprecated = 4096,
};
class Type;
struct Field
2013-10-26 03:41:45 -04:00
{
int ID;
const char *TypeName;
2013-12-03 03:59:21 -05:00
const char *Name;
const char *NavigationName;
const char *RefTypeName;
2013-10-26 03:41:45 -04:00
int Attributes;
int ArrayRank;
2013-10-26 03:41:45 -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
{ }
};
enum TypeAttribute
{
TAAbstract = 1
};
class ValidationUtils
{
public:
virtual bool ValidateName(const String& type, const String& name) const = 0;
};
2017-12-31 01:22:16 -05:00
class Type : public Object
2013-10-26 03:41:45 -04:00
{
public:
DECLARE_OBJECT(Type);
String ToString() const override;
virtual String GetName() const = 0;
virtual Type::Ptr GetBaseType() const = 0;
virtual int GetAttributes() const = 0;
2013-10-26 03:41:45 -04:00
virtual int GetFieldId(const String& name) const = 0;
virtual Field GetFieldInfo(int id) const = 0;
virtual int GetFieldCount() const = 0;
String GetPluralName() const;
2016-03-29 08:42:32 -04:00
Object::Ptr Instantiate(const std::vector<Value>& args) const;
2014-11-02 18:44:04 -05:00
bool IsAssignableFrom(const Type::Ptr& other) const;
bool IsAbstract() const;
Object::Ptr GetPrototype() 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);
static std::vector<Type::Ptr> GetAllTypes();
/**
* Returns a list of config types sorted by their "load_after" dependencies.
*
* All dependencies of a given type are listed at a lower index than that of the type itself. In other words,
* if a `Service` type load depends on the `Host` and `ApiListener` types, the Host and ApiListener types are
* guaranteed to appear first on the list. Nevertheless, the order of the Host and ApiListener types themselves
* is arbitrary if the two types are not dependent.
*
* It should be noted that this method will fail fatally when used prior to the completion
* of namespace initialization.
*
* @return std::vector<Type::Ptr>
*/
static const std::vector<Ptr>& GetConfigTypesSortedByLoadDependencies();
void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty) override;
Value GetField(int id) const override;
virtual const std::unordered_set<Type*>& GetLoadDependencies() const;
virtual int GetActivationPriority() const;
2017-12-13 06:54:14 -05:00
typedef std::function<void (const Object::Ptr&, const Value&)> AttributeHandler;
virtual void RegisterAttributeHandler(int fieldId, const AttributeHandler& callback);
2014-11-07 06:32:25 -05:00
protected:
virtual ObjectFactory GetFactory() const = 0;
private:
Object::Ptr m_Prototype;
};
2018-01-04 00:11:04 -05:00
class TypeType final : public Type
{
public:
DECLARE_PTR_TYPEDEFS(Type);
String GetName() const override;
Type::Ptr GetBaseType() const override;
int GetAttributes() const override;
int GetFieldId(const String& name) const override;
Field GetFieldInfo(int id) const override;
int GetFieldCount() const override;
2017-12-13 06:54:14 -05:00
static Object::Ptr GetPrototype();
protected:
ObjectFactory GetFactory() const override;
};
template<typename T>
2017-12-31 01:22:16 -05:00
class TypeImpl
{
};
/* Ensure that the priority is lower than the basic namespace initialization in scriptframe.cpp. */
#define REGISTER_TYPE(type) \
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
icinga::Type::Ptr t = new TypeImpl<type>(); \
type::TypeInstance = t; \
icinga::Type::Register(t); \
}, InitializePriority::RegisterTypes); \
2014-11-07 06:32:25 -05:00
DEFINE_TYPE_INSTANCE(type)
#define REGISTER_TYPE_WITH_PROTOTYPE(type, prototype) \
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
icinga::Type::Ptr t = new TypeImpl<type>(); \
t->SetPrototype(prototype); \
type::TypeInstance = t; \
icinga::Type::Register(t); \
}, InitializePriority::RegisterTypes); \
DEFINE_TYPE_INSTANCE(type)
2014-11-07 06:32:25 -05:00
#define DEFINE_TYPE_INSTANCE(type) \
Type::Ptr type::TypeInstance
2013-10-26 03:41:45 -04:00
}
#endif /* TYPE_H */