
00001 // ***************************************************************************** 00002 // File : dyndata.cpp 00003 // Module : 00004 // Description : Example of dynamic data in GizmoBase 00005 // Author : Anders Modén 00006 // Product : GizmoBase 2.1.1 00007 // 00008 // Copyright © 1998 Anders Modén , Sweden 00009 // 00010 // NOTE: The GIZMO package defines a general purpose API for large model 00011 // visualisation and advanced graphic behaviours. The package is similar 00012 // to Cosmo3D, Inventor, Performer etc. but adds automated behaviour 00013 // patterns to the graphics. 00014 // 00015 // 00016 // Revision History... 00017 // 00018 // Who Date Description 00019 // 00020 // AMO 040209 Created file 00021 // 00022 // ****************************************************************************** 00023 00024 #include "gzBaseLibrary.h" 00025 00026 00027 class MyDynamicData : public gzSerializeData 00028 { 00029 public: 00030 00031 // The data, One wide string and one number 00032 00033 gzSerializeStringWide string; 00034 00035 gzSerializeBinary<gzDouble> number; 00036 00037 00038 // The serializing methods (Format) 00039 00040 gzVoid write(gzSerializeAdapter *adapter) // Provide this method to write the data in the correct order 00041 { 00042 string.write(adapter); // Format defined as a string first and then a number 00043 number.write(adapter); 00044 } 00045 00046 gzVoid read(gzSerializeAdapter *adapter) // Provide this method to read the data in the correct order 00047 { 00048 string.read(adapter); 00049 number.read(adapter); 00050 } 00051 00052 gzVoid pushBack(gzSerializeAdapter *adapter) // Provide this method to pushback the data in the correct order 00053 { 00054 number.pushBack(adapter); // Note that this is done in the reversed order !!! 00055 string.pushBack(adapter); 00056 } 00057 00058 gzULong getDataSize(gzSerializeAdapter *adapter=NULL) const // Provide this method to get the correct size of the stored data (in bytes) 00059 { 00060 return string.getDataSize(adapter)+number.getDataSize(adapter); 00061 } 00062 00063 00064 // The type definition (either const char * or gzString) 00065 00066 static const char * getDataTag() // The custom class will be identified by this tag "MyData" 00067 { 00068 return "MyData"; 00069 } 00070 00071 }; 00072 00073 int main(int argc , char *argv[] ) 00074 { 00075 try 00076 { 00077 gzMessage::setMessageLevel(GZ_MESSAGE_DEBUG); 00078 00079 00080 // Create the data structure 00081 00082 MyDynamicData data; 00083 00084 // set some values 00085 00086 data.string=L"Nisse Hült sĺg en CAN buss"; 00087 data.number=667; 00088 00089 00090 gzDynamicTypeContainer container; // Can contain any data 00091 00092 container.setAttribute("testdata",gzDynamicTypeCustom<MyDynamicData>(data)); // Set the attribute "testdata" 00093 00094 00095 // and do some stuff etc... 00096 00097 00098 00099 MyDynamicData data2; 00100 00101 data2=gzDynamic_Cast<MyDynamicData>(container.getAttribute("testdata")); 00102 00103 // Check some data 00104 00105 GZTRACE("String is %s",(const char *)(const gzString &)data2.string); 00106 00107 GZTRACE("Number is %d",(gzDouble)data2.number); 00108 00109 00110 // Some other checks 00111 00112 gzDynamicType rawdata=container.getAttribute("testdata"); 00113 00114 GZTRACE("Type is %s",(const char *)rawdata.getDynamicType()); 00115 00116 00117 } 00118 catch(gzBaseError &error) // In case of exceptions thrown we want to print the message 00119 { 00120 error.reportError(); 00121 } 00122 00123 return 0; 00124 } 00125