Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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//
27// $Id: G4UImessenger.hh,v 1.9 2006-06-29 19:08:19 gunter Exp $
28//
29// See http://www.boost.org/libs/any for Documentation.
30// Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
31//
32// Permission to use, copy, modify, and distribute this software for any
33// purpose is hereby granted without fee, provided that this copyright and
34// permissions notice appear in all copies and derivatives.
35//
36// This software is provided "as is" without express or implied warranty.
37// What: variant At boost::any
38// who: contributed by Kevlin Henney,
39// with features contributed and bugs found by
40// Ed Brey, Mark Rodgers, Peter Dimov, and James Curran
41// when: July 2001
42// where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95
43
44#ifndef G4AnyType_h
45#define G4AnyType_h 1
46
47#include <algorithm>
48#include <typeinfo>
49#include <iostream>
50#include <sstream>
51
52class G4String;
53namespace CLHEP {
54 class Hep3Vector;
55}
56
57/**
58 * @class G4AnyType G4AnyType.hh
59 * This class represents any data type. The class only holds a reference to the type and not the value.
60 */
61class G4AnyType {
62public:
63 /** Constructor */
65 fContent(0) {}
66
67 /** Constructor */
68 template <typename ValueType> G4AnyType(ValueType &value):
69 fContent(new Ref<ValueType>(value)) {}
70
71 /** Copy Constructor */
72 G4AnyType(const G4AnyType &other):
73 fContent(other.fContent ? other.fContent->Clone() : 0) {}
74
75 /** Dtor */
77 delete fContent;
78 }
79
80 /** bool operator */
81 operator bool() {
82 return !Empty();
83 }
84 /** Modifier */
86 std::swap(fContent, rhs.fContent);
87 return *this;
88 }
89 /** Modifier */
90 template <typename ValueType> G4AnyType& operator =(const ValueType& rhs) {
91 G4AnyType(rhs).Swap(*this);
92 return *this;
93 }
94 /** Modifier */
96 G4AnyType(rhs).Swap(*this);
97 return *this;
98 }
99 /** Query */
100 bool Empty() const {
101 return !fContent;
102 }
103 /** Query */
104 const std::type_info& TypeInfo() const {
105 return fContent ? fContent->TypeInfo() : typeid(void);
106 }
107 /** Adress */
108 void* Address() const {
109 return fContent ? fContent->Address() : 0;
110 }
111 /** String conversion */
112 std::string ToString() const {
113 return fContent->ToString();
114 }
115 /** String conversion */
116 void FromString(const std::string& val) {
117 fContent->FromString(val);
118 }
119private:
120 /**
121 * @class Placeholder G4AnyType.h G4AnyType.h
122 */
123 class Placeholder {
124 public:
125 /** Constructor */
126 Placeholder() {}
127 /** Destructor */
128 virtual ~Placeholder() {}
129 /** Query */
130 virtual const std::type_info& TypeInfo() const = 0;
131 /** Query */
132 virtual Placeholder* Clone() const = 0;
133 /** Query */
134 virtual void* Address() const = 0;
135 /** ToString */
136 virtual std::string ToString() const = 0;
137 /** FromString */
138 virtual void FromString(const std::string& val) = 0;
139 };
140
141 template <typename ValueType> class Ref: public Placeholder {
142 public:
143 /** Constructor */
144 Ref(ValueType& value): fRef(value) {}
145 /** Query */
146 virtual const std::type_info& TypeInfo() const {
147 return typeid(ValueType);
148 }
149 /** Clone */
150 virtual Placeholder* Clone() const {
151 return new Ref(fRef);
152 }
153 /** Address */
154 virtual void* Address() const {
155 return (void*) (&fRef);
156 }
157 /** ToString */
158 virtual std::string ToString() const {
159 std::stringstream s;
160 s << fRef;
161 return s.str();
162 }
163 /** FromString */
164 virtual void FromString(const std::string& val) {
165 std::stringstream s(val);
166 s >> fRef;
167 }
168 /** representation */
169 ValueType& fRef;
170 };
171 /** representation */
172 template <typename ValueType> friend ValueType* any_cast(G4AnyType*);
173 /** representation */
174 Placeholder* fContent;
175};
176
177/**
178 * Specializations
179 */
180
181template<> void G4AnyType::Ref<bool>::FromString(const std::string& val);
182template<> void G4AnyType::Ref<G4String>::FromString(const std::string& val);
183template<> void G4AnyType::Ref<CLHEP::Hep3Vector>::FromString(const std::string& val);
184
185/**
186 * @class G4BadAnyCast G4AnyType.h Reflex/G4AnyType.h
187 * @author K. Henney
188 */
189class G4BadAnyCast: public std::bad_cast {
190public:
191 /** Constructor */
193
194 /** Query */
195 virtual const char* what() const throw() {
196 return "G4BadAnyCast: failed conversion using any_cast";
197 }
198};
199
200/** value */
201template <typename ValueType> ValueType* any_cast(G4AnyType* operand) {
202 return operand && operand->TypeInfo() == typeid(ValueType)
203 ? &static_cast<G4AnyType::Ref<ValueType>*>(operand->fContent)->fRef : 0;
204}
205/** value */
206template <typename ValueType> const ValueType* any_cast(const G4AnyType* operand) {
207 return any_cast<ValueType>(const_cast<G4AnyType*>(operand));
208}
209/** value */
210template <typename ValueType> ValueType any_cast(const G4AnyType& operand) {
211 const ValueType* result = any_cast<ValueType>(&operand);
212 if (!result) {
213 throw G4BadAnyCast();
214 }
215 return *result;
216}
217
218#endif
ValueType * any_cast(G4AnyType *operand)
Definition: G4AnyType.hh:201
~G4AnyType()
Definition: G4AnyType.hh:76
void FromString(const std::string &val)
Definition: G4AnyType.hh:116
G4AnyType & Swap(G4AnyType &rhs)
Definition: G4AnyType.hh:85
G4AnyType(ValueType &value)
Definition: G4AnyType.hh:68
const std::type_info & TypeInfo() const
Definition: G4AnyType.hh:104
friend ValueType * any_cast(G4AnyType *)
Definition: G4AnyType.hh:201
void * Address() const
Definition: G4AnyType.hh:108
bool Empty() const
Definition: G4AnyType.hh:100
G4AnyType & operator=(const ValueType &rhs)
Definition: G4AnyType.hh:90
G4AnyType(const G4AnyType &other)
Definition: G4AnyType.hh:72
std::string ToString() const
Definition: G4AnyType.hh:112
virtual const char * what() const
Definition: G4AnyType.hh:195
Definition: DoubConv.h:17