diff --git a/DDCore/include/DD4hep/Grammar.h b/DDCore/include/DD4hep/Grammar.h index 2d500a0f9deba584c7947fc080c39539f541533d..53549798452dccf00afc812c4ab0ccfcacdf6731 100644 --- a/DDCore/include/DD4hep/Grammar.h +++ b/DDCore/include/DD4hep/Grammar.h @@ -69,6 +69,8 @@ namespace dd4hep { /// Structure to be filled if automatic object parsing from string is supposed to be supported struct specialization_t { + /// Ponter to ABI Cast structure + const Cast* cast = 0; /// Bind opaque address to object void (*bind)(void* pointer) = 0; /// Opaque copy constructor @@ -120,6 +122,7 @@ namespace dd4hep { static void invalidConversion(const std::type_info& from, const std::type_info& to); /// Error callback on invalid conversion static void invalidConversion(const std::string& value, const std::type_info& to); + /// Access the hash value for this grammar type key_type hash() const { return hash_value; } /// Access to the type information name @@ -134,12 +137,14 @@ namespace dd4hep { if ( inited ) return root_class; return initialized_clazz(); } + /// Set cast structure + virtual void setCast(const Cast* cast) const; + /// Access ABI object cast + virtual const Cast& cast() const; /// Access to the type information virtual bool equals(const std::type_info& other_type) const = 0; /// Access to the type information virtual const std::type_info& type() const = 0; - /// Access ABI object cast - virtual const Cast& cast() const = 0; /// Access the object size (sizeof operator) virtual size_t sizeOf() const = 0; /// Opaque object destructor @@ -179,8 +184,6 @@ namespace dd4hep { virtual size_t sizeOf() const override; /// Opaque object destructor virtual void destruct(void* pointer) const override; - /// Access ABI object cast - virtual const Cast& cast() const override; /// Bind opaque address to object template <typename... Args> void construct(void* pointer, Args... args) const; }; @@ -200,11 +203,6 @@ namespace dd4hep { return typeid(TYPE); } - /// Access ABI object cast - template <typename TYPE> const Cast& Grammar<TYPE>::cast() const { - return Cast::instance<TYPE>(); - } - /// Access to the type information template <typename TYPE> bool Grammar<TYPE>::equals(const std::type_info& other_type) const { return other_type == typeid(TYPE); diff --git a/DDCore/src/Grammar.cpp b/DDCore/src/Grammar.cpp index 806c9581210a9b04a94a1a6ea706982d9ba90779..f28ae012d12c57fd669af98203b0f69d9d38dd27 100644 --- a/DDCore/src/Grammar.cpp +++ b/DDCore/src/Grammar.cpp @@ -160,6 +160,20 @@ void dd4hep::BasicGrammar::invalidConversion(const std::type_info& from, const s "' to '" + to_name + "' is not implemented."); } +/// Set cast structure +void dd4hep::BasicGrammar::setCast(const Cast* cast) const { + BasicGrammar* g = const_cast<BasicGrammar*>(this); + g->specialization.cast = cast; +} + +/// Access ABI object cast +const dd4hep::Cast& dd4hep::BasicGrammar::cast() const { + if ( specialization.cast ) + return *specialization.cast; + except("Grammar","Cannot serialize object with incomplete grammar: %s",type_name().c_str()); + return *specialization.cast; +} + /// Serialize an opaque value to a string std::string dd4hep::BasicGrammar::str(const void* ptr) const { if ( specialization.str ) diff --git a/examples/Conditions/src/Conditions_dynamic.cpp b/examples/Conditions/src/Conditions_dynamic.cpp index 64252dcc59a84bac87063e6f4f74332adcc11f79..f893463b02c9de48046a6ef1b69f50fff4b5e8ed 100644 --- a/examples/Conditions/src/Conditions_dynamic.cpp +++ b/examples/Conditions/src/Conditions_dynamic.cpp @@ -93,8 +93,13 @@ static int condition_example (Detector& /* description */, int /* argc */, char* #define DATA_2 2.2222e2 Condition cond("Cond","payload"); - payload& p = cond.bind<payload>(); + payload& p = cond.bind<payload>(); + BasicGrammar::instance<payload>().setCast( &Cast::instance<payload>() ); + BasicGrammar::instance<base_0>().setCast( &Cast::instance<base_0>() ); + BasicGrammar::instance<base_1<int> >().setCast( &Cast::instance<base_1<int> >() ); + BasicGrammar::instance<base_2<double> >().setCast( &Cast::instance<base_2<double> >() ); + p.data_0 = DATA_0; p.data_1 = DATA_1; p.data_2 = DATA_2;