cppbind: make ghost_type static to keep same pointer on reset.

Using a static naGhostType ensures that always the same
address is used for the same ghost and prevents ghost_types
from getting invalid (eg. on reset).
This commit is contained in:
Thomas Geymayer
2013-12-01 01:18:13 +01:00
parent 6fe1433497
commit dfb07f359e
+11 -8
View File
@@ -67,9 +67,6 @@ namespace nasal
bool isBaseOf(naGhostType* ghost_type) const bool isBaseOf(naGhostType* ghost_type) const
{ {
if( ghost_type == &_ghost_type )
return true;
for( DerivedList::const_iterator derived = _derived_classes.begin(); for( DerivedList::const_iterator derived = _derived_classes.begin();
derived != _derived_classes.end(); derived != _derived_classes.end();
++derived ) ++derived )
@@ -86,7 +83,6 @@ namespace nasal
typedef std::vector<const GhostMetadata*> DerivedList; typedef std::vector<const GhostMetadata*> DerivedList;
const std::string _name; const std::string _name;
naGhostType _ghost_type;
DerivedList _derived_classes; DerivedList _derived_classes;
std::vector<naRef> _parents; std::vector<naRef> _parents;
@@ -105,7 +101,7 @@ namespace nasal
( (
SG_NASAL, SG_NASAL,
SG_INFO, SG_INFO,
"Ghost::addDerived: " <<_ghost_type.name << " -> " << derived->_name "Ghost::addDerived: " << _name << " -> " << derived->_name
); );
} }
@@ -549,6 +545,8 @@ namespace nasal
{ {
if( !ghost_type ) if( !ghost_type )
return false; return false;
if( ghost_type == &_ghost_type )
return true;
return getSingletonPtr()->GhostMetadata::isBaseOf(ghost_type); return getSingletonPtr()->GhostMetadata::isBaseOf(ghost_type);
} }
@@ -599,6 +597,8 @@ namespace nasal
template<class> template<class>
friend class Ghost; friend class Ghost;
static naGhostType _ghost_type;
typedef naGhostType* (*type_checker_t)(const raw_type*); typedef naGhostType* (*type_checker_t)(const raw_type*);
typedef std::vector<type_checker_t> DerivedList; typedef std::vector<type_checker_t> DerivedList;
DerivedList _derived_types; DerivedList _derived_types;
@@ -671,7 +671,7 @@ namespace nasal
// If base is not an instance of any derived class, this class has to // If base is not an instance of any derived class, this class has to
// be the dynamic type. // be the dynamic type.
return &getSingletonPtr()->_ghost_type; return &_ghost_type;
} }
template<class BaseGhost> template<class BaseGhost>
@@ -684,7 +684,7 @@ namespace nasal
{ {
// For non polymorphic classes there is no possibility to get the actual // For non polymorphic classes there is no possibility to get the actual
// dynamic type, therefore we can only use its static type. // dynamic type, therefore we can only use its static type.
return &BaseGhost::getSingletonPtr()->_ghost_type; return &BaseGhost::_ghost_type;
} }
static Ghost* getSingletonPtr() static Ghost* getSingletonPtr()
@@ -703,7 +703,7 @@ namespace nasal
c, c,
"method called on object of wrong type: is '%s' expected '%s'", "method called on object of wrong type: is '%s' expected '%s'",
ghost_type ? ghost_type->name : "unknown", ghost_type ? ghost_type->name : "unknown",
getSingletonPtr()->_ghost_type.name _ghost_type.name
); );
return *obj; return *obj;
@@ -894,6 +894,9 @@ namespace nasal
} }
}; };
template<class T>
naGhostType Ghost<T>::_ghost_type;
} // namespace nasal } // namespace nasal
// Needs to be outside any namespace to make ADL work // Needs to be outside any namespace to make ADL work