Geant4.10
 All Data Structures 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 #include "G4UIcommand.hh"
53 
54 class G4String;
55 namespace CLHEP {
56  class Hep3Vector;
57 }
58 
59 /**
60  * @class G4AnyType G4AnyType.hh
61  * This class represents any data type. The class only holds a reference to the type and not the value.
62  */
63 class G4AnyType {
64 public:
65  /** Constructor */
67  fContent(0) {}
68 
69  /** Constructor */
70  template <typename ValueType> G4AnyType(ValueType &value):
71  fContent(new Ref<ValueType>(value)) {}
72 
73  /** Copy Constructor */
74  G4AnyType(const G4AnyType &other):
75  fContent(other.fContent ? other.fContent->Clone() : 0) {}
76 
77  /** Dtor */
79  delete fContent;
80  }
81 
82  /** bool operator */
83  operator bool() {
84  return !Empty();
85  }
86  /** Modifier */
88  std::swap(fContent, rhs.fContent);
89  return *this;
90  }
91  /** Modifier */
92  template <typename ValueType> G4AnyType& operator =(const ValueType& rhs) {
93  G4AnyType(rhs).Swap(*this);
94  return *this;
95  }
96  /** Modifier */
98  G4AnyType(rhs).Swap(*this);
99  return *this;
100  }
101  /** Query */
102  bool Empty() const {
103  return !fContent;
104  }
105  /** Query */
106  const std::type_info& TypeInfo() const {
107  return fContent ? fContent->TypeInfo() : typeid(void);
108  }
109  /** Adress */
110  void* Address() const {
111  return fContent ? fContent->Address() : 0;
112  }
113  /** String conversion */
114  std::string ToString() const {
115  return fContent->ToString();
116  }
117  /** String conversion */
118  void FromString(const std::string& val) {
119  fContent->FromString(val);
120  }
121 private:
122  /**
123  * @class Placeholder G4AnyType.h G4AnyType.h
124  */
125  class Placeholder {
126  public:
127  /** Constructor */
128  Placeholder() {}
129  /** Destructor */
130  virtual ~Placeholder() {}
131  /** Query */
132  virtual const std::type_info& TypeInfo() const = 0;
133  /** Query */
134  virtual Placeholder* Clone() const = 0;
135  /** Query */
136  virtual void* Address() const = 0;
137  /** ToString */
138  virtual std::string ToString() const = 0;
139  /** FromString */
140  virtual void FromString(const std::string& val) = 0;
141  };
142 
143  template <typename ValueType> class Ref: public Placeholder {
144  public:
145  /** Constructor */
146  Ref(ValueType& value): fRef(value) {}
147  /** Query */
148  virtual const std::type_info& TypeInfo() const {
149  return typeid(ValueType);
150  }
151  /** Clone */
152  virtual Placeholder* Clone() const {
153  return new Ref(fRef);
154  }
155  /** Address */
156  virtual void* Address() const {
157  return (void*) (&fRef);
158  }
159  /** ToString */
160  virtual std::string ToString() const {
161  std::stringstream ss;
162  ss << fRef;
163  return ss.str();
164  }
165  /** FromString */
166  virtual void FromString(const std::string& val) {
167  std::stringstream ss(val);
168  ss >> fRef;
169  }
170  /** representation */
171  ValueType& fRef;
172  };
173  /** representation */
174  template <typename ValueType> friend ValueType* any_cast(G4AnyType*);
175  /** representation */
176  Placeholder* fContent;
177 };
178 
179 /**
180  * Specializations
181  */
182 
183 template <> inline void G4AnyType::Ref<bool>::FromString(const std::string& val) {
184  fRef = G4UIcommand::ConvertToBool(val.c_str());
185 }
186 
187 template <> inline void G4AnyType::Ref<G4String>::FromString(const std::string& val) {
188  if (val[0] == '"' ) fRef = val.substr(1,val.size()-2);
189  else fRef = val;
190 }
191 
192 template <> inline void G4AnyType::Ref<G4ThreeVector>::FromString(const std::string& val) {
193  fRef = G4UIcommand::ConvertTo3Vector(val.c_str());
194 }
195 
196 
197 /**
198  * @class G4BadAnyCast G4AnyType.h Reflex/G4AnyType.h
199  * @author K. Henney
200  */
201 class G4BadAnyCast: public std::bad_cast {
202 public:
203  /** Constructor */
205 
206  /** Query */
207  virtual const char* what() const throw() {
208  return "G4BadAnyCast: failed conversion using any_cast";
209  }
210 };
211 
212 /** value */
213 template <typename ValueType> ValueType* any_cast(G4AnyType* operand) {
214  return operand && operand->TypeInfo() == typeid(ValueType)
215  ? &static_cast<G4AnyType::Ref<ValueType>*>(operand->fContent)->fRef : 0;
216 }
217 /** value */
218 template <typename ValueType> const ValueType* any_cast(const G4AnyType* operand) {
219  return any_cast<ValueType>(const_cast<G4AnyType*>(operand));
220 }
221 /** value */
222 template <typename ValueType> ValueType any_cast(const G4AnyType& operand) {
223  const ValueType* result = any_cast<ValueType>(&operand);
224  if (!result) {
225  throw G4BadAnyCast();
226  }
227  return *result;
228 }
229 
230 #endif
G4AnyType(const G4AnyType &other)
Definition: G4AnyType.hh:74
void * Address() const
Definition: G4AnyType.hh:110
typedef void(XMLCALL *XML_ElementDeclHandler)(void *userData
virtual const char * what() const
Definition: G4AnyType.hh:207
const std::type_info & TypeInfo() const
Definition: G4AnyType.hh:106
static G4ThreeVector ConvertTo3Vector(const char *st)
Definition: G4UIcommand.cc:449
G4AnyType(ValueType &value)
Definition: G4AnyType.hh:70
void FromString(const std::string &val)
Definition: G4AnyType.hh:118
std::string ToString() const
Definition: G4AnyType.hh:114
bool Empty() const
Definition: G4AnyType.hh:102
static G4bool ConvertToBool(const char *st)
Definition: G4UIcommand.cc:411
void swap(shared_ptr< P > &, shared_ptr< P > &)
Definition: memory.h:1247
friend ValueType * any_cast(G4AnyType *)
Definition: G4AnyType.hh:213
~G4AnyType()
Definition: G4AnyType.hh:78
G4AnyType & Swap(G4AnyType &rhs)
Definition: G4AnyType.hh:87
const XML_Char int const XML_Char * value
ValueType * any_cast(G4AnyType *operand)
Definition: G4AnyType.hh:213
G4AnyType & operator=(const ValueType &rhs)
Definition: G4AnyType.hh:92