Text archives Help
- From: sparker@sci.utah.edu
- To: manta@sci.utah.edu
- Subject: [Manta] r1800 - in branches/persistent: Core/Persistent Model/Primitives StandAlone
- Date: Thu, 1 Nov 2007 22:44:45 -0600 (MDT)
Author: sparker
Date: Thu Nov 1 22:44:43 2007
New Revision: 1800
Modified:
branches/persistent/Core/Persistent/ArchiveElement.cc
branches/persistent/Core/Persistent/ArchiveElement.h
branches/persistent/Core/Persistent/MantaRTTI.cc
branches/persistent/Core/Persistent/MantaRTTI.h
branches/persistent/Core/Persistent/XMLArchive.cc
branches/persistent/Core/Persistent/stdRTTI.h
branches/persistent/Model/Primitives/Parallelogram.cc
branches/persistent/StandAlone/savescene.cc
Log:
Implementing pointer merges on read. Persistent object functionality is now
largely functional and will be merged into the trunk very soon.
Modified: branches/persistent/Core/Persistent/ArchiveElement.cc
==============================================================================
--- branches/persistent/Core/Persistent/ArchiveElement.cc (original)
+++ branches/persistent/Core/Persistent/ArchiveElement.cc Thu Nov 1
22:44:43 2007
@@ -17,8 +17,3 @@
ArchiveElement::~ArchiveElement()
{
}
-
-ArchiveElement::PointerWrapper::~PointerWrapper()
-{
-}
-
Modified: branches/persistent/Core/Persistent/ArchiveElement.h
==============================================================================
--- branches/persistent/Core/Persistent/ArchiveElement.h (original)
+++ branches/persistent/Core/Persistent/ArchiveElement.h Thu Nov 1
22:44:43 2007
@@ -4,14 +4,11 @@
#include <Core/Persistent/MantaRTTI.h>
#include <Core/Exceptions/SerializationError.h>
-#include <Core/Util/NotFinished.h>
#include <string>
#include <typeinfo>
namespace Manta {
class ArchiveElement {
- protected:
- class PointerWrapper;
public:
bool reading() const {
return isreading;
@@ -36,76 +33,11 @@
virtual bool nextContainerElement() = 0;
protected:
- virtual void readwrite(const std::string& fieldname, PointerWrapper&
ptr, bool isPointer) = 0;
+ virtual void readwrite(const std::string& fieldname,
PointerWrapperInterface& ptr, bool isPointer) = 0;
virtual ~ArchiveElement();
ArchiveElement(bool isreading);
bool isreading;
-
- class PointerWrapper {
- public:
- virtual ~PointerWrapper();
-
- virtual bool isNull() const = 0;
- virtual void setNull() = 0;
- virtual bool createObject(const std::string& classname) = 0;
- virtual bool copyPointer(PointerWrapper* copy) = 0;
- virtual void* getUniqueID() const = 0;
- virtual void readwrite(ArchiveElement*) = 0;
- virtual const GenericRTTIInterface* getRTTI() const = 0;
- virtual PointerWrapper* clone() const = 0;
- };
-
- template<class T>
- class ConcretePointerWrapper : public PointerWrapper {
- public:
- ConcretePointerWrapper(T* data, const ClassRTTIInterface<T>* rtti)
- : data(data), rtti(rtti) {
- }
- virtual ~ConcretePointerWrapper() {}
- T* getData() const {
- return data;
- }
-
- bool isNull() const {
- return data == 0;
- }
- void setNull() {
- data = 0;
- }
- virtual bool createObject(const std::string& classname) {
- ClassRTTIInterface<T>* new_rtti = ClassIndex<T>::getRTTI(classname);
- if(!new_rtti)
- return false;
- data = new_rtti->createInstance();
- if(!data)
- return false;
- rtti = new_rtti;
- return true;
- }
- virtual bool copyPointer(PointerWrapper* copy) {
- NOT_FINISHED("copyPointer");
- return false;
- }
- virtual void* getUniqueID() const {
- // This will cast it to the most derived class and return the void*
equivalent.
- // This ensures that the pointer will be unique.
- return rtti->getPointerValue(data);
- }
- virtual void readwrite(ArchiveElement* element) {
- rtti->readwrite(element, data);
- }
- virtual const GenericRTTIInterface* getRTTI() const {
- return rtti;
- }
- virtual ConcretePointerWrapper<T>* clone() const {
- return new ConcretePointerWrapper(data, rtti);
- }
- private:
- T* data;
- const ClassRTTIInterface<T>* rtti;
- };
-
};
template<class T>
@@ -114,13 +46,13 @@
if(writing() && typeid(T) != typeid(data))
throw SerializationError("Cannot serialize reference to derived
class");
- ClassRTTIInterface<T>* rtti = ClassIndex<T>::getRTTI(typeid(T));
+ const ClassRTTIInterface<T>* rtti = ClassIndex<T>::getRTTI(typeid(T));
if(!rtti){
std::string baseclassname = MantaRTTI<T>::getPublicClassname();
std::string derivedclassname = typeid(data).name();
throw SerializationError("Class: " + derivedclassname + " is not a
known subclass of " + baseclassname + "(probably missing RTTI information)");
}
- ConcretePointerWrapper<T> ptr(&data, rtti);
+ PointerWrapper<T> ptr(&data, rtti);
readwrite(fieldname, ptr, false);
}
@@ -128,7 +60,7 @@
template<class T>
void ArchiveElement::readwrite(const std::string& fieldname, T*& data)
{
- ClassRTTIInterface<T>* rtti;
+ const ClassRTTIInterface<T>* rtti;
if(reading() || !data)
rtti = ClassIndex<T>::getRTTI(typeid(T));
else
@@ -138,16 +70,16 @@
std::string derivedclassname = typeid(*data).name();
throw SerializationError("Class: " + derivedclassname + " is not a
known subclass of " + baseclassname + "(probably missing RTTI information)");
}
- ConcretePointerWrapper<T> ptr(data, rtti);
+ PointerWrapper<T> ptr(data, rtti);
readwrite(fieldname, ptr, true);
if(reading())
- data = ptr.getData();
+ data = ptr.getPointer();
}
template<class T>
void ArchiveElement::readwrite(const std::string& fieldname, const T*&
data) {
T* tmp = const_cast<T*>(data);
- ClassRTTIInterface<T>* rtti;
+ const ClassRTTIInterface<T>* rtti;
if(reading() || !tmp)
rtti = ClassIndex<T>::getRTTI(typeid(T));
else
@@ -157,10 +89,10 @@
std::string derivedclassname = typeid(*tmp).name();
throw SerializationError("Class: " + derivedclassname + " is not a
known subclass of " + baseclassname + "(probably missing RTTI information)");
}
- ConcretePointerWrapper<T> ptr(tmp, rtti);
+ PointerWrapper<T> ptr(tmp, rtti);
readwrite(fieldname, ptr, true);
if(reading())
- data = ptr.getData();
+ data = ptr.getPointer();
}
}//namespace
Modified: branches/persistent/Core/Persistent/MantaRTTI.cc
==============================================================================
--- branches/persistent/Core/Persistent/MantaRTTI.cc (original)
+++ branches/persistent/Core/Persistent/MantaRTTI.cc Thu Nov 1 22:44:43
2007
@@ -6,3 +6,7 @@
GenericRTTIInterface::~GenericRTTIInterface()
{
}
+
+PointerWrapperInterface::~PointerWrapperInterface()
+{
+}
Modified: branches/persistent/Core/Persistent/MantaRTTI.h
==============================================================================
--- branches/persistent/Core/Persistent/MantaRTTI.h (original)
+++ branches/persistent/Core/Persistent/MantaRTTI.h Thu Nov 1 22:44:43
2007
@@ -71,6 +71,8 @@
static void registerClassAndParents(const std::string& classname);
};
+ class PointerWrapperInterface;
+
/**
* This is the interface to dynamic RTTI. Use the ClassIndex below to get
* instances of this given a classname or a typeid. Functionality is
limited
@@ -84,6 +86,21 @@
virtual const std::type_info& get_typeinfo() const = 0;
};
+ class PointerWrapperInterface {
+ public:
+ virtual ~PointerWrapperInterface();
+
+ virtual bool isNull() const = 0;
+ virtual void setNull() = 0;
+ virtual bool upcast(PointerWrapperInterface* destination) = 0;
+ virtual void* getUniqueID() const = 0;
+ virtual void readwrite(ArchiveElement*) = 0;
+ virtual const GenericRTTIInterface* getRTTI() const = 0;
+ virtual bool createObject(const std::string& classname,
PointerWrapperInterface** derivedptr = 0) = 0;
+ };
+
+ template<class T> class PointerWrapper;
+
/**
* This is the interface to dynamic RTTI for types derived from the given
Base.
* In addition to the capabilities of the GenericRTTIInterface, you can
@@ -94,11 +111,55 @@
template<class Base> class ClassRTTIInterface : public
GenericRTTIInterface {
public:
virtual ~ClassRTTIInterface() {}
- virtual Base* createInstance() const = 0;
+ virtual Base* createObject(PointerWrapperInterface** derivedptr) const =
0;
virtual void readwrite(ArchiveElement* archive, Base* ptr) const = 0;
virtual void* getPointerValue(Base* ptr) const = 0;
};
+ template<class T>
+ class PointerWrapper : public PointerWrapperInterface {
+ public:
+ PointerWrapper(T* ptr, const ClassRTTIInterface<T>* rtti)
+ : ptr(ptr), rtti(rtti) {
+ }
+ virtual ~PointerWrapper() {}
+ T* getPointer() const {
+ return ptr;
+ }
+ void setPointer(T* newptr) {
+ ptr = newptr;
+ }
+
+ bool isNull() const {
+ return ptr == 0;
+ }
+ void setNull() {
+ ptr = 0;
+ }
+ virtual bool upcast(PointerWrapperInterface* destination) {
+ if(!ptr)
+ return 0;
+ return MantaRTTI<T>::upcast(ptr, destination);
+ }
+ virtual bool createObject(const std::string& classname,
+ PointerWrapperInterface** derivedptr = 0);
+ virtual void* getUniqueID() const {
+ // This will cast it to the most derived class and return
+ // the void* equivalent. This ensures that the pointer will be
+ // unique.
+ return rtti->getPointerValue(ptr);
+ }
+ virtual void readwrite(ArchiveElement* element) {
+ rtti->readwrite(element, ptr);
+ }
+ virtual const GenericRTTIInterface* getRTTI() const {
+ return rtti;
+ }
+ private:
+ T* ptr;
+ const ClassRTTIInterface<T>* rtti;
+ };
+
/**
* This class indexes MantaRTTI information for all of the known subclasses
* of a particular class. For example, for the following classes:
@@ -111,7 +172,7 @@
*
* This provides access to the MantaRTTI information through an
* abstract interface. This design avoids the need to require virtual
- * functions in the class.
+ * specific virtual functions in the class.
*/
template<class Base>
class ClassIndex {
@@ -129,8 +190,11 @@
virtual const std::type_info& get_typeinfo() const {
return typeid(Derived);
}
- virtual Derived* createInstance() const {
- return MantaRTTI<Derived>::createInstance();
+ virtual Derived* createObject(PointerWrapperInterface** derivedptr)
const {
+ Derived* result = MantaRTTI<Derived>::createInstance();
+ if(result && derivedptr)
+ *derivedptr = new PointerWrapper<Derived>(result,
ClassIndex<Derived>::getRTTI(typeid(Derived)));
+ return result;
}
virtual void readwrite(ArchiveElement* archive, Base* ptr) const {
Derived* derivedptr = dynamic_cast<Derived*>(ptr);
@@ -182,10 +246,10 @@
}
}
- static ClassRTTIInterface<Base>* getRTTI(const std::string& name) {
+ static const ClassRTTIInterface<Base>* getRTTI(const std::string& name) {
return singleton()->lookupEntry(name);
}
- static ClassRTTIInterface<Base>* getRTTI(const std::type_info& ti){
+ static const ClassRTTIInterface<Base>* getRTTI(const std::type_info& ti){
return singleton()->lookupEntry(ti);
}
@@ -198,13 +262,13 @@
singleton_instance = new ClassIndex<Base>();
return singleton_instance;
}
- ClassRTTIInterface<Base>* lookupEntry(const std::string& name) const {
+ const ClassRTTIInterface<Base>* lookupEntry(const std::string& name)
const {
typename std::map<std::string,
ClassRTTIInterface<Base>*>::const_iterator iter = index.find(name);
if(iter == index.end())
return 0;
return iter->second;
}
- ClassRTTIInterface<Base>* lookupEntry(const std::type_info& ti) const {
+ const ClassRTTIInterface<Base>* lookupEntry(const std::type_info& ti)
const {
typename std::map<std::string,
ClassRTTIInterface<Base>*>::const_iterator iter = index.find(ti.name());
if(iter == index.end())
return 0;
@@ -219,6 +283,21 @@
template<class Class>
ClassIndex<Class>* ClassIndex<Class>::singleton_instance;
+
+ template<class T>
+ bool PointerWrapper<T>::createObject(const std::string& classname,
+ PointerWrapperInterface** derivedptr)
+ {
+ const ClassRTTIInterface<T>* new_rtti =
ClassIndex<T>::getRTTI(classname);
+ if(!new_rtti)
+ return false;
+ ptr = new_rtti->createObject(derivedptr);
+ if(!ptr)
+ return false;
+ rtti = new_rtti;
+ return true;
+ }
+
// Helper classes for implementing MantaRTTI specializations
template<class T>
class MantaRTTI_readwriteMethod {
@@ -292,6 +371,16 @@
static void registerClassAndParents(const std::string& classname) {
ClassIndex<T>::template registerClass<C>(classname);
}
+
+ static bool upcast(T* ptr, PointerWrapperInterface* destination) {
+ PointerWrapper<T>* dp = dynamic_cast<PointerWrapper<T>*>(destination);
+ if(dp){
+ dp->setPointer(ptr);
+ return true;
+ } else {
+ return false;
+ }
+ }
private:
MantaRTTI_BaseClass();
static std::string* classname;
@@ -313,6 +402,16 @@
ClassIndex<T>::template registerClass<C>(classname);
MantaRTTI<Parent>::template registerClassAndParents<C>(classname);
}
+
+ static bool upcast(T* ptr, PointerWrapperInterface* destination) {
+ PointerWrapper<T>* dp = dynamic_cast<PointerWrapper<T>*>(destination);
+ if(dp){
+ dp->setPointer(ptr);
+ return true;
+ } else {
+ return MantaRTTI<Parent>::upcast(ptr, destination);
+ }
+ }
private:
MantaRTTI_DerivedClass();
// This is a string* instead of a string to avoid initialization order
issues
@@ -344,6 +443,15 @@
ClassIndex<T>::template registerClass<C>(classname);
MantaRTTI<Parent1>::template registerClassAndParents<C>(classname);
MantaRTTI<Parent2>::template registerClassAndParents<C>(classname);
+ }
+ static bool upcast(T* ptr, PointerWrapperInterface* destination) {
+ PointerWrapper<T>* dp = dynamic_cast<PointerWrapper<T>*>(ptr);
+ if(dp){
+ dp->setPointer(ptr);
+ return true;
+ } else {
+ return MantaRTTI<Parent1>::upcast(ptr, destination) ||
MantaRTTI<Parent2>::upcast(ptr, destination);
+ }
}
private:
MantaRTTI_DerivedClass2();
Modified: branches/persistent/Core/Persistent/XMLArchive.cc
==============================================================================
--- branches/persistent/Core/Persistent/XMLArchive.cc (original)
+++ branches/persistent/Core/Persistent/XMLArchive.cc Thu Nov 1 22:44:43
2007
@@ -37,11 +37,11 @@
virtual void readwrite(const std::string& fieldname, int& data);
virtual void readwrite(const std::string& fieldname, std::string& data);
- virtual void readwrite(const std::string& fieldname, PointerWrapper&
ptr, bool isPointer);
+ virtual void readwrite(const std::string& fieldname,
PointerWrapperInterface& ptr, bool isPointer);
virtual bool nextContainerElement();
- typedef std::map<std::string, XMLArchiveElement::PointerWrapper*>
pointermaptype;
+ typedef std::map<std::string, PointerWrapperInterface*> pointermaptype;
protected:
XMLArchive* archive;
xmlNodePtr node;
@@ -380,9 +380,8 @@
}
}
-void XMLArchiveElement::readwrite(const std::string& fieldname,
PointerWrapper& ptr, bool isPointer)
+void XMLArchiveElement::readwrite(const std::string& fieldname,
PointerWrapperInterface& ptr, bool isPointer)
{
- cerr << "Reading field: " << fieldname << '\n';
if(reading()){
if(element_type == TagElement || element_type == UsedTagElement)
throw SerializationError("Object cannot be stored in a property: " +
fieldname);
@@ -398,20 +397,18 @@
if(!isPointer)
throw SerializationError("Cannot read a reference to a null
pointer");
ptr.setNull();
- cerr << "Null\n";
return;
}
}
if(element_type == ContainerElement){
- cerr << "Is a container element\n";
// Second, see if this is a container - it must be handled separately
// If this is a container element, try to create the next class
if(!node || !current_container_child)
throw SerializationError("Container must iterate reads with
nextContainerObject");
string classname = get_classname(current_container_child, fieldname);
- cerr << "Creating container class: " << classname << " from
current_container_child: " << current_container_child << '\n';
+ PointerWrapperInterface* newobj = 0;
if(isPointer){
- if(!ptr.createObject(classname))
+ if(!ptr.createObject(classname, &newobj))
throw SerializationError("Cannot instantiate class: " + classname
+ " for field: " + fieldname);
}
const GenericRTTIInterface* classinfo = ptr.getRTTI();
@@ -421,12 +418,14 @@
child_element_type = ContainerElement;
// See if the object has a tag called "id". If so, save that as the
pointer name
- xmlChar* id = xmlGetProp(current_container_child, to_xml_ch_ptr("id"));
- if(id){
- string sid(to_char_ptr(id));
- if(archive->pointermap.find(sid) != archive->pointermap.end())
- throw SerializationError("Pointer: " + sid + " defined twice while
reading field: " + fieldname + " in class: " + classname);
- archive->pointermap.insert(std::make_pair(sid, ptr.clone()));
+ if(isPointer){
+ xmlChar* id = xmlGetProp(current_container_child,
to_xml_ch_ptr("id"));
+ if(id){
+ string sid(to_char_ptr(id));
+ if(archive->pointermap.find(sid) != archive->pointermap.end())
+ throw SerializationError("Pointer: " + sid + " defined twice
while reading field: " + fieldname + " in class: " + classname);
+ archive->pointermap.insert(std::make_pair(sid, newobj));
+ }
}
XMLArchiveElement subelement(archive, current_container_child,
child_element_type);
@@ -449,28 +448,27 @@
// If this is a lightweight object, read it directly. Otherwise, it
// should be the name of a pointer
if(storagehint == PersistentStorage::Lightweight){
- cerr << "Is lightweight\n";
XMLArchiveElement subelement(archive, node, fieldname);
ptr.readwrite(&subelement);
return;
} else {
- cerr << "Is a pointer\n";
string pointername = to_char_ptr(prop);
pointermaptype::iterator iter =
archive->pointermap.find(pointername);
if(iter == archive->pointermap.end())
throw SerializationError("Unknown pointer: " + pointername + "
while reading field: " + fieldname + " (forward references not yet
supported)");
- if(!ptr.copyPointer(iter->second))
+ if(!iter->second->upcast(&ptr)){
+ iter->second->upcast(&ptr);
throw SerializationError("Pointer type mismatch while reading
field: " + fieldname);
+ }
return;
}
}
xmlNodePtr child = 0;
ElementType child_element_type = NormalElement;
- bool object_created;
+ PointerWrapperInterface* newobj = 0;
string classname;
- cerr << "4\n";
// Fourth, look for a node with the fieldname
child = node->children;
while(child && (child->type != XML_ELEMENT_NODE ||
strcmp(to_char_ptr(child->name), fieldname.c_str()) != 0))
@@ -489,9 +487,11 @@
xmlChar* type = xmlGetProp(child, to_xml_ch_ptr("type"));
if(type){
classname = to_char_ptr(type);
- } else if(!isPointer || storagehint == PersistentStorage::Container){
+ } else if(storagehint == PersistentStorage::Container){
classname = "<unknown>";
child_element_type = ContainerElement;
+ } else if(!isPointer){
+ classname = "<fixed>";
} else {
xmlNode* c = child->children;
while(c && c->type != XML_ELEMENT_NODE)
@@ -500,7 +500,6 @@
child = c;
classname = get_classname(c, fieldname);
}
- cerr << "field, class=" << classname << '\n';
c = c->next;
while(c && c->type != XML_ELEMENT_NODE)
c = c->next;
@@ -509,28 +508,24 @@
}
} else if(element_type == RootElement){
// If this is the root element, find the first class that matches
- cerr << "Root:\n";
child = node->children;
do {
while(child && child->type != XML_ELEMENT_NODE)
child = child->next;
if(child){
classname = get_classname(child, fieldname);
- cerr << "Trying root class: " << classname << " from node: " <<
child << '\n';
- if(ptr.createObject(classname))
- object_created = true;
- else
+ if(!ptr.createObject(classname, &newobj))
child = child->next;
} else {
throw SerializationError("Cannot find root object for field: " +
fieldname);
}
- } while(child && !object_created);
+ } while(child && !newobj);
} else {
throw SerializationError("Cannot find field: " + fieldname);
}
- if(isPointer){
- if(!ptr.createObject(classname))
+ if(isPointer && !newobj){
+ if(!ptr.createObject(classname, &newobj))
throw SerializationError("Cannot instantiate class: " + classname +
" for field: " + fieldname);
}
@@ -540,7 +535,7 @@
string sid(to_char_ptr(id));
if(archive->pointermap.find(sid) != archive->pointermap.end())
throw SerializationError("Pointer: " + sid + " defined twice while
reading field: " + fieldname + " in class: " + classname);
- archive->pointermap.insert(std::make_pair(sid, ptr.clone()));
+ archive->pointermap.insert(std::make_pair(sid, newobj));
}
XMLArchiveElement subelement(archive, child, child_element_type);
ptr.readwrite(&subelement);
Modified: branches/persistent/Core/Persistent/stdRTTI.h
==============================================================================
--- branches/persistent/Core/Persistent/stdRTTI.h (original)
+++ branches/persistent/Core/Persistent/stdRTTI.h Thu Nov 1 22:44:43
2007
@@ -9,7 +9,7 @@
namespace Manta {
template<class T>
- class MantaRTTI<std::vector<T> > {
+ class MantaRTTI<std::vector<T> > : public
MantaRTTI_BaseClass<std::vector<T*> > {
public:
static std::string getPublicClassname() {
init.forceinit();
@@ -39,7 +39,7 @@
typename MantaRTTI<std::vector<T> >::Initializer MantaRTTI<std::vector<T>
>
::init;
template<class T>
- class MantaRTTI<std::vector<T*> > {
+ class MantaRTTI<std::vector<T*> > : public
MantaRTTI_BaseClass<std::vector<T*> > {
public:
static std::string getPublicClassname() {
init.forceinit();
Modified: branches/persistent/Model/Primitives/Parallelogram.cc
==============================================================================
--- branches/persistent/Model/Primitives/Parallelogram.cc (original)
+++ branches/persistent/Model/Primitives/Parallelogram.cc Thu Nov 1
22:44:43 2007
@@ -563,8 +563,11 @@
archive->readwrite("anchor", anchor);
archive->readwrite("v1", v1);
archive->readwrite("v2", v2);
+ archive->readwrite("normal", normal);
+ archive->readwrite("offset", d);
if(archive->reading()){
- NOT_FINISHED("Parallelogram readwrite normal/d");
+ double len = normal.normalize();
+ d *= len;
}
}
Modified: branches/persistent/StandAlone/savescene.cc
==============================================================================
--- branches/persistent/StandAlone/savescene.cc (original)
+++ branches/persistent/StandAlone/savescene.cc Thu Nov 1 22:44:43 2007
@@ -37,6 +37,7 @@
#include <Interface/InterfaceRTTI.h>
#include <Interface/Scene.h>
#include <SCIRun/Core/Thread/Thread.h>
+#include <Model/Textures/CheckerTexture.h>
#include <iostream>
#include <string>
@@ -110,6 +111,9 @@
cerr << "savescene.cc (top level): Caught unknown exception\n";
Thread::exitAll(1);
}
+
+ cerr << "Temporary measure to force instantiation of checker texture\n";
+ new CheckerTexture<Color>();
Thread::exitAll(0);
}
- [Manta] r1800 - in branches/persistent: Core/Persistent Model/Primitives StandAlone, sparker, 11/02/2007
Archive powered by MHonArc 2.6.16.