Types.h
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #ifndef NUKLEI_TYPES_H
8 #define NUKLEI_TYPES_H
9 
10 #include <vector>
11 
12 #include <nuklei/Common.h>
13 
14 namespace nuklei {
15 
16  /**
17  * @addtogroup type
18  * @{
19  */
20 
21  /**
22  * @brief Cats all type names into a string.
23  *
24  * Names are separated by a bar.
25  * The default name is followed by a star.
26  */
27  template<class T>
28  std::string catTypeNames()
29  {
30  std::string s;
31  for (int i = 0; i < T::UNKNOWN; i++)
32  {
33  s += T::TypeNames[i];
34  if (i == T::defaultType) s += "*";
35  if (i != T::UNKNOWN-1) s += std::string("|");
36  }
37  return s;
38  }
39 
40  /** @brief Fills a std::vector with all type names. */
41  template<class T>
42  std::vector<std::string> listTypeNames()
43  {
44  std::vector<std::string> l;
45  for (int i = 0; i < T::UNKNOWN; i++)
46  l.push_back(T::TypeNames[i]);
47  return l;
48  }
49 
50  /** @brief Returns the default type name. */
51  template<class T>
52  std::string defaultTypeName()
53  {
54  return T::TypeNames[T::defaultType];
55  }
56 
57  /** @brief Returns the default type. */
58  template<class T>
59  typename T::Type defaultType()
60  {
61  return T::defaultType;
62  }
63 
64  /** @brief Returns the name of type @p t. */
65  template<class T>
66  std::string nameFromType(int t)
67  {
68  NUKLEI_ASSERT(0 <= t && t <= T::UNKNOWN);
69  if (t == T::UNKNOWN) return "unknown";
70  else return T::TypeNames[t];
71  }
72 
73  /** @brief Returns the type whose name is @p s. */
74  template<class T>
75  typename T::Type typeFromName(std::string s)
76  {
77  for (int i = 0; i < T::UNKNOWN; i++)
78  {
79  if (s == T::TypeNames[i]) return typename T::Type(i);
80  }
81  if (s == "unknown") return T::UNKNOWN;
82  NUKLEI_THROW("Invalid type `" << s << "'.");
83  }
84 
85  /** @} */
86 }
87 #endif
std::string defaultTypeName()
Returns the default type name.
Definition: Types.h:52
Public namespace.
Definition: Color.cpp:9
T::Type typeFromName(std::string s)
Returns the type whose name is s.
Definition: Types.h:75
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
std::vector< std::string > listTypeNames()
Fills a std::vector with all type names.
Definition: Types.h:42
std::string nameFromType(int t)
Returns the name of type t.
Definition: Types.h:66
std::string catTypeNames()
Cats all type names into a string.
Definition: Types.h:28
T::Type defaultType()
Returns the default type.
Definition: Types.h:59
#define NUKLEI_THROW(x)
Throws an Error.
Definition: Common.h:94
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.