OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
enum_map.h
Go to the documentation of this file.
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// E N U M M A P
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#pragma once
10
11#include "pch.h"
12
13namespace opennn
14{
15
16template <typename Enum>
17struct EnumMap
18{
19 using Entry = pair<Enum, string>;
20
21 const vector<Entry>& entries;
22
23 const string& to_string(Enum value) const
24 {
25 for (const auto& [enum_value, name] : entries)
26 if (enum_value == value)
27 return name;
28 throw runtime_error("Unknown enum value");
29 }
30
31 Enum from_string(const string& name) const
32 {
33 for (const auto& [enum_value, entry_name] : entries)
34 if (entry_name == name)
35 return enum_value;
36 throw runtime_error("Unknown enum string: " + name);
37 }
38
39 Enum from_string(const string& name, Enum fallback) const
40 {
41 for (const auto& [enum_value, entry_name] : entries)
42 if (entry_name == name)
43 return enum_value;
44 return fallback;
45 }
46};
47
48}
49
50// OpenNN: Open Neural Networks Library.
51// Copyright(C) 2005-2026 Artificial Intelligence Techniques, SL.
52// Licensed under the GNU Lesser General Public License v2.1 or later.
Definition adaptive_moment_estimation.h:19
Definition enum_map.h:18
Enum from_string(const string &name, Enum fallback) const
Definition enum_map.h:39
const string & to_string(Enum value) const
Definition enum_map.h:23
const vector< Entry > & entries
Definition enum_map.h:21
Enum from_string(const string &name) const
Definition enum_map.h:31
pair< Enum, string > Entry
Definition enum_map.h:19