icinga2/lib/base/namespace.hpp
Julian Brost fbb68dbcd0 Namespace: replace behavior classes with a bool
In essence, namespace behaviors acted as hooks for update operations on
namespaces. Two behaviors were implemented:

- `NamespaceBehavior`: allows the update operation unless it acts on a value
  that itself was explicitly marked as constant.
- `ConstNamespaceBehavior`: initially allows insert operations but marks the
  individual values as const. Additionally provides a `Freeze()` member
  function. After this was called, updates are rejected unless a special
  `overrideFrozen` flag is set explicitly.

This marvel of object-oriented programming can be replaced with a simple bool.
This commit basically replaces `Namespace::m_Behavior` with
`Namespace::m_ConstValues` and inlines the behavior functions where they were
called. While doing so, the code was slightly simplified by assuming that
`m_ConstValues` is true if `m_Frozen` is true. This is similar to what the API
allowed in the old code as you could only freeze a `ConstNamespaceBehavior`.
However, this PR moves the `Freeze()` member function and the related
`m_Freeze` member variable to the `Namespace` class. So now the API allows any
namespace to be frozen. The new code also makes sense with the previously
mentioned simplification: a `Namespace` with `m_ConstValues = false` can be
modified without restrictions until `Freeze()` is called. When this is done, it
becomes read-only.

The changes outside of `namespace.*` just adapt the code to the slightly
changed API.
2022-12-09 09:25:46 +01:00

97 lines
2.6 KiB
C++

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#ifndef NAMESPACE_H
#define NAMESPACE_H
#include "base/i2-base.hpp"
#include "base/object.hpp"
#include "base/shared-object.hpp"
#include "base/value.hpp"
#include "base/debuginfo.hpp"
#include <map>
#include <vector>
#include <memory>
namespace icinga
{
struct NamespaceValue : public SharedObject
{
DECLARE_PTR_TYPEDEFS(NamespaceValue);
virtual Value Get(const DebugInfo& debugInfo = DebugInfo()) const = 0;
virtual void Set(const Value& value, bool overrideFrozen, const DebugInfo& debugInfo = DebugInfo()) = 0;
};
struct EmbeddedNamespaceValue : public NamespaceValue
{
EmbeddedNamespaceValue(const Value& value);
Value Get(const DebugInfo& debugInfo) const override;
void Set(const Value& value, bool overrideFrozen, const DebugInfo& debugInfo) override;
private:
Value m_Value;
};
struct ConstEmbeddedNamespaceValue : public EmbeddedNamespaceValue
{
using EmbeddedNamespaceValue::EmbeddedNamespaceValue;
void Set(const Value& value, bool overrideFrozen, const DebugInfo& debugInfo) override;
};
/**
* A namespace.
*
* @ingroup base
*/
class Namespace final : public Object
{
public:
DECLARE_OBJECT(Namespace);
typedef std::map<String, NamespaceValue::Ptr>::iterator Iterator;
typedef std::map<String, NamespaceValue::Ptr>::value_type Pair;
explicit Namespace(bool constValues = false);
Value Get(const String& field) const;
bool Get(const String& field, Value *value) const;
void Set(const String& field, const Value& value, bool overrideFrozen = false);
bool Contains(const String& field) const;
void Remove(const String& field, bool overrideFrozen = false);
void Freeze();
NamespaceValue::Ptr GetAttribute(const String& field) const;
void SetAttribute(const String& field, const NamespaceValue::Ptr& nsVal);
void RemoveAttribute(const String& field);
Iterator Begin();
Iterator End();
size_t GetLength() const;
Value GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const override;
void SetFieldByName(const String& field, const Value& value, bool overrideFrozen, const DebugInfo& debugInfo) override;
bool HasOwnField(const String& field) const override;
bool GetOwnField(const String& field, Value *result) const override;
static Object::Ptr GetPrototype();
private:
std::map<String, NamespaceValue::Ptr> m_Data;
bool m_ConstValues;
bool m_Frozen;
};
Namespace::Iterator begin(const Namespace::Ptr& x);
Namespace::Iterator end(const Namespace::Ptr& x);
}
extern template class std::map<icinga::String, std::shared_ptr<icinga::NamespaceValue> >;
#endif /* NAMESPACE_H */