enum_names.hpp
1 //*****************************************************************************
2 // Copyright 2017-2020 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(), ::tolower);
39  return rc;
40  };
41  for (auto p : get().m_string_enums)
42  {
43  if (to_lower(p.first) == to_lower(name))
44  {
45  return p.second;
46  }
47  }
48  NGRAPH_CHECK(false, "\"", name, "\"", " is not a member of enum ", get().m_enum_name);
49  }
50 
51  /// Converts enum values to strings
52  static const std::string& as_string(EnumType e)
53  {
54  for (auto& p : get().m_string_enums)
55  {
56  if (p.second == e)
57  {
58  return p.first;
59  }
60  }
61  NGRAPH_CHECK(false, " invalid member of enum ", get().m_enum_name);
62  }
63 
64  private:
65  /// Creates the mapping.
66  EnumNames(const std::string& enum_name,
67  const std::vector<std::pair<std::string, EnumType>> string_enums)
68  : m_enum_name(enum_name)
69  , m_string_enums(string_enums)
70  {
71  }
72 
73  /// Must be defined to returns a singleton for each supported enum class
74  static EnumNames<EnumType>& get();
75 
76  const std::string m_enum_name;
77  std::vector<std::pair<std::string, EnumType>> m_string_enums;
78  };
79 
80  /// Returns the enum value matching the string
81  template <typename Type, typename Value>
82  typename std::enable_if<std::is_convertible<Value, std::string>::value, Type>::type
83  as_enum(const Value& value)
84  {
85  return EnumNames<Type>::as_enum(value);
86  }
87 
88  /// Returns the string matching the enum value
89  template <typename Value>
90  const std::string& as_string(Value value)
91  {
92  return EnumNames<Value>::as_string(value);
93  }
94 }
ngraph::EnumNames::as_string
static const std::string & as_string(EnumType e)
Converts enum values to strings.
Definition: enum_names.hpp:52
ngraph::EnumNames::as_enum
static EnumType as_enum(const std::string &name)
Converts strings to enum values.
Definition: enum_names.hpp:34
ngraph
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::as_enum
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:83
ngraph::EnumNames
Definition: enum_names.hpp:31
ngraph::as_string
const std::string & as_string(Value value)
Returns the string matching the enum value.
Definition: enum_names.hpp:90