Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4AnyType.hh
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4AnyType
27//
28// Class description:
29//
30// The class G4AnyType represents any data type.
31// The class only holds a reference to the type and not the value.
32
33// See http://www.boost.org/libs/any for Documentation.
34// Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
35//
36// Permission to use, copy, modify, and distribute this software for any
37// purpose is hereby granted without fee, provided that this copyright and
38// permissions notice appear in all copies and derivatives.
39//
40// This software is provided "as is" without express or implied warranty.
41// What: variant At boost::any
42// who: contributed by Kevlin Henney,
43// with features contributed and bugs found by
44// Ed Brey, Mark Rodgers, Peter Dimov, and James Curran
45// when: July 2001
46// --------------------------------------------------------------------
47#ifndef G4AnyType_hh
48#define G4AnyType_hh 1
49
50#include <algorithm>
51#include <typeinfo>
52#include <iostream>
53#include <sstream>
54
55#include "G4UIcommand.hh"
56
57class G4String;
58namespace CLHEP
59{
60 class Hep3Vector;
61}
62
64{
65 public:
66
67 /** Constructors */
68
69 G4AnyType() = default;
70
71 template <typename ValueType>
72 G4AnyType(ValueType& value)
73 : fContent(new Ref<ValueType>(value))
74 {}
75
76 /** Copy Constructor */
77
78 G4AnyType(const G4AnyType& other)
79 : fContent(other.fContent != nullptr ? other.fContent->Clone() : nullptr)
80 {}
81
82 /** Destructor */
83
84 ~G4AnyType() { delete fContent; }
85
86 /** bool operator */
87
88 operator bool() { return !Empty(); }
89
90 /** Modifiers */
91
93 {
94 std::swap(fContent, rhs.fContent);
95 return *this;
96 }
97
98 template <typename ValueType>
99 G4AnyType& operator=(const ValueType& rhs)
100 {
101 G4AnyType(rhs).Swap(*this);
102 return *this;
103 }
104
106 {
107 G4AnyType(rhs).Swap(*this);
108 return *this;
109 }
110
111 /** Queries */
112
113 G4bool Empty() const { return fContent == nullptr; }
114
115 const std::type_info& TypeInfo() const
116 {
117 return fContent != nullptr ? fContent->TypeInfo() : typeid(void);
118 }
119
120 /** Address */
121
122 void* Address() const {
123 return fContent != nullptr ? fContent->Address() : nullptr;
124 }
125
126 /** String conversions */
127
128 std::string ToString() const { return fContent->ToString(); }
129
130 void FromString(const std::string& val) { fContent->FromString(val); }
131
132 private:
133
134 class Placeholder
135 {
136 public:
137 Placeholder() = default;
138
139 virtual ~Placeholder() = default;
140
141 /** Queries */
142
143 virtual const std::type_info& TypeInfo() const = 0;
144
145 virtual Placeholder* Clone() const = 0;
146
147 virtual void* Address() const = 0;
148
149 /** ToString */
150
151 virtual std::string ToString() const = 0;
152
153 /** FromString */
154
155 virtual void FromString(const std::string& val) = 0;
156 };
157
158 template <typename ValueType>
159 class Ref : public Placeholder
160 {
161 public:
162
163 /** Constructor */
164
165 Ref(ValueType& value)
166 : fRef(value)
167 {}
168
169 /** Query */
170
171 const std::type_info& TypeInfo() const override
172 {
173 return typeid(ValueType);
174 }
175
176 /** Clone */
177
178 Placeholder* Clone() const override { return new Ref(fRef); }
179
180 /** Address */
181
182 void* Address() const override { return (void*) (&fRef); }
183
184 /** ToString */
185
186 std::string ToString() const override
187 {
188 std::stringstream ss;
189 ss << fRef;
190 return ss.str();
191 }
192
193 /** FromString */
194
195 void FromString(const std::string& val) override
196 {
197 std::stringstream ss(val);
198 ss >> fRef;
199 }
200
201 ValueType& fRef; // representation
202 };
203
204 /** representation */
205
206 template <typename ValueType>
207 friend ValueType* any_cast(G4AnyType*);
208
209 Placeholder* fContent = nullptr;
210};
211
212//
213// Specializations
214//
215
216template <>
217inline void G4AnyType::Ref<bool>::FromString(const std::string& val)
218{
219 fRef = G4UIcommand::ConvertToBool(val.c_str());
220}
221
222template <>
223inline void G4AnyType::Ref<G4String>::FromString(const std::string& val)
224{
225 if(val[0] == '"')
226 {
227 fRef = val.substr(1, val.size() - 2);
228 }
229 else
230 {
231 fRef = val;
232 }
233}
234
235template <>
236inline void G4AnyType::Ref<G4ThreeVector>::FromString(const std::string& val)
237{
238 fRef = G4UIcommand::ConvertTo3Vector(val.c_str());
239}
240
241/**
242 * @class G4BadAnyCast G4AnyType.h Reflex/G4AnyType.h
243 * @author K. Henney
244 */
245class G4BadAnyCast : public std::bad_cast
246{
247 public:
248 G4BadAnyCast() = default;
249
250 const char* what() const throw() override
251 {
252 return "G4BadAnyCast: failed conversion using any_cast";
253 }
254};
255
256/** value */
257
258template <typename ValueType>
259ValueType* any_cast(G4AnyType* operand)
260{
261 return operand && operand->TypeInfo() == typeid(ValueType)
262 ? &static_cast<G4AnyType::Ref<ValueType>*>(operand->fContent)->fRef
263 : nullptr;
264}
265
266template <typename ValueType>
267const ValueType* any_cast(const G4AnyType* operand)
268{
269 return any_cast<ValueType>(const_cast<G4AnyType*>(operand));
270}
271
272template <typename ValueType>
273ValueType any_cast(const G4AnyType& operand)
274{
275 const ValueType* result = any_cast<ValueType>(&operand);
276 if(!result)
277 {
278 throw G4BadAnyCast();
279 }
280 return *result;
281}
282
283#endif
ValueType * any_cast(G4AnyType *operand)
Definition: G4AnyType.hh:259
bool G4bool
Definition: G4Types.hh:86
~G4AnyType()
Definition: G4AnyType.hh:84
void FromString(const std::string &val)
Definition: G4AnyType.hh:130
G4AnyType & Swap(G4AnyType &rhs)
Definition: G4AnyType.hh:92
G4bool Empty() const
Definition: G4AnyType.hh:113
G4AnyType(ValueType &value)
Definition: G4AnyType.hh:72
G4AnyType & operator=(const G4AnyType &rhs)
Definition: G4AnyType.hh:105
const std::type_info & TypeInfo() const
Definition: G4AnyType.hh:115
friend ValueType * any_cast(G4AnyType *)
Definition: G4AnyType.hh:259
G4AnyType()=default
void * Address() const
Definition: G4AnyType.hh:122
G4AnyType & operator=(const ValueType &rhs)
Definition: G4AnyType.hh:99
G4AnyType(const G4AnyType &other)
Definition: G4AnyType.hh:78
std::string ToString() const
Definition: G4AnyType.hh:128
G4BadAnyCast()=default
const char * what() const override
Definition: G4AnyType.hh:250
static G4ThreeVector ConvertTo3Vector(const char *st)
Definition: G4UIcommand.cc:601
static G4bool ConvertToBool(const char *st)
Definition: G4UIcommand.cc:549
Definition: DoubConv.h:17
std::string ToString(const T &value)