E57 Foundation API v1.1.312  Aug. 10, 2011
Functions
examples/CheckInvariant.cpp File Reference

example: use checkInvariant function for ImageFile and other objects. More...

Include dependency graph for CheckInvariant.cpp:

Functions

int main (int, char **)
 Example use of various checkInvariant functions.

Detailed Description

example: use checkInvariant function for ImageFile and other objects.

Also see listing at end of this page for source without line numbers (to cut&paste from).

00001 /*** CheckInvariant.cpp example: use checkInvariant function for ImageFile and other objects */
00004 #include <iostream>
00005 #include "E57Foundation.h"
00006 using namespace e57;
00007 using namespace std;
00008 
00010 int main(int /*argc*/, char** /*argv*/) {
00011     try {
00012         ImageFile imf("temp._e57", "w");
00013             imf.checkInvariant();
00014         StructureNode root = imf.root();
00015             root.checkInvariant();
00016             imf.checkInvariant();
00017             
00018 
00019         // Add three elements: /child1, /child1/grandchild, /child2
00020         StructureNode child1  = StructureNode(imf);
00021             child1.checkInvariant();
00022             imf.checkInvariant();
00023         StructureNode child2  = StructureNode(imf);
00024             child2.checkInvariant();
00025             imf.checkInvariant();
00026         StringNode grandchild = StringNode(imf, "I'm a grandchild");
00027             grandchild.checkInvariant();
00028             imf.checkInvariant();
00029         root.set("child1", child1);
00030             root.checkInvariant();
00031             child1.checkInvariant();
00032         root.set("child2", child2);
00033             root.checkInvariant();
00034             child2.checkInvariant();
00035         child1.set("grandchild", grandchild);
00036             child1.checkInvariant();
00037             grandchild.checkInvariant();
00038 
00039         Node n = root.get("child2");
00040             n.checkInvariant();
00041 
00042         // Create vector /exampleVector, with 2 child elements
00043         VectorNode v(imf, false);
00044             v.checkInvariant();
00045             imf.checkInvariant();
00046         root.set("exampleVector", v);
00047             root.checkInvariant();
00048             v.checkInvariant();
00049         v.append(IntegerNode(imf, 100));
00050             v.checkInvariant();
00051             imf.checkInvariant();
00052         v.append(IntegerNode(imf, 101));
00053             v.checkInvariant();
00054             imf.checkInvariant();
00055 
00056         IntegerNode iNode(imf, 100);
00057             iNode.checkInvariant();
00058             imf.checkInvariant();
00059         root.set("exampleInteger", iNode);
00060             root.checkInvariant();
00061             iNode.checkInvariant();
00062 
00063         ScaledIntegerNode si(imf, 123, 0, 1023, .001, 100.0);
00064             si.checkInvariant();
00065             imf.checkInvariant();
00066         root.set("si1", si);
00067             root.checkInvariant();
00068             si.checkInvariant();
00069 
00070         FloatNode f(imf, 123.45F);
00071             f.checkInvariant();
00072             imf.checkInvariant();
00073         root.set("f1", f);
00074             root.checkInvariant();
00075             f.checkInvariant();
00076 
00077         StringNode s(imf, "a random string");
00078             s.checkInvariant();
00079             imf.checkInvariant();
00080         root.set("s1", s);
00081             root.checkInvariant();
00082             s.checkInvariant();
00083 
00084         const int blobLength = 10;
00085         static uint8_t buf[blobLength] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
00086         BlobNode b = BlobNode(imf, blobLength);
00087             b.checkInvariant();
00088             imf.checkInvariant();
00089         root.set("blob1", b);
00090             root.checkInvariant();
00091             b.checkInvariant();
00092         b.write(buf, 0, blobLength);
00093             b.checkInvariant();
00094 
00095         StructureNode prototype(imf);
00096             prototype.checkInvariant();
00097             imf.checkInvariant();
00098         prototype.set("cartesianX", FloatNode(imf));
00099             prototype.checkInvariant();
00100             imf.checkInvariant();
00101         prototype.set("cartesianY", FloatNode(imf));
00102             prototype.checkInvariant();
00103             imf.checkInvariant();
00104         prototype.set("cartesianZ", FloatNode(imf));
00105             prototype.checkInvariant();
00106             imf.checkInvariant();
00107         
00108         VectorNode codecs(imf, true);
00109             codecs.checkInvariant();
00110             imf.checkInvariant();
00111 
00112         CompressedVectorNode cv(imf, prototype, codecs);
00113             cv.checkInvariant();
00114             imf.checkInvariant();
00115             prototype.checkInvariant();
00116             codecs.checkInvariant();
00117         root.set("points", cv);
00118             root.checkInvariant();
00119             cv.checkInvariant();
00120 
00121         const int N = 4;
00122         static double cartesianX[N] = {1.0, 2.0, 3.0, 4.0};
00123         static double cartesianY[N] = {1.1, 2.1, 3.1, 4.1};
00124         static double cartesianZ[N] = {1.2, 2.2, 3.2, 4.2};
00125         vector<SourceDestBuffer> sourceBuffers;
00126         sourceBuffers.push_back(SourceDestBuffer(imf, "cartesianX",  cartesianX,  N));
00127             sourceBuffers[0].checkInvariant();
00128             imf.checkInvariant();
00129         sourceBuffers.push_back(SourceDestBuffer(imf, "cartesianY",  cartesianY,  N));
00130             sourceBuffers[1].checkInvariant();
00131             imf.checkInvariant();
00132         sourceBuffers.push_back(SourceDestBuffer(imf, "cartesianZ",  cartesianZ,  N));
00133             sourceBuffers[2].checkInvariant();
00134             imf.checkInvariant();
00135         {
00136             CompressedVectorWriter writer = cv.writer(sourceBuffers);
00137                 writer.checkInvariant();
00138                 sourceBuffers[0].checkInvariant();
00139                 sourceBuffers[1].checkInvariant();
00140                 sourceBuffers[2].checkInvariant();
00141             writer.write(N);
00142                 writer.checkInvariant();
00143         }
00144 
00145         imf.extensionsAdd("ext1", "http://www.example.com/MyExtension1");
00146             imf.checkInvariant();
00147         root.set("ext1:greeting", StringNode(imf, "Hello world."));
00148             root.checkInvariant();
00149             imf.checkInvariant();
00150 
00151         imf.close(); // don't forget to explicitly close the ImageFile
00152             imf.checkInvariant();
00153     } catch(E57Exception& ex) {
00154         ex.report(__FILE__, __LINE__, __FUNCTION__);
00155         return(-1);
00156     }
00157 
00158     try {
00159         ImageFile imf("temp._e57", "r");
00160             imf.checkInvariant();
00161         StructureNode root = imf.root();
00162             root.checkInvariant();
00163             imf.checkInvariant();
00164 
00165         CompressedVectorNode cv = static_cast<CompressedVectorNode> (root.get("points"));
00166             cv.checkInvariant();
00167             root.checkInvariant();
00168 
00169         const int N = 10;
00170         static double cartesianX[N];
00171         static double cartesianY[N];
00172         static double cartesianZ[N];
00173         vector<SourceDestBuffer> destBuffers;
00174         destBuffers.push_back(SourceDestBuffer(imf, "cartesianX", cartesianX, N));
00175             destBuffers[0].checkInvariant();
00176             imf.checkInvariant();
00177         destBuffers.push_back(SourceDestBuffer(imf, "cartesianY", cartesianY, N));
00178             destBuffers[1].checkInvariant();
00179             imf.checkInvariant();
00180         destBuffers.push_back(SourceDestBuffer(imf, "cartesianZ", cartesianZ, N));
00181             destBuffers[2].checkInvariant();
00182             imf.checkInvariant();
00183 
00184         if (cv.prototype().type() != E57_STRUCTURE)
00185             return(-1);
00186             cv.checkInvariant();
00187         StructureNode prototype = static_cast<StructureNode>(cv.prototype());
00188             prototype.checkInvariant();
00189             cv.checkInvariant();
00190         {
00191             CompressedVectorReader reader = cv.reader(destBuffers);
00192                 reader.checkInvariant();
00193                 cv.checkInvariant();
00194             
00195             uint64_t totalRead = 0;
00196             unsigned nRead = 0;
00197             while ((nRead = reader.read(destBuffers)) > 0) {
00198                 reader.checkInvariant();
00199                 destBuffers[0].checkInvariant();
00200                 destBuffers[1].checkInvariant();
00201                 destBuffers[2].checkInvariant();
00202 
00203                 cout << "Got " << nRead << " points" << endl;
00204                 for (unsigned i = 0; i < nRead; i++) {
00205                     cout << "point " << totalRead + i << endl;
00206                     cout << "  cartesianX = " << cartesianX[i] << endl;
00207                     cout << "  cartesianY = " << cartesianY[i] << endl;
00208                     cout << "  cartesianZ = " << cartesianZ[i] << endl;
00209                 }
00210                 totalRead += nRead;
00211             }
00212             reader.checkInvariant();
00213             destBuffers[0].checkInvariant();
00214             destBuffers[1].checkInvariant();
00215             destBuffers[2].checkInvariant();
00216 
00217             reader.close(); // don't forget to explicitly close the CompressedVectorReader
00218                 reader.checkInvariant();
00219         }
00220         imf.checkInvariant();
00221 
00222         imf.close(); // don't forget to explicitly close the ImageFile
00223             imf.checkInvariant();
00224 
00225         cout << "***** Got to end without any checkInvariant exceptions" << endl;
00226     } catch(E57Exception& ex) {
00227         ex.report(__FILE__, __LINE__, __FUNCTION__);
00228         return(-1);
00229     }
00230     return(0);
00231 }

