DUNE-FEM (unstable)

staticlistofint.hh
1#ifndef DUNE_FEM_STATICLISTOFINTS_HH
2#define DUNE_FEM_STATICLISTOFINTS_HH
3
4#include <iostream>
5#include <sstream>
6#include <map>
7#include <cassert>
8
9// macro that defines a struct containing a list of static const ints
10// and the names of those ints as strings. This is mainly intended for
11// parameter identifiers, e.g. in dune/fem/solver/parameter.hh
12#define LIST_OF_INT(ListName, ...) \
13struct ListName {\
14static constexpr int __VA_ARGS__; \
15typedef std::pair< std::map<int, int>, std::vector<std::string> > EntriesType; \
16static inline EntriesType& entries() { \
17static EntriesType idMap; \
18static bool initialized = false;\
19if( !initialized ){\
20std::string str = #__VA_ARGS__; \
21int len = str.length(); \
22std::ostringstream temp; \
23int id = 0;\
24int n = 0;\
25for(int i = 0; i < len; ++i) { \
26 if(str[i] == '=') { \
27 size_t c = str.find(',', i+1);\
28 std::string number = str.substr(i+1, c-(i+1));\
29 id = stoi(number);\
30 while( i < len && str[i] != ',') \
31 ++i;\
32 }\
33 if(isspace(str[i])) continue; \
34 else if(str[i] == ',') { \
35 idMap.first[ id ] = n++; \
36 idMap.second.push_back(temp.str()); \
37 temp.str(std::string());\
38 } \
39 else if (str[i] == '_') {\
40 temp << "-";\
41 }\
42 else temp<< str[i]; \
43} \
44idMap.first[id] = n; \
45idMap.second.push_back( temp.str() );\
46idMap.second.back().pop_back();\
47initialized = true;\
48}\
49return idMap;} \
50static inline int to_id(int value){\
51 for( const auto& item : entries().first ){\
52 if( item.second == value )\
53 return item.first;\
54 }\
55 assert(false); \
56 return -1;\
57}\
58static inline const std::vector<std::string>& names() { \
59 return entries().second;}\
60static int length() { return entries().second.size(); }\
61static std::string to_string(int value){\
62 auto it = entries().first.find( value );\
63 assert( it != entries().first.end() );\
64 assert( it->second < int(entries().second.size()) );\
65 return entries().second[ it->second ];\
66}\
67};
68
69// same as LIST_OF_INT. In addition the integers are forwarded to the
70// current namespace. This is needed for SolverParameters.
71#define LIST_OF_INT_FORWARDED(ListName, ...) \
72 LIST_OF_INT(ListName, __VA_ARGS__);\
73 static const int __VA_ARGS__;
74
75#endif
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)