Geant4 10.7.0
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
70 {}
71
72 template <typename ValueType>
73 G4AnyType(ValueType& value)
74 : fContent(new Ref<ValueType>(value))
75 {}
76
77 /** Copy Constructor */
78
79 G4AnyType(const G4AnyType& other)
80 : fContent(other.fContent ? other.fContent->Clone() : 0)
81 {}
82
83 /** Destructor */
84
85 ~G4AnyType() { delete fContent; }
86
87 /** bool operator */
88
89 operator bool() { return !Empty(); }
90
91 /** Modifiers */
92
94 {
95 std::swap(fContent, rhs.fContent);
96 return *this;
97 }
98
99 template <typename ValueType>
100 G4AnyType& operator=(const ValueType& rhs)
101 {
102 G4AnyType(rhs).Swap(*this);
103 return *this;
104 }
105
107 {
108 G4AnyType(rhs).Swap(*this);
109 return *this;
110 }
111
112 /** Queries */
113
114 G4bool Empty() const { return !fContent; }
115
116 const std::type_info& TypeInfo() const
117 {
118 return fContent ? fContent->TypeInfo() : typeid(void);
119 }
120
121 /** Address */
122
123 void* Address() const { return fContent ? fContent->Address() : 0; }
124
125 /** String conversions */
126
127 std::string ToString() const { return fContent->ToString(); }
128
129 void FromString(const std::string& val) { fContent->FromString(val); }
130
131 private:
132
133 class Placeholder
134 {
135 public:
136
137 Placeholder() {}
138
139 virtual ~Placeholder() {}
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 virtual const std::type_info& TypeInfo() const
172 {
173 return typeid(ValueType);
174 }
175
176 /** Clone */
177
178 virtual Placeholder* Clone() const { return new Ref(fRef); }
179
180 /** Address */
181
182 virtual void* Address() const { return (void*) (&fRef); }
183
184 /** ToString */
185
186 virtual std::string ToString() const
187 {
188 std::stringstream ss;
189 ss << fRef;
190 return ss.str();
191 }
192
193 /** FromString */
194
195 virtual void FromString(const std::string& val)
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 fRef = val.substr(1, val.size() - 2);
227 else
228 fRef = val;
229}
230
231template <>
232inline void G4AnyType::Ref<G4ThreeVector>::FromString(const std::string& val)
233{
234 fRef = G4UIcommand::ConvertTo3Vector(val.c_str());
235}
236
237/**
238 * @class G4BadAnyCast G4AnyType.h Reflex/G4AnyType.h
239 * @author K. Henney
240 */
241class G4BadAnyCast : public std::bad_cast
242{
243 public:
244
246
247 virtual const char* what() const throw()
248 {
249 return "G4BadAnyCast: failed conversion using any_cast";
250 }
251};
252
253/** value */
254
255template <typename ValueType>
256ValueType* any_cast(G4AnyType* operand)
257{
258 return operand && operand->TypeInfo() == typeid(ValueType)
259 ? &static_cast<G4AnyType::Ref<ValueType>*>(operand->fContent)->fRef
260 : nullptr;
261}
262
263template <typename ValueType>
264const ValueType* any_cast(const G4AnyType* operand)
265{
266 return any_cast<ValueType>(const_cast<G4AnyType*>(operand));
267}
268
269template <typename ValueType>
270ValueType any_cast(const G4AnyType& operand)
271{
272 const ValueType* result = any_cast<ValueType>(&operand);
273 if(!result)
274 {
275 throw G4BadAnyCast();
276 }
277 return *result;
278}
279
280#endif
ValueType * any_cast(G4AnyType *operand)
Definition: G4AnyType.hh:256
bool G4bool
Definition: G4Types.hh:86
~G4AnyType()
Definition: G4AnyType.hh:85
void FromString(const std::string &val)
Definition: G4AnyType.hh:129
G4AnyType & Swap(G4AnyType &rhs)
Definition: G4AnyType.hh:93
G4bool Empty() const
Definition: G4AnyType.hh:114
G4AnyType(ValueType &value)
Definition: G4AnyType.hh:73
G4AnyType & operator=(const G4AnyType &rhs)
Definition: G4AnyType.hh:106
const std::type_info & TypeInfo() const
Definition: G4AnyType.hh:116
friend ValueType * any_cast(G4AnyType *)
Definition: G4AnyType.hh:256
void * Address() const
Definition: G4AnyType.hh:123
G4AnyType & operator=(const ValueType &rhs)
Definition: G4AnyType.hh:100
G4AnyType(const G4AnyType &other)
Definition: G4AnyType.hh:79
std::string ToString() const
Definition: G4AnyType.hh:127
virtual const char * what() const
Definition: G4AnyType.hh:247
static G4ThreeVector ConvertTo3Vector(const char *st)
Definition: G4UIcommand.cc:583
static G4bool ConvertToBool(const char *st)
Definition: G4UIcommand.cc:530
Definition: DoubConv.h:17