ANNA Suite  2020b
Multipurpose development suite for Telco applications
define.autoenum.hpp
Go to the documentation of this file.
1 // ANNA - Anna is Not Nothingness Anymore //
2 // //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
4 // //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7 
8 
9 #ifndef anna_core_define_autoenum_hpp
10 #define anna_core_define_autoenum_hpp
11 
12 #include <string.h>
13 
15 
48 #define anna_declare_enum(name) \
49  static const char* literal []; \
50  static int calculateSize () { \
51  int ii = 0; \
52  while (literal [ii] != NULL) ii ++; \
53  return ii; \
54  } \
55  static _v asEnum (const char* str) { \
56  for (int ii = 0; literal [ii] != NULL; ii ++) { \
57  if (strcasecmp (str, literal [ii]) == 0) \
58  return (_v) ii; \
59  } \
60  return None; \
61  } \
62  static _v asEnumEx (const char* str) noexcept(false) { \
63  if (str == NULL) { \
64  std::string msg (#name); \
65  msg += "::asEnumEx | str can not be null"; \
66  throw anna::RuntimeException (msg, __FILE__,__LINE__); \
67  } \
68  _v result = asEnum (str); \
69  if (result == None) { \
70  std::string msg (#name); \
71  msg += " | Value: '"; \
72  msg += str; \
73  msg += "' is not valid | Valid values: "; \
74  msg += asList (); \
75  throw anna::RuntimeException (msg, __FILE__, __LINE__); \
76  } \
77  return result; \
78  } \
79  static _v asEnum (const std::string& str) { return asEnum (str.c_str ()); } \
80  static _v asEnumEx (const std::string& str) noexcept(false) { return asEnumEx (str.c_str ()); } \
81  static const char* asCString (const _v v) { return (v != None) ? literal [v]: NULL; } \
82  static const char* asNotNullCString (const _v v) { return (v != None) ? literal [v]: "<none>"; } \
83  static std::string asList () {\
84  std::string result;\
85  for (int ii = 0; literal [ii] != NULL; ii ++) { \
86  if (ii == 0 && strcmp (literal [ii], "None") == 0) continue; \
87  if (ii > 1) result += ' '; \
88  result += "'"; result += literal [ii]; result += "'"; \
89  } \
90  return result; \
91  }
92 
107 #define anna_assign_enum(name) const char* name::literal []
108 
113 #define anna_item_enum(name,ii) name::literal[ii]
114 
115 
116 #endif