icinga2/lib/base/registry.hpp
Yonas Habteab 91c7e60df8 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-02-04 14:00:05 +01:00

96 lines
1.8 KiB
C++

// SPDX-FileCopyrightText: 2012 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
#ifndef REGISTRY_H
#define REGISTRY_H
#include "base/i2-base.hpp"
#include "base/atomic.hpp"
#include "base/exception.hpp"
#include "base/string.hpp"
#include "base/singleton.hpp"
#include <shared_mutex>
#include <stdexcept>
#include <unordered_map>
#include <mutex>
namespace icinga
{
/**
* A registry.
*
* @ingroup base
*/
template<typename T>
class Registry
{
public:
typedef std::unordered_map<String, T> ItemMap;
static Registry* GetInstance()
{
return Singleton<Registry>::GetInstance();
}
void Register(const String& name, const T& item)
{
std::unique_lock lock (m_Mutex);
if (m_Frozen) {
BOOST_THROW_EXCEPTION(std::logic_error("Registry is read-only and must not be modified."));
}
m_Items[name] = item;
}
T GetItem(const String& name) const
{
auto lock (ReadLockUnlessFrozen());
auto it = m_Items.find(name);
if (it == m_Items.end())
return T();
return it->second;
}
ItemMap GetItems() const
{
auto lock (ReadLockUnlessFrozen());
return m_Items; /* Makes a copy of the map. */
}
/**
* Freeze the registry, preventing further updates.
*
* This only prevents inserting, replacing or deleting values from the registry.
* This operation has no effect on objects referenced by the values, these remain mutable if they were before.
*/
void Freeze()
{
std::unique_lock lock (m_Mutex);
m_Frozen.store(true);
}
private:
mutable std::shared_mutex m_Mutex;
Atomic<bool> m_Frozen {false};
ItemMap m_Items;
std::shared_lock<std::shared_mutex> ReadLockUnlessFrozen() const
{
if (m_Frozen.load(std::memory_order_relaxed)) {
return {};
}
return std::shared_lock(m_Mutex);
}
};
}
#endif /* REGISTRY_H */