Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
52 class G4String;
53 namespace CLHEP {
54  class Hep3Vector;
55 }
56 
61 class G4AnyType {
62 public:
65  fContent(0) {}
66 
68  template <typename ValueType> G4AnyType(ValueType &value):
69  fContent(new Ref<ValueType>(value)) {}
70 
72  G4AnyType(const G4AnyType &other):
73  fContent(other.fContent ? other.fContent->Clone() : 0) {}
74 
77  delete fContent;
78  }
79 
81  operator bool() {
82  return !Empty();
83  }
86  std::swap(fContent, rhs.fContent);
87  return *this;
88  }
90  template <typename ValueType> G4AnyType& operator =(const ValueType& rhs) {
91  G4AnyType(rhs).Swap(*this);
92  return *this;
93  }
96  G4AnyType(rhs).Swap(*this);
97  return *this;
98  }
100  bool Empty() const {
101  return !fContent;
102  }
104  const std::type_info& TypeInfo() const {
105  return fContent ? fContent->TypeInfo() : typeid(void);
106  }
108  void* Address() const {
109  return fContent ? fContent->Address() : 0;
110  }
112  std::string ToString() const {
113  return fContent->ToString();
114  }
116  void FromString(const std::string& val) {
117  fContent->FromString(val);
118  }
119 private:
123  class Placeholder {
124  public:
126  Placeholder() {}
128  virtual ~Placeholder() {}
130  virtual const std::type_info& TypeInfo() const = 0;
132  virtual Placeholder* Clone() const = 0;
134  virtual void* Address() const = 0;
136  virtual std::string ToString() const = 0;
138  virtual void FromString(const std::string& val) = 0;
139  };
140 
141  template <typename ValueType> class Ref: public Placeholder {
142  public:
144  Ref(ValueType& value): fRef(value) {}
146  virtual const std::type_info& TypeInfo() const {
147  return typeid(ValueType);
148  }
150  virtual Placeholder* Clone() const {
151  return new Ref(fRef);
152  }
154  virtual void* Address() const {
155  return (void*) (&fRef);
156  }
158  virtual std::string ToString() const {
159  std::stringstream s;
160  s << fRef;
161  return s.str();
162  }
164  virtual void FromString(const std::string& val) {
165  std::stringstream s(val);
166  s >> fRef;
167  }
169  ValueType& fRef;
170  };
172  template <typename ValueType> friend ValueType* any_cast(G4AnyType*);
174  Placeholder* fContent;
175 };
176 
181 template<> void G4AnyType::Ref<bool>::FromString(const std::string& val);
182 template<> void G4AnyType::Ref<G4String>::FromString(const std::string& val);
183 template<> void G4AnyType::Ref<CLHEP::Hep3Vector>::FromString(const std::string& val);
184 
189 class G4BadAnyCast: public std::bad_cast {
190 public:
193 
195  virtual const char* what() const throw() {
196  return "G4BadAnyCast: failed conversion using any_cast";
197  }
198 };
199 
201 template <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 }
206 template <typename ValueType> const ValueType* any_cast(const G4AnyType* operand) {
207  return any_cast<ValueType>(const_cast<G4AnyType*>(operand));
208 }
210 template <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