ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4AnyType.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4AnyType.hh
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 //
28 // See http://www.boost.org/libs/any for Documentation.
29 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
30 //
31 // Permission to use, copy, modify, and distribute this software for any
32 // purpose is hereby granted without fee, provided that this copyright and
33 // permissions notice appear in all copies and derivatives.
34 //
35 // This software is provided "as is" without express or implied warranty.
36 // What: variant At boost::any
37 // who: contributed by Kevlin Henney,
38 // with features contributed and bugs found by
39 // Ed Brey, Mark Rodgers, Peter Dimov, and James Curran
40 // when: July 2001
41 // where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95
42 
43 #ifndef G4AnyType_h
44 #define G4AnyType_h 1
45 
46 #include <algorithm>
47 #include <typeinfo>
48 #include <iostream>
49 #include <sstream>
50 
51 #include "G4UIcommand.hh"
52 
53 class G4String;
54 namespace CLHEP {
55  class Hep3Vector;
56 }
57 
62 class G4AnyType {
63 public:
66  fContent(0) {}
67 
69  template <typename ValueType> G4AnyType(ValueType &value):
70  fContent(new Ref<ValueType>(value)) {}
71 
73  G4AnyType(const G4AnyType &other):
74  fContent(other.fContent ? other.fContent->Clone() : 0) {}
75 
78  delete fContent;
79  }
80 
82  operator bool() {
83  return !Empty();
84  }
88  return *this;
89  }
91  template <typename ValueType> G4AnyType& operator =(const ValueType& rhs) {
92  G4AnyType(rhs).Swap(*this);
93  return *this;
94  }
97  G4AnyType(rhs).Swap(*this);
98  return *this;
99  }
101  bool Empty() const {
102  return !fContent;
103  }
105  const std::type_info& TypeInfo() const {
106  return fContent ? fContent->TypeInfo() : typeid(void);
107  }
109  void* Address() const {
110  return fContent ? fContent->Address() : 0;
111  }
113  std::string ToString() const {
114  return fContent->ToString();
115  }
117  void FromString(const std::string& val) {
118  fContent->FromString(val);
119  }
120 private:
124  class Placeholder {
125  public:
129  virtual ~Placeholder() {}
131  virtual const std::type_info& TypeInfo() const = 0;
133  virtual Placeholder* Clone() const = 0;
135  virtual void* Address() const = 0;
137  virtual std::string ToString() const = 0;
139  virtual void FromString(const std::string& val) = 0;
140  };
141 
142  template <typename ValueType> class Ref: public Placeholder {
143  public:
145  Ref(ValueType& value): fRef(value) {}
147  virtual const std::type_info& TypeInfo() const {
148  return typeid(ValueType);
149  }
151  virtual Placeholder* Clone() const {
152  return new Ref(fRef);
153  }
155  virtual void* Address() const {
156  return (void*) (&fRef);
157  }
159  virtual std::string ToString() const {
160  std::stringstream ss;
161  ss << fRef;
162  return ss.str();
163  }
165  virtual void FromString(const std::string& val) {
166  std::stringstream ss(val);
167  ss >> fRef;
168  }
170  ValueType& fRef;
171  };
173  template <typename ValueType> friend ValueType* any_cast(G4AnyType*);
176 };
177 
182 template <> inline void G4AnyType::Ref<bool>::FromString(const std::string& val) {
183  fRef = G4UIcommand::ConvertToBool(val.c_str());
184 }
185 
186 template <> inline void G4AnyType::Ref<G4String>::FromString(const std::string& val) {
187  if (val[0] == '"' ) fRef = val.substr(1,val.size()-2);
188  else fRef = val;
189 }
190 
191 template <> inline void G4AnyType::Ref<G4ThreeVector>::FromString(const std::string& val) {
192  fRef = G4UIcommand::ConvertTo3Vector(val.c_str());
193 }
194 
195 
200 class G4BadAnyCast: public std::bad_cast {
201 public:
204 
206  virtual const char* what() const throw() {
207  return "G4BadAnyCast: failed conversion using any_cast";
208  }
209 };
210 
212 template <typename ValueType> ValueType* any_cast(G4AnyType* operand) {
213  return operand && operand->TypeInfo() == typeid(ValueType)
214  ? &static_cast<G4AnyType::Ref<ValueType>*>(operand->fContent)->fRef : 0;
215 }
217 template <typename ValueType> const ValueType* any_cast(const G4AnyType* operand) {
218  return any_cast<ValueType>(const_cast<G4AnyType*>(operand));
219 }
221 template <typename ValueType> ValueType any_cast(const G4AnyType& operand) {
222  const ValueType* result = any_cast<ValueType>(&operand);
223  if (!result) {
224  throw G4BadAnyCast();
225  }
226  return *result;
227 }
228 
229 #endif