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