1.1.0
CCC
 
AEnum.hpp File Reference
#include "gstlearn_export.hpp"
#include "geoslib_define.h"
#include "Basic/VectorT.hpp"
#include "Basic/VectorNumT.hpp"
#include "Basic/RepeatMacro.hpp"
#include "Basic/String.hpp"
#include <iostream>
#include <string>
#include <iomanip>
#include <map>
#include <vector>

Classes

class  AEnum
 

Macros

#define ENUM_ITEM(NAME, x, y, z)   E_ ## x = y,
 
#define ENUM_ITEMS(NAME, ...)   EXPAND(REPEAT3(ENUM_ITEM, NAME, __VA_ARGS__))
 
#define ENUM_DECL(NAME, x, y, z)   static const NAME x;
 
#define ENUM_DECLS(NAME, ...)   EXPAND(REPEAT3(ENUM_DECL, NAME, __VA_ARGS__))
 
#define ENUM_IMPL(NAME, x, y, z)   const NAME NAME::x = NAME(#x, y, z);
 
#define ENUM_IMPLS(NAME, ...)   EXPAND(REPEAT3(ENUM_IMPL, NAME, __VA_ARGS__))
 
#define ENUM_DECLARE_(NAME, DEFAULT, ...)
 
#define ENUM_DEFINE_(NAME, DEFAULT, ...)
 
#define ENUM_DECLARE(...)   EXPAND(ENUM_DECLARE_(__VA_ARGS__))
 
#define ENUM_DEFINE(...)   EXPAND(ENUM_DEFINE_(__VA_ARGS__))
 

Macro Definition Documentation

#define ENUM_DECL (   NAME,
  x,
  y,
 
)    static const NAME x;
#define ENUM_DECLARE (   ...)    EXPAND(ENUM_DECLARE_(__VA_ARGS__))
#define ENUM_DECLARE_ (   NAME,
  DEFAULT,
  ... 
)
Value:
class NAME;\
\
typedef std::map<int, NAME*> NAME ## Map;\
\
class GSTLEARN_EXPORT NAME ## Iterator\
{\
friend class NAME;\
\
NAME ## Iterator() = delete;\
NAME ## Iterator(NAME ## Map* map);\
public:\
~NAME ## Iterator() = default;\
NAME ## Iterator(const NAME ## Iterator&) = default;\
NAME ## Iterator& operator=(const NAME ## Iterator&) = default;\
\
const NAME& operator*() const;\
bool hasNext() const;\
const NAME& toNext();\
const NAME& toFront();\
const NAME& getEnum() const;\
int getValue() const;\
const String& getKey() const;\
const String& getDescr() const;\
\
private:\
NAME ## Map::iterator _stditer;\
NAME ## Map* _refmap;\
};\
\
class GSTLEARN_EXPORT NAME : public AEnum\
{\
\
public:\
NAME();\
~NAME();\
NAME(const NAME&) = default;\
NAME(int value);\
NAME(const String& key);\
NAME& operator=(const NAME&) = default;\
\
static size_t getSize();\
static NAME ## Iterator getIterator();\
static void printAll();\
static VectorString getAllKeys();\
static VectorString getAllDescr();\
\
static bool existsKey(const String& key);\
static bool existsValue(int value);\
static const NAME& fromKey(const String& key);\
static const NAME& fromValue(int value);\
static std::vector<NAME> fromKeys(const VectorString& keys);\
static std::vector<NAME> fromValues(const VectorInt& values);\
\
private:\
NAME(const String& key, int value, const String& descr);\
\
static NAME ## Map _map;\
static NAME ## Iterator _iterator;\
static const NAME* _default;\
\
public:\
enum E ## NAME\
{\
EXPAND(ENUM_ITEMS(NAME, __VA_ARGS__))\
};\
E ## NAME toEnum() const;\
EXPAND(ENUM_DECLS(NAME, __VA_ARGS__))\
};\
\
#define ENUM_DECLS(NAME,...)
Definition: AEnum.hpp:87
#define ENUM_ITEMS(NAME,...)
Definition: AEnum.hpp:84
Definition: AEnum.hpp:28
Definition: VectorT.hpp:31
#define EXPAND(x)
Definition: RepeatMacroOrig.hpp:15
std::string String
Definition: geoslib_define.h:18
#define ENUM_DECLS (   NAME,
  ... 
)    EXPAND(REPEAT3(ENUM_DECL, NAME, __VA_ARGS__))
#define ENUM_DEFINE (   ...)    EXPAND(ENUM_DEFINE_(__VA_ARGS__))
#define ENUM_DEFINE_ (   NAME,
  DEFAULT,
  ... 
)
Value:
NAME ## Map NAME::_map = NAME ## Map();\
NAME ## Iterator NAME::_iterator = NAME ## Iterator(&NAME::_map);\
\
const NAME* NAME::_default = &NAME::DEFAULT;\
\
NAME::NAME()\
: AEnum(*_default)\
{\
}\
\
NAME::NAME(int value)\
: AEnum(fromValue(value))\
{\
}\
\
NAME::NAME(const String& key)\
: AEnum(fromKey(key))\
{\
}\
\
NAME::NAME(const String& key, int value, const String& descr)\
: AEnum(key, value, descr)\
{\
if (_map.find(value) != _map.end())\
throw("Duplicated item");\
_map[value] = this;\
}\
\
NAME::~NAME()\
{\
}\
\
size_t NAME::getSize()\
{\
return _map.size();\
}\
\
NAME ## Iterator NAME::getIterator()\
{\
auto it(_iterator);\
it.toFront();\
return it;\
}\
\
void NAME::printAll()\
{\
auto it(getIterator());\
while (it.hasNext())\
{\
(*it).printEnum();\
it.toNext();\
}\
}\
VectorString NAME::getAllKeys()\
{\
auto it(getIterator());\
while (it.hasNext())\
{\
keys.push_back((*it).getKey());\
it.toNext();\
}\
return keys;\
}\
VectorString NAME::getAllDescr()\
{\
auto it(getIterator());\
while (it.hasNext())\
{\
descr.push_back((*it).getDescr());\
it.toNext();\
}\
return descr;\
}\
\
bool NAME::existsKey(const String& key)\
{\
auto it = _map.begin();\
while (it != _map.end())\
{\
if (it->second->getKey() == key)\
return true;\
it++;\
}\
return false;\
}\
\
bool NAME::existsValue(int value)\
{\
return (_map.find(value) != _map.end());\
}\
\
const NAME& NAME::fromKey(const String& key)\
{\
auto it = _map.begin();\
while (it != _map.end())\
{\
if (it->second->getKey() == toUpper(key))\
return (*(it->second));\
it++;\
}\
std::cout << "Unknown key " << key << " for enum " << #NAME << std::endl;\
return *_default;\
}\
\
const NAME& NAME::fromValue(int value)\
{\
if (existsValue(value))\
return (*(_map[value]));\
std::cout << "Unknown value " << value << " for enum " << #NAME << std::endl;\
return *_default;\
}\
\
std::vector<NAME> NAME::fromKeys(const VectorString& keys)\
{\
std::vector<NAME> vec;\
for (auto v : keys)\
vec.push_back(fromKey(v));\
return vec;\
}\
\
std::vector<NAME> NAME::fromValues(const VectorInt& values)\
{\
std::vector<NAME> vec;\
for (auto v : values)\
vec.push_back(fromValue(v));\
return vec;\
}\
\
NAME::E ## NAME NAME::toEnum() const\
{\
return static_cast<E ## NAME>(getValue());\
}\
EXPAND(ENUM_IMPLS(NAME, __VA_ARGS__))\
\
NAME ## Iterator::NAME ## Iterator(NAME ## Map* map) \
: _stditer(map->begin())\
, _refmap(map)\
{\
}\
\
const NAME& NAME ## Iterator::operator*() const\
{\
return (*(_stditer->second));\
}\
\
bool NAME ## Iterator::hasNext() const\
{\
return (_stditer != _refmap->end());\
}\
\
const NAME& NAME ## Iterator::toNext()\
{\
return (*((_stditer++)->second));\
}\
\
const NAME& NAME ## Iterator::toFront()\
{\
_stditer = _refmap->begin();\
return (*(_stditer->second));\
}\
\
const NAME& NAME ## Iterator::getEnum() const\
{\
return (*(_stditer->second));\
}\
\
int NAME ## Iterator::getValue() const\
{\
return (_stditer->second->getValue());\
}\
\
const String& NAME ## Iterator::getKey() const\
{\
return (_stditer->second->getKey());\
}\
\
const String& NAME ## Iterator::getDescr() const\
{\
return (_stditer->second->getDescr());\
}\
\
VectorT< String > VectorString
Definition: VectorT.hpp:298
GSTLEARN_EXPORT String toUpper(const String &string)
Definition: String.cpp:51
#define ENUM_IMPLS(NAME,...)
Definition: AEnum.hpp:90
Definition: AEnum.hpp:28
Definition: VectorT.hpp:31
#define EXPAND(x)
Definition: RepeatMacroOrig.hpp:15
std::string String
Definition: geoslib_define.h:18
#define ENUM_IMPL (   NAME,
  x,
  y,
 
)    const NAME NAME::x = NAME(#x, y, z);
#define ENUM_IMPLS (   NAME,
  ... 
)    EXPAND(REPEAT3(ENUM_IMPL, NAME, __VA_ARGS__))
#define ENUM_ITEM (   NAME,
  x,
  y,
 
)    E_ ## x = y,
#define ENUM_ITEMS (   NAME,
  ... 
)    EXPAND(REPEAT3(ENUM_ITEM, NAME, __VA_ARGS__))