70 template<
typename ValueType>
71 G4AnyType(ValueType& value) : fContent(new Ref<ValueType>(value))
77 : fContent(other.fContent != nullptr ? other.fContent->Clone() : nullptr)
86 operator bool() {
return !
Empty(); }
92 std::swap(fContent, rhs.fContent);
96 template<
typename ValueType>
115 return fContent !=
nullptr ? fContent->TypeInfo() :
typeid(void);
120 void*
Address()
const {
return fContent !=
nullptr ? fContent->Address() :
nullptr; }
124 std::string
ToString()
const {
return fContent->ToString(); }
126 void FromString(
const std::string& val) { fContent->FromString(val); }
132 Placeholder() =
default;
134 virtual ~Placeholder() =
default;
138 virtual const std::type_info& TypeInfo()
const = 0;
140 virtual Placeholder* Clone()
const = 0;
142 virtual void* Address()
const = 0;
146 virtual std::string ToString()
const = 0;
150 virtual void FromString(
const std::string& val) = 0;
153 template<
typename ValueType>
154 class Ref :
public Placeholder
159 Ref(ValueType& value) : fRef(value) {}
163 const std::type_info& TypeInfo()
const override {
return typeid(ValueType); }
167 Placeholder* Clone()
const override {
return new Ref(fRef); }
171 void* Address()
const override {
return (
void*)(&fRef); }
175 std::string ToString()
const override
177 std::stringstream ss;
184 void FromString(
const std::string& val)
override
186 std::stringstream ss(val);
195 template<
typename ValueType>
198 Placeholder* fContent =
nullptr;
206inline void G4AnyType::Ref<bool>::FromString(
const std::string& val)
212inline void G4AnyType::Ref<G4String>::FromString(
const std::string& val)
215 fRef = val.substr(1, val.size() - 2);
223inline void G4AnyType::Ref<G4ThreeVector>::FromString(
const std::string& val)
237 const char*
what()
const throw()
override
239 return "G4BadAnyCast: failed conversion using any_cast";
245template<
typename ValueType>
248 return operand && operand->
TypeInfo() ==
typeid(ValueType)
249 ? &
static_cast<G4AnyType::Ref<ValueType>*
>(operand->fContent)->fRef
253template<
typename ValueType>
259template<
typename ValueType>
ValueType * any_cast(G4AnyType *operand)
void FromString(const std::string &val)
G4AnyType & Swap(G4AnyType &rhs)
G4AnyType(ValueType &value)
G4AnyType & operator=(const G4AnyType &rhs)
const std::type_info & TypeInfo() const
friend ValueType * any_cast(G4AnyType *)
G4AnyType & operator=(const ValueType &rhs)
G4AnyType(const G4AnyType &other)
std::string ToString() const
const char * what() const override
static G4ThreeVector ConvertTo3Vector(const char *st)
static G4bool ConvertToBool(const char *st)