icinga2/lib/base/generator.hpp

95 lines
2.8 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: 2025 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
2025-07-01 03:49:31 -04:00
#pragma once
#include "base/i2-base.hpp"
#include "base/value.hpp"
#include <optional>
namespace icinga
{
/**
* Abstract base class for generators that produce a sequence of Values.
2025-07-01 03:49:31 -04:00
*
* @note Any instance of a Generator should be treated as an @c Array -like object that produces its elements
* on-the-fly.
2025-07-01 03:49:31 -04:00
*
* @ingroup base
*/
class Generator : public Object
2025-07-01 03:49:31 -04:00
{
public:
DECLARE_PTR_TYPEDEFS(Generator);
2025-07-01 03:49:31 -04:00
/**
* Produces the next Value in the sequence.
*
* This method returns the next Value produced by the generator. If the generator is exhausted,
* it returns std::nullopt for all subsequent calls to this method.
2025-07-01 03:49:31 -04:00
*
* @return The next Value in the sequence, or std::nullopt if the generator is exhausted.
2025-07-01 03:49:31 -04:00
*/
virtual std::optional<Value> Next() = 0;
};
2025-07-01 03:49:31 -04:00
/**
* A generator that transforms elements of a container into Values using a provided transformation function.
*
* This class takes a container and a transformation function as input. It uses the transformation function
* to convert each element of the container into a Value. The generator produces Values on-the-fly as they
* are requested via the `Next()` method.
*
* @tparam Container The type of the container holding the elements to be transformed.
*
* @ingroup base
*/
template<typename Container>
class ValueGenerator final : public Generator
{
public:
DECLARE_PTR_TYPEDEFS(ValueGenerator);
// The type of elements in the container and the type to be passed to the transformation function.
using InputType = typename Container::value_type;
// The type of the transformation function that takes an element of the container and returns a Value.
using TransformFunc = std::function<Value (const InputType&)>;
/**
* Constructs a ValueGenerator with the given container and transformation function.
*
* The generator will iterate over the elements of the container, applying the transformation function
* to each element to produce values on-the-fly. You must ensure that the container remains valid for
* the lifetime of the generator.
*
* @param container The container holding the elements to be transformed.
* @param generator The transformation function to convert elements into Values.
*/
ValueGenerator(Container& container, TransformFunc generator)
: m_It{container.begin()}, m_End{container.end()}, m_Func{std::move(generator)}
2025-07-01 03:49:31 -04:00
{
}
std::optional<Value> Next() override
2025-07-01 03:49:31 -04:00
{
if (m_It == m_End) {
return std::nullopt;
}
auto next = m_Func(*m_It);
++m_It;
return next;
2025-07-01 03:49:31 -04:00
}
private:
using Iterator = typename Container::iterator;
Iterator m_It; // Current iterator position.
Iterator m_End; // End iterator position.
TransformFunc m_Func; // The transformation function.
2025-07-01 03:49:31 -04:00
};
} // namespace icinga