BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSTypeSafeEnum.hh
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#ifndef BDSTYPESAFEENUM_H
19#define BDSTYPESAFEENUM_H
20
21#include <map>
22#include <ostream>
23#include <string>
24
44template<typename def, typename inner = typename def::type>
45class BDSTypeSafeEnum : public def
46{
47 typedef inner type;
48 inner val;
49
50protected:
53 // pointer used since static memory possibly doesn't know size of map at creation
54 static std::map<BDSTypeSafeEnum<def,inner>,std::string>* dictionary;
55
56public:
57
58 BDSTypeSafeEnum() : val() {}
59 BDSTypeSafeEnum(type v) : val(v) {}
61 type underlying() const { return val; }
62
63 std::string ToString() const {
64 if (dictionary) {return (*dictionary)[val];}
65 // if no dictionary defined return empty string
66 return "";
67 }
68
70 friend bool operator == (const BDSTypeSafeEnum & lhs, const BDSTypeSafeEnum & rhs) { return lhs.val == rhs.val; }
71 friend bool operator != (const BDSTypeSafeEnum & lhs, const BDSTypeSafeEnum & rhs) { return lhs.val != rhs.val; }
72 friend bool operator < (const BDSTypeSafeEnum & lhs, const BDSTypeSafeEnum & rhs) { return lhs.val < rhs.val; }
73 friend bool operator <= (const BDSTypeSafeEnum & lhs, const BDSTypeSafeEnum & rhs) { return lhs.val <= rhs.val; }
74 friend bool operator > (const BDSTypeSafeEnum & lhs, const BDSTypeSafeEnum & rhs) { return lhs.val > rhs.val; }
75 friend bool operator >= (const BDSTypeSafeEnum & lhs, const BDSTypeSafeEnum & rhs) { return lhs.val >= rhs.val; }
78 friend std::ostream& operator<< (std::ostream &out, const BDSTypeSafeEnum& a) {
79 if (dictionary) {out << a.ToString();}
80 else {out << a.underlying();}
81 return out;
82 }
83};
84
86// this seemed to cause a problem, so commented out and every class needs to define its map
87// template<typename def, typename inner>
88// std::map<BDSTypeSafeEnum<def,inner>,std::string>* BDSTypeSafeEnum<def,inner>::dictionary=nullptr;
89
90#endif
Improve type-safety of native enum data type in C++.
friend bool operator!=(const BDSTypeSafeEnum &lhs, const BDSTypeSafeEnum &rhs)
operators for ordering
friend bool operator==(const BDSTypeSafeEnum &lhs, const BDSTypeSafeEnum &rhs)
operators for ordering
static std::map< BDSTypeSafeEnum< def, inner >, std::string > * dictionary
friend bool operator<=(const BDSTypeSafeEnum &lhs, const BDSTypeSafeEnum &rhs)
operators for ordering
friend std::ostream & operator<<(std::ostream &out, const BDSTypeSafeEnum &a)
friend bool operator>(const BDSTypeSafeEnum &lhs, const BDSTypeSafeEnum &rhs)
operators for ordering
friend bool operator>=(const BDSTypeSafeEnum &lhs, const BDSTypeSafeEnum &rhs)
operators for ordering
type underlying() const
return underlying value (can be used in switch statement)
friend bool operator<(const BDSTypeSafeEnum &lhs, const BDSTypeSafeEnum &rhs)
operators for ordering