From f7e28c2533ab2ea358a722a8492d44b9469c840e Mon Sep 17 00:00:00 2001 From: "Morten V. Pedersen" Date: Mon, 3 Feb 2025 23:02:51 +0100 Subject: [PATCH] WIP --- src/abacus/required_metric.hpp | 258 --------------------------------- 1 file changed, 258 deletions(-) delete mode 100644 src/abacus/required_metric.hpp diff --git a/src/abacus/required_metric.hpp b/src/abacus/required_metric.hpp deleted file mode 100644 index 00d1724..0000000 --- a/src/abacus/required_metric.hpp +++ /dev/null @@ -1,258 +0,0 @@ -// Copyright (c) Steinwurf ApS 2020. -// All Rights Reserved -// -// Distributed under the "BSD License". See the accompanying LICENSE.rst file. - -#pragma once - -#include -#include -#include -#include - -#include "detail/has_arithmetic_operators.hpp" - -#include "version.hpp" - -namespace abacus -{ -inline namespace STEINWURF_ABACUS_VERSION -{ -/// An optional metric class for most metrics -template -struct required_metric -{ - using value_type = typename Metric::type; - - /// Default constructor - required_metric() = default; - - /// Constructor - /// @param memory The memory to use for the metric, note that the memory - /// must be at least sizeof(value_type) + 1 bytes long. - /// @param value The initial value of the metric - required_metric(uint8_t* memory) - { - assert(memory != nullptr); - m_memory = memory; - - // Set the initialized flag to true as this should always be the case - // for required metrics. - m_memory[0] = 1; - } - - /// Check if the metric is initialized - /// @return true if the metric is initialized - auto is_initialized() const -> bool - { - return m_memory != nullptr; - } - - /// Get the value of the metric - /// @return The value of the metric - auto value() const -> value_type - { - assert(is_initialized()); - - value_type value; - std::memcpy(&value, m_memory + 1, sizeof(value_type)); - return value; - } - - /// Assign a new value to the metric - /// @param value The value to assign - auto set_value(value_type value) -> void - { - assert(is_initialized()); - - if constexpr (std::is_floating_point_v) - { - assert(!std::isnan(value) && "Cannot assign a NaN"); - assert(!std::isinf(value) && "Cannot assign an Inf/-Inf value"); - } - - std::memcpy(m_memory + 1, &value, sizeof(value_type)); - } - - /// Assign the metric a new value - /// @param value The value to assign - /// @return the metric with the new value - auto operator=(value_type value) -> required_metric& - { - set_value(value); - return *this; - } - -public: - /// Arithmetic operators - - /// Increment the metric - /// @param increment The value to add - /// @return The result of the arithmetic - auto operator+=(value_type increment) -> required_metric& - { - set_value(increment + value()); - return *this; - } - - /// Decrement the metric - /// @param decrement The value to subtract - /// @return The result of the arithmetic - auto operator-=(value_type decrement) -> required_metric& - { - set_value(value() - decrement); - return *this; - } - - /// Increment the value of the metric - /// @return The result of the arithmetic - auto operator++() -> required_metric& - { - set_value(value() + 1); - return *this; - } - - /// Decrement the value of the metric - /// @return The result of the arithmetic - auto operator--() -> required_metric& - { - set_value(value() - 1); - return *this; - } - -protected: - /// The metric memory - uint8_t* m_memory = nullptr; -}; - -/// Enum specializations -template <> -struct required_metric -{ - /// Default constructor - required_metric() = default; - - /// Constructor - /// @param memory The memory to use for the metric, note that the memory - /// must be at least sizeof(value_type) + 1 bytes long. - /// @param value The initial value of the metric - required_metric(uint8_t* memory) - { - assert(memory != nullptr); - m_memory = memory; - - // Set the initialized flag to true as this should always be the case - // for required metrics. - m_memory[0] = 1; - } - - /// Check if the metric is initialized - /// @return true if the metric is initialized - auto is_initialized() const -> bool - { - return m_memory != nullptr; - } - - /// The the value as a specific enum type - /// @return The value of the metric as the enum type - template - auto value() const -> T - { - static_assert(std::is_enum_v); - assert(is_initialized()); - - return static_cast(m_memory[1]); - } - - /// Assign a new value to the metric - /// @param value The value to assign - template - auto set_value(T value) -> void - { - // @todo reenable this check - // static_assert(std::is_enum_v); - - assert(static_cast(value) <= - std::numeric_limits::max() && - "The value is too large to fit in the enum"); - assert(static_cast(value) >= - std::numeric_limits::min() && - "The value is too small to fit in the enum"); - - assert(is_initialized()); - - m_memory[1] = static_cast(value); - } - - /// Assign the metric a new value - template - auto operator=(T value) -> required_metric& - { - set_value(value); - return *this; - } - -protected: - /// The metric memory - uint8_t* m_memory = nullptr; -}; - -/// Boolean specializations -template <> -struct required_metric -{ - /// Default constructor - required_metric() = default; - - /// Constructor - /// @param memory The memory to use for the metric, note that the memory - /// must be at least sizeof(value_type) + 1 bytes long. - /// @param value The initial value of the metric - required_metric(uint8_t* memory) - { - assert(memory != nullptr); - m_memory = memory; - - // Set the initialized flag to true as this should always be the case - // for required metrics. - m_memory[0] = 1; - } - - /// Check if the metric is initialized - /// @return true if the metric is initialized - auto is_initialized() const -> bool - { - return m_memory != nullptr; - } - - /// Get the value of the metric - /// @return The value of the metric - auto value() const -> bool - { - assert(is_initialized()); - return static_cast(m_memory[1]); - } - - /// Assign a new value to the metric - /// @param value The value to assign - auto set_value(bool value) -> void - { - assert(is_initialized()); - m_memory[1] = static_cast(value); - } - - /// Assign the metric a new value - /// @param value The value to assign - /// @return the metric with the new value - auto operator=(bool value) -> required_metric& - { - set_value(value); - return *this; - } - -private: - /// The metric memory - uint8_t* m_memory = nullptr; -}; -} -}