Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
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 // $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 
63 class G4AnyType {
64 public:
67  fContent(0) {}
68 
70  template <typename ValueType> G4AnyType(ValueType &value):
71  fContent(new Ref<ValueType>(value)) {}
72 
74  G4AnyType(const G4AnyType &other):
75  fContent(other.fContent ? other.fContent->Clone() : 0) {}
76 
79  delete fContent;
80  }
81 
83  operator bool() {
84  return !Empty();
85  }
88  std::swap(fContent, rhs.fContent);
89  return *this;
90  }
92  template <typename ValueType> G4AnyType& operator =(const ValueType& rhs) {
93  G4AnyType(rhs).Swap(*this);
94  return *this;
95  }
98  G4AnyType(rhs).Swap(*this);
99  return *this;
100  }
102  bool Empty() const {
103  return !fContent;
104  }
106  const std::type_info& TypeInfo() const {
107  return fContent ? fContent->TypeInfo() : typeid(void);
108  }
110  void* Address() const {
111  return fContent ? fContent->Address() : 0;
112  }
114  std::string ToString() const {
115  return fContent->ToString();
116  }
118  void FromString(const std::string& val) {
119  fContent->FromString(val);
120  }
121 private:
125  class Placeholder {
126  public:
130  virtual ~Placeholder() {}
132  virtual const std::type_info& TypeInfo() const = 0;
134  virtual Placeholder* Clone() const = 0;
136  virtual void* Address() const = 0;
138  virtual std::string ToString() const = 0;
140  virtual void FromString(const std::string& val) = 0;
141  };
142 
143  template <typename ValueType> class Ref: public Placeholder {
144  public:
146  Ref(ValueType& value): fRef(value) {}
148  virtual const std::type_info& TypeInfo() const {
149  return typeid(ValueType);
150  }
152  virtual Placeholder* Clone() const {
153  return new Ref(fRef);
154  }
156  virtual void* Address() const {
157  return (void*) (&fRef);
158  }
160  virtual std::string ToString() const {
161  std::stringstream ss;
162  ss << fRef;
163  return ss.str();
164  }
166  virtual void FromString(const std::string& val) {
167  std::stringstream ss(val);
168  ss >> fRef;
169  }
171  ValueType& fRef;
172  };
174  template <typename ValueType> friend ValueType* any_cast(G4AnyType*);
177 };
178 
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 
201 class G4BadAnyCast: public std::bad_cast {
202 public:
205 
207  virtual const char* what() const throw() {
208  return "G4BadAnyCast: failed conversion using any_cast";
209  }
210 };
211 
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 }
218 template <typename ValueType> const ValueType* any_cast(const G4AnyType* operand) {
219  return any_cast<ValueType>(const_cast<G4AnyType*>(operand));
220 }
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
std::string ToString() const
Definition: G4AnyType.hh:114
Ref(ValueType &value)
Definition: G4AnyType.hh:146
Placeholder * fContent
Definition: G4AnyType.hh:176
friend ValueType * any_cast(G4AnyType *)
Definition: G4AnyType.hh:213
G4AnyType & Swap(G4AnyType &rhs)
Definition: G4AnyType.hh:87
void FromString(const std::string &val)
Definition: G4AnyType.hh:118
virtual std::string ToString() const =0
static G4bool ConvertToBool(const char *st)
Definition: G4UIcommand.cc:439
virtual std::string ToString() const
Definition: G4AnyType.hh:160
virtual void * Address() const =0
G4AnyType(const G4AnyType &other)
Definition: G4AnyType.hh:74
virtual Placeholder * Clone() const =0
virtual void FromString(const std::string &val)
Definition: G4AnyType.hh:166
G4AnyType(ValueType &value)
Definition: G4AnyType.hh:70
ValueType * any_cast(G4AnyType *operand)
Definition: G4AnyType.hh:213
virtual Placeholder * Clone() const
Definition: G4AnyType.hh:152
const XML_Char int const XML_Char * value
Definition: expat.h:331
virtual const char * what() const
Definition: G4AnyType.hh:207
virtual void * Address() const
Definition: G4AnyType.hh:156
bool Empty() const
Definition: G4AnyType.hh:102
virtual const std::type_info & TypeInfo() const =0
~G4AnyType()
Definition: G4AnyType.hh:78
static G4ThreeVector ConvertTo3Vector(const char *st)
Definition: G4UIcommand.cc:477
G4double G4ParticleHPJENDLHEData::G4double result
G4AnyType & operator=(const ValueType &rhs)
Definition: G4AnyType.hh:92
const std::type_info & TypeInfo() const
Definition: G4AnyType.hh:106
ValueType & fRef
Definition: G4AnyType.hh:171
typedef void(XMLCALL *XML_ElementDeclHandler)(void *userData
virtual void FromString(const std::string &val)=0
void * Address() const
Definition: G4AnyType.hh:110
virtual const std::type_info & TypeInfo() const
Definition: G4AnyType.hh:148
static int operand(pchar begin, pchar end, double &result, pchar &endp, const dic_type &dictionary)
Definition: Evaluator.cc:164