enum_names.hpp
1 //*****************************************************************************
2 // Copyright 2017-2021 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16 
17 #pragma once
18 
19 #include <algorithm>
20 #include <string>
21 #include <utility>
22 
23 #include "ngraph/check.hpp"
24 
25 namespace ngraph
26 {
27  /// Uses a pairings defined by EnumTypes::get() to convert between strings
28  /// and enum values.
29  template <typename EnumType>
30  class EnumNames
31  {
32  public:
33  /// Converts strings to enum values
34  static EnumType as_enum(const std::string& name)
35  {
36  auto to_lower = [](const std::string& s) {
37  std::string rc = s;
38  std::transform(rc.begin(), rc.end(), rc.begin(), [](char c) {
39  return static_cast<char>(::tolower(static_cast<int>(c)));
40  });
41  return rc;
42  };
43  for (auto p : get().m_string_enums)
44  {
45  if (to_lower(p.first) == to_lower(name))
46  {
47  return p.second;
48  }
49  }
50  NGRAPH_CHECK(false, "\"", name, "\"", " is not a member of enum ", get().m_enum_name);
51  }
52 
53  /// Converts enum values to strings
54  static const std::string& as_string(EnumType e)
55  {
56  for (auto& p : get().m_string_enums)
57  {
58  if (p.second == e)
59  {
60  return p.first;
61  }
62  }
63  NGRAPH_CHECK(false, " invalid member of enum ", get().m_enum_name);
64  }
65 
66  private:
67  /// Creates the mapping.
68  EnumNames(const std::string& enum_name,
69  const std::vector<std::pair<std::string, EnumType>> string_enums)
70  : m_enum_name(enum_name)
71  , m_string_enums(string_enums)
72  {
73  }
74 
75  /// Must be defined to returns a singleton for each supported enum class
76  static EnumNames<EnumType>& get();
77 
78  const std::string m_enum_name;
79  std::vector<std::pair<std::string, EnumType>> m_string_enums;
80  };
81 
82  /// Returns the enum value matching the string
83  template <typename Type, typename Value>
84  typename std::enable_if<std::is_convertible<Value, std::string>::value, Type>::type
85  as_enum(const Value& value)
86  {
87  return EnumNames<Type>::as_enum(value);
88  }
89 
90  /// Returns the string matching the enum value
91  template <typename Value>
92  const std::string& as_string(Value value)
93  {
94  return EnumNames<Value>::as_string(value);
95  }
96 }
Definition: enum_names.hpp:31
static const std::string & as_string(EnumType e)
Converts enum values to strings.
Definition: enum_names.hpp:54
static EnumType as_enum(const std::string &name)
Converts strings to enum values.
Definition: enum_names.hpp:34
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
std::enable_if< std::is_convertible< Value, std::string >::value, Type >::type as_enum(const Value &value)
Returns the enum value matching the string.
Definition: enum_names.hpp:85
const std::string & as_string(Value value)
Returns the string matching the enum value.
Definition: enum_names.hpp:92