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

example: creating StructureNodes More...

Include dependency graph for StructureCreate.cpp:

Functions

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

Detailed Description

example: creating StructureNodes

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

00001 /*** StructureCreate.cpp example: creating StructureNodes */
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         // Add three elements: /child1, /child1/grandchild, /child2
00016         StructureNode child1  = StructureNode(imf);
00017         StructureNode child2  = StructureNode(imf);
00018         StringNode grandchild = StringNode(imf, "I'm a grandchild");
00019         root.set("child1", child1);
00020         root.set("child2", child2);
00021         child1.set("grandchild", grandchild);
00022 
00023         cout << "root has " << root.childCount() << " children:" << endl;
00024         for (int i = 0; i < root.childCount(); i++) {
00025             Node n = root.get(i);
00026             if (n.type() == E57_STRUCTURE) {
00027                 StructureNode s = static_cast<StructureNode>(n);
00028                 cout << "  " << s.pathName() << " Structure with "
00029                      << s.childCount() << " child elements" << endl;
00030            } else
00031                 cout << "  " << n.pathName() << endl;
00032         }
00033 
00034         if (child1.isDefined("grandchild"))
00035             cout << "/child1 has grandchild element" << endl;
00036 
00037         if (!child1.isDefined("/child2/grandchild"))
00038             cout << "/child2 has no grandchild element" << endl;
00039 
00040         ustring absolutePathName = "/child1/grandchild";
00041         if (child1.isDefined(absolutePathName)) {
00042             Node n = root.get(absolutePathName);
00043             cout << n.pathName() << " exists" << endl;
00044         }
00045 
00046         imf.close(); // don't forget to explicitly close the ImageFile
00047     } catch(E57Exception& ex) {
00048         ex.report(__FILE__, __LINE__, __FUNCTION__);
00049         return(-1);
00050     }
00051     return(0);
00052 }
00053 

This example program writes an ImageFile containing two StructureNodes (/child1, and /child2) and a StringNode (/child1/grandchild). Some StructureNode functions then interrogate the resulting tree. See the HelloWorld.cpp example for discussion of the use of include files, constructing an ImageFile, and the try/catch block to handle exceptions.

Two StructureNodes are created in source lines 16-17, and are attached as children to the ImageFile root node in source lines 19-20. Handles are saved (in child1 and child2) so that they may be referred to later in the program. A StringNode (with handle grandchild) is created and attached underneath child1. Attaching grandchild to child1 before it was attached to the ImageFile root would have also been possible. The number of children of root is printed (2) in source line 23.

A for loop, in source lines 24-32, that iterates through the two children of root and prints out some information about each. Each child is fetched by index in source line 25. The StructureNode::get(int64_t) const function returns a generic Node handle whose type must be checked before it is downcast to StructureNode (also see discussion in the IntegerNode::IntegerNode example concerning downcasting).

Source line 34 illustrates a call to see if a structure contains an node using a relative pathname (relative to child1). In contrast, source line 37 illustrates a test of existance of an node using an absolute pathname (the path from the root of the tree that contains the child1 node). Source lines 41-42 show the testing of existance of an absolute pathName in a tree that is several levels deep Once the existence of the elements is established in source line 41, it may be safely fetched in source line 42.

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:

/*** StructureCreate.cpp example: creating StructureNodes */
#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();

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

        cout << "root has " << root.childCount() << " children:" << endl;
        for (int i = 0; i < root.childCount(); i++) {
            Node n = root.get(i);
            if (n.type() == E57_STRUCTURE) {
                StructureNode s = static_cast<StructureNode>(n);
                cout << "  " << s.pathName() << " Structure with "
                     << s.childCount() << " child elements" << endl;
           } else
                cout << "  " << n.pathName() << endl;
        }

        if (child1.isDefined("grandchild"))
            cout << "/child1 has grandchild element" << endl;

        if (!child1.isDefined("/child2/grandchild"))
            cout << "/child2 has no grandchild element" << endl;

        ustring absolutePathName = "/child1/grandchild";
        if (child1.isDefined(absolutePathName)) {
            Node n = root.get(absolutePathName);
            cout << n.pathName() << " exists" << 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