This example program demonstrates the use of the checkInvariant function of various API classes. See the HelloWorld.cpp example for discussion of the use of include files, constructing an ImageFile, and the try/catch block to handle exceptions.

A class invariant is a collection of statements that are always true before and after a call to any member function of the class. The checkInvariant functions are diagnostics that check an object and its access functions for consistency. Each API call in this example, is followed (with an extra indentation) by checkInvariant calls to the object that was invoked and any objects used as arguments that might have been modified. This example illustrates that it can become tedious to manually check each object for consistency. A E57 Foundation Implementation may have a specially-built library version that can perform these checks on all objects used in API functions, before and after the call. It may be useful to selectively call checkInvariant explicitly from user code when debugging a particular problem.

The following console output is produced:

The XML section of the temp._e57 E57 file produced by this example program is as follows:

Here is the source code without line numbers to cut&paste from:

/*** CheckInvariant.cpp example: use checkInvariant function for ImageFile and other objects */
#include <iostream>
#include "E57Foundation.h"
using namespace e57;
using namespace std;

int main(int /*argc*/, char** /*argv*/) {
    try {
        ImageFile imf("temp._e57", "w");
            imf.checkInvariant();
        StructureNode root = imf.root();
            root.checkInvariant();
            imf.checkInvariant();
            

        // Add three elements: /child1, /child1/grandchild, /child2
        StructureNode child1  = StructureNode(imf);
            child1.checkInvariant();
            imf.checkInvariant();
        StructureNode child2  = StructureNode(imf);
            child2.checkInvariant();
            imf.checkInvariant();
        StringNode grandchild = StringNode(imf, "I'm a grandchild");
            grandchild.checkInvariant();
            imf.checkInvariant();
        root.set("child1", child1);
            root.checkInvariant();
            child1.checkInvariant();
        root.set("child2", child2);
            root.checkInvariant();
            child2.checkInvariant();
        child1.set("grandchild", grandchild);
            child1.checkInvariant();
            grandchild.checkInvariant();

        Node n = root.get("child2");
            n.checkInvariant();

        // Create vector /exampleVector, with 2 child elements
        VectorNode v(imf, false);
            v.checkInvariant();
            imf.checkInvariant();
        root.set("exampleVector", v);
            root.checkInvariant();
            v.checkInvariant();
        v.append(IntegerNode(imf, 100));
            v.checkInvariant();
            imf.checkInvariant();
        v.append(IntegerNode(imf, 101));
            v.checkInvariant();
            imf.checkInvariant();

        IntegerNode iNode(imf, 100);
            iNode.checkInvariant();
            imf.checkInvariant();
        root.set("exampleInteger", iNode);
            root.checkInvariant();
            iNode.checkInvariant();

        ScaledIntegerNode si(imf, 123, 0, 1023, .001, 100.0);
            si.checkInvariant();
            imf.checkInvariant();
        root.set("si1", si);
            root.checkInvariant();
            si.checkInvariant();

        FloatNode f(imf, 123.45F);
            f.checkInvariant();
            imf.checkInvariant();
        root.set("f1", f);
            root.checkInvariant();
            f.checkInvariant();

        StringNode s(imf, "a random string");
            s.checkInvariant();
            imf.checkInvariant();
        root.set("s1", s);
            root.checkInvariant();
            s.checkInvariant();

        const int blobLength = 10;
        static uint8_t buf[blobLength] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
        BlobNode b = BlobNode(imf, blobLength);
            b.checkInvariant();
            imf.checkInvariant();
        root.set("blob1", b);
            root.checkInvariant();
            b.checkInvariant();
        b.write(buf, 0, blobLength);
            b.checkInvariant();

        StructureNode prototype(imf);
            prototype.checkInvariant();
            imf.checkInvariant();
        prototype.set("cartesianX", FloatNode(imf));
            prototype.checkInvariant();
            imf.checkInvariant();
        prototype.set("cartesianY", FloatNode(imf));
            prototype.checkInvariant();
            imf.checkInvariant();
        prototype.set("cartesianZ", FloatNode(imf));
            prototype.checkInvariant();
            imf.checkInvariant();
        
        VectorNode codecs(imf, true);
            codecs.checkInvariant();
            imf.checkInvariant();

        CompressedVectorNode cv(imf, prototype, codecs);
            cv.checkInvariant();
            imf.checkInvariant();
            prototype.checkInvariant();
            codecs.checkInvariant();
        root.set("points", cv);
            root.checkInvariant();
            cv.checkInvariant();

        const int N = 4;
        static double cartesianX[N] = {1.0, 2.0, 3.0, 4.0};
        static double cartesianY[N] = {1.1, 2.1, 3.1, 4.1};
        static double cartesianZ[N] = {1.2, 2.2, 3.2, 4.2};
        vector<SourceDestBuffer> sourceBuffers;
        sourceBuffers.push_back(SourceDestBuffer(imf, "cartesianX",  cartesianX,  N));
            sourceBuffers[0].checkInvariant();
            imf.checkInvariant();
        sourceBuffers.push_back(SourceDestBuffer(imf, "cartesianY",  cartesianY,  N));
            sourceBuffers[1].checkInvariant();
            imf.checkInvariant();
        sourceBuffers.push_back(SourceDestBuffer(imf, "cartesianZ",  cartesianZ,  N));
            sourceBuffers[2].checkInvariant();
            imf.checkInvariant();
        {
            CompressedVectorWriter writer = cv.writer(sourceBuffers);
                writer.checkInvariant();
                sourceBuffers[0].checkInvariant();
                sourceBuffers[1].checkInvariant();
                sourceBuffers[2].checkInvariant();
            writer.write(N);
                writer.checkInvariant();
        }

        imf.extensionsAdd("ext1", "http://www.example.com/MyExtension1");
            imf.checkInvariant();
        root.set("ext1:greeting", StringNode(imf, "Hello world."));
            root.checkInvariant();
            imf.checkInvariant();

        imf.close(); // don't forget to explicitly close the ImageFile
            imf.checkInvariant();
    } catch(E57Exception& ex) {
        ex.report(__FILE__, __LINE__, __FUNCTION__);
        return(-1);
    }

    try {
        ImageFile imf("temp._e57", "r");
            imf.checkInvariant();
        StructureNode root = imf.root();
            root.checkInvariant();
            imf.checkInvariant();

        CompressedVectorNode cv = static_cast<CompressedVectorNode> (root.get("points"));
            cv.checkInvariant();
            root.checkInvariant();

        const int N = 10;
        static double cartesianX[N];
        static double cartesianY[N];
        static double cartesianZ[N];
        vector<SourceDestBuffer> destBuffers;
        destBuffers.push_back(SourceDestBuffer(imf, "cartesianX", cartesianX, N));
            destBuffers[0].checkInvariant();
            imf.checkInvariant();
        destBuffers.push_back(SourceDestBuffer(imf, "cartesianY", cartesianY, N));
            destBuffers[1].checkInvariant();
            imf.checkInvariant();
        destBuffers.push_back(SourceDestBuffer(imf, "cartesianZ", cartesianZ, N));
            destBuffers[2].checkInvariant();
            imf.checkInvariant();

        if (cv.prototype().type() != E57_STRUCTURE)
            return(-1);
            cv.checkInvariant();
        StructureNode prototype = static_cast<StructureNode>(cv.prototype());
            prototype.checkInvariant();
            cv.checkInvariant();
        {
            CompressedVectorReader reader = cv.reader(destBuffers);
                reader.checkInvariant();
                cv.checkInvariant();
            
            uint64_t totalRead = 0;
            unsigned nRead = 0;
            while ((nRead = reader.read(destBuffers)) > 0) {
                reader.checkInvariant();
                destBuffers[0].checkInvariant();
                destBuffers[1].checkInvariant();
                destBuffers[2].checkInvariant();

                cout << "Got " << nRead << " points" << endl;
                for (unsigned i = 0; i < nRead; i++) {
                    cout << "point " << totalRead + i << endl;
                    cout << "  cartesianX = " << cartesianX[i] << endl;
                    cout << "  cartesianY = " << cartesianY[i] << endl;
                    cout << "  cartesianZ = " << cartesianZ[i] << endl;
                }
                totalRead += nRead;
            }
            reader.checkInvariant();
            destBuffers[0].checkInvariant();
            destBuffers[1].checkInvariant();
            destBuffers[2].checkInvariant();

            reader.close(); // don't forget to explicitly close the CompressedVectorReader
                reader.checkInvariant();
        }
        imf.checkInvariant();

        imf.close(); // don't forget to explicitly close the ImageFile
            imf.checkInvariant();

        cout << "***** Got to end without any checkInvariant exceptions" << endl;
    } catch(E57Exception& ex) {
        ex.report(__FILE__, __LINE__, __FUNCTION__);
        return(-1);
    }
    return(0);
}
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines