Skip to content

Commit

Permalink
Avoid repeated code to map enum values to designations
Browse files Browse the repository at this point in the history
The goals behind these changes is to create a structure that holds the mapping between enum value and the corresponding designation, and then use it to make conversions.

This approach avoids needless source code repetition (including repeated designation values), and allows to easily support new enum values/designation, by simply adding new entries to the mapping table.

Re ECFLOW-1931
  • Loading branch information
marcosbento committed Dec 8, 2023
1 parent 9b19f03 commit 88e9f3e
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 299 deletions.
124 changes: 124 additions & 0 deletions ACore/src/ecflow/core/Enumerate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright 2009- ECMWF.
*
* This software is licensed under the terms of the Apache Licence version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation
* nor does it submit to any jurisdiction.
*/

#ifndef ecflow_core_Enumerate_HPP
#define ecflow_core_Enumerate_HPP

#include <optional>
#include <string>

#include <string_view>

namespace ecf {

namespace detail {

/**
* EnumTraits defines the mapping between a set of enum values and their designation.
*
* EnumTraits must define:
* - the mapping `map`, provided as an std::array composes of an std::pair<E, const char *> for each enum value
* - the `size`, holding the number os entries in `map`
*
* @tparam E
*/

template <typename E>
struct EnumTraits
{
};

} // namespace detail

template <typename E, typename TRAITS = detail::EnumTraits<E>>
struct Enumerate
{
public:
using enum_t = E;
using string_t = std::string_view;

/**
* Convert the given enum value to its designation
*
* @param e the enum value
* @return the designation, in case it exists; an empty optional, otherwise
*/
static constexpr inline std::optional<string_t> to_string(enum_t e) noexcept {
auto found = std::find_if(
std::begin(TRAITS::map), std::end(TRAITS::map), [&](const auto& item) { return item.first == e; });
if (found == std::end(TRAITS::map)) {
return std::nullopt;
}
return std::make_optional(found->second);
}

/**
* Convert the given designation to the related enum value
*
* @param s the designation
* @return the enum value, in case it exists; an empty optional, otherwise
*/
static constexpr inline std::optional<E> to_enum(string_t s) noexcept {
auto found = std::find_if(
std::begin(TRAITS::map), std::end(TRAITS::map), [&](const auto& item) { return item.second == s; });
if (found == std::end(TRAITS::map)) {
return std::nullopt;
}
return std::make_optional(found->first);
}

/**
* Checks if the given designation is valid (i.e. has a related enum value)
*
* @param s the designation
* @return true, if valid; false, otherwise
*/
static bool inline is_valid(string_t s) {
auto found = std::find_if(
std::begin(TRAITS::map), std::end(TRAITS::map), [&](const auto& item) { return item.second == s; });
return found != std::end(TRAITS::map);
}

/**
* The number of mapped enum values
*/
static const size_t size = TRAITS::size;

/**
* The vector of mapped enum values
*/
static auto enums() {
std::vector<E> result;
result.reserve(TRAITS::size);
std::transform(std::begin(TRAITS::map),
std::end(TRAITS::map),
std::back_inserter(result),
[](const auto& item) { return item.first; });
return result;
}

/**
* The vector of mapped designations
*/
static auto designations() {
std::vector<std::string> result;
result.reserve(TRAITS::size);
std::transform(std::begin(TRAITS::map),
std::end(TRAITS::map),
std::back_inserter(result),
[](const auto& item) { return item.second; });

return result;
}
};

} // namespace ecf

#endif /* ecflow_core_Enumerate_HPP */
Loading

0 comments on commit 88e9f3e

Please sign in to comment.