Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Example6.cpp

00001 
00002 #include"ImLib3D.hpp"
00003 
00004 // Demonstrates how to override the Streamable class and how to use the XMLBinaryFile
00005 
00006 class MyStreamableClass : public Streamable
00007 {
00008     float someComplexBinaryData; //Your complex binary data that you want to be storable
00009 public:
00011     void Read( ImLib3DFile *file,XMLNode *parentNode=NULL,XMLNode *node=NULL);
00012     void Write(ImLib3DFile *file,XMLNode *parentNode=NULL,XMLNode *node=NULL) const;
00013     MyStreamableClass(float theData) : someComplexBinaryData(theData)
00014     {
00015         ;
00016     }
00017     MyStreamableClass(const string &fname)
00018     {
00019         ReadFromFile(fname); // Calls ReadFromFile which in turn calls Read
00020     }
00021 };
00022 
00023 int main (int argc, char** argv)
00024 {
00025     MyStreamableClass myObj(3);
00026     
00027     string fname="streamable.test";
00028     myObj.WriteToFile(fname); // The method Streamable::WriteToFile() calls Write();
00029 
00030     
00031 }
00032 
00033 // Implementations -----------------------------------------------------
00034 
00035 // XMLBinaryFile makes it possible to store binary information along with the text. 
00036 // To do this it write the data into a temporary file and joins the files afterwards. 
00037 inline 
00038 void 
00039 MyStreamableClass::Write( ImLib3DFile *file,XMLNode *parentNode,XMLNode *node) const
00040 {
00041     // First we need to create the top node with our proper name
00042     node = CreateWriteNode("MyStreamableClassFile",file,parentNode,node);
00043 
00044     // Or/And we can add binary node(s) :
00045     XMLNode* dataNode = file->AddChild(node, "DataNode"); //conceptionally we want this to be node on its own
00046     file->AddBinaryNode(dataNode, sizeof(float));
00047     
00048     // Finally we need to write the data itself
00049     file->BinaryWrite((void*)(&someComplexBinaryData), sizeof(float));
00050     
00051     // The temp text and data will be joined automatically on file close (or file object destruction)
00052 }
00053 
00054 inline 
00055 void 
00056 MyStreamableClass::Read( ImLib3DFile *file,XMLNode *parentNode,XMLNode *node)
00057 {
00058     // First we need to find the top node
00059     node = CreateReadNode("MyStreamableClassFile",file,parentNode,node); // The name MUST correspond to the name in Write
00060 
00061     // Find the binary node:
00062     XMLNode* dataNode = file -> GetChild(node, "DataNode");
00063 
00064     file->ReadBinaryNode(dataNode);
00065 
00066     file->BinaryRead((void*)(&someComplexBinaryData), sizeof(float)); 
00067 
00068     // Optionally you can test if the data was all read:
00069     if(!file->CheckBinaryReadComplete())
00070     {ThrowError("ImageVectorsFile::Read bad read size ");}
00071 }
00072 

Generated on Fri Jun 17 13:36:01 2005 for ImLib3D by  doxygen 1.4.2