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

example: get info about VectorNodes More...

Include dependency graph for VectorFunctions.cpp:

Functions

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

Detailed Description

example: get info about VectorNodes

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

00001 /*** VectorFunctions.cpp example: get info about VectorNodes */
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         StructureNode root = imf.root();
00014 
00015         // Create vector /exampleVector, with 2 child elements
00016         VectorNode v(imf, false);
00017         root.set("exampleVector", v);
00018         v.append(IntegerNode(imf, 100));
00019         v.append(IntegerNode(imf, 101));
00020 
00021         cout << v.pathName() << " has " << v.childCount() << " children:" << endl;
00022         for (int i = 0; i < v.childCount(); i++) {
00023             Node n = v.get(i);
00024             if (n.type() == E57_INTEGER) {
00025                 IntegerNode iNode = static_cast<IntegerNode>(n);
00026                 cout << "  " << iNode.pathName() << " IntegerNode = " << iNode.value() << endl;
00027            } else
00028                 cout << "  " << n.pathName() << endl;
00029         }
00030 
00031         if (v.isDefined("1"))
00032             cout << v.pathName() << " has a second child element" << endl;
00033         if (!v.isDefined("2"))
00034             cout << v.pathName() << " doesn't have a third child element" << endl;
00035 
00036         Node n = root.get("/exampleVector");
00037         if (n.type() == E57_VECTOR) {
00038             VectorNode v2 = static_cast<VectorNode>(n);
00039             cout << v2.pathName() << " has " << v2.childCount() << " children" << endl;
00040         }
00041 
00042         imf.close(); // don't forget to explicitly close the ImageFile
00043     } catch(E57Exception& ex) {
00044         ex.report(__FILE__, __LINE__, __FUNCTION__);
00045         return(-1);
00046     }
00047     return(0);
00048 }

This example program writes an ImageFile containing a VectorNode that contains two IntegerNodes. The VectorNode is interrogated is several ways. See the HelloWorld.cpp example for discussion of the use of include files, constructing an ImageFile, and the try/catch block to handle exceptions.

In source lines 16-19 a homogeneous VectorNode (can only contain children with identical types) is created, attached to the root of the ImageFile, and two IntegerNodes are appended. The number of children is printed out in source line 21. In source lines 22-29, a for loop iterates over the children, downcasting them to IntegerNode handles, and then prints out some information about each. Source lines 31 and 33 check to see whether a particular child is defined, but using a ustring element name rather than an integer index. Source lines 37-40 illustrates the downcasting of the VectorNode itself.

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:

/*** VectorFunctions.cpp example: get info about VectorNodes */
#include <iostream>
#include "E57Foundation.h"
using namespace e57;
using namespace std;

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

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

        cout << v.pathName() << " has " << v.childCount() << " children:" << endl;
        for (int i = 0; i < v.childCount(); i++) {
            Node n = v.get(i);
            if (n.type() == E57_INTEGER) {
                IntegerNode iNode = static_cast<IntegerNode>(n);
                cout << "  " << iNode.pathName() << " IntegerNode = " << iNode.value() << endl;
           } else
                cout << "  " << n.pathName() << endl;
        }

        if (v.isDefined("1"))
            cout << v.pathName() << " has a second child element" << endl;
        if (!v.isDefined("2"))
            cout << v.pathName() << " doesn't have a third child element" << endl;

        Node n = root.get("/exampleVector");
        if (n.type() == E57_VECTOR) {
            VectorNode v2 = static_cast<VectorNode>(n);
            cout << v2.pathName() << " has " << v2.childCount() << " children" << endl;
        }

        imf.close(); // don't forget to explicitly close the ImageFile
    } catch(E57Exception& ex) {
        ex.report(__FILE__, __LINE__, __FUNCTION__);
        return(-1);
    }
    return(0);
}
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines