OOFEM 3.0
Loading...
Searching...
No Matches
enum.h
Go to the documentation of this file.
1/*
2 *
3 * ##### ##### ###### ###### ### ###
4 * ## ## ## ## ## ## ## ### ##
5 * ## ## ## ## #### #### ## # ##
6 * ## ## ## ## ## ## ## ##
7 * ## ## ## ## ## ## ## ##
8 * ##### ##### ## ###### ## ##
9 *
10 *
11 * OOFEM : Object Oriented Finite Element Code
12 *
13 * Copyright (C) 1993 - 2025 Borek Patzak
14 *
15 *
16 *
17 * Czech Technical University, Faculty of Civil Engineering,
18 * Department of Structural Mechanics, 166 29 Prague, Czech Republic
19 *
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 */
34
35#ifndef enum_h
36#define enum_h
37
38
39#include<optional>
40#include<initializer_list>
41#include<utility>
42#include<cstring>
43#include<iostream>
44#include<vector>
45#include<map>
46
47namespace oofem {
48 /* including enum-impl.h will define the enum SomeEnum and specialize EnumData<SomeEnum>; EnumTraits<SomeEnum> the accesses EnumData<SomeEnum> to work with the enum itself */
49 template<typename T> struct EnumData;
50
51 template<typename Enum> struct EnumTraits{
53 // just forward
54 static constexpr auto enum_name = D::enum_name;
55 // helper function
56 static const bool _starts_with_prefix(const char* s){
57 return D::opt_prefix_len>0 && std::strlen(s)>D::opt_prefix_len && std::strncmp(s,D::opt_prefix,D::opt_prefix_len)==0;
58 }
59 // convert enum value to name
60 static std::optional<const char*> name(typename D::Enum v){
61 for(const auto& vn: D::value_to_name){ if(vn.value==v) return vn.name; }
62 return {};
63 };
64 // convert integer to value
65 static std::optional<typename D::Enum> value(int i){
66 for(const auto& vn: D::value_to_name){ if((int)vn.value==i) return vn.value; }
67 return {};
68 };
69 // convert name to value; considers optional prefix (opt_prefix), so e.g. VM_Total with opt_prefix "VM_" will also match "Total"
70 static std::optional<typename D::Enum> value(const char* n){
71 for(const auto& vn: D::value_to_name){
72 if(std::strcmp(vn.name,n)==0) return vn.value;
73 if(_starts_with_prefix(vn.name) && std::strcmp(vn.name+D::opt_prefix_len,n)==0) return vn.value;
74 }
75 return {};
76 };
77 // return all permissible names (with and without opt_prefix, if used)
78 static std::map<int,std::vector<std::string>> all_values_to_names(){
79 std::map<int,std::vector<std::string>> ret;
80 for(const auto& vn: D::value_to_name){
81 auto [I,_]=ret.insert({(int)vn.value,{vn.name,}});
82 // add any aliases for the value here
83 if(_starts_with_prefix(vn.name)) I->second.push_back(vn.name+D::opt_prefix_len);
84 }
85 return ret;
86 }
87 };
88
89}
90#endif // enum_h
static const bool _starts_with_prefix(const char *s)
Definition enum.h:56
static std::map< int, std::vector< std::string > > all_values_to_names()
Definition enum.h:78
static std::optional< typename D::Enum > value(int i)
Definition enum.h:65
static std::optional< const char * > name(typename D::Enum v)
Definition enum.h:60
static std::optional< typename D::Enum > value(const char *n)
Definition enum.h:70
static constexpr auto enum_name
Definition enum.h:54
EnumData< Enum > D
Definition enum.h:52

This page is part of the OOFEM-3.0 documentation. Copyright Copyright (C) 1994-2025 Borek Patzak Bořek Patzák
Project e-mail: oofem@fsv.cvut.cz
Generated at for OOFEM by doxygen 1.15.0 written by Dimitri van Heesch, © 1997-2011