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

example: creating IntegerNodes More...

Include dependency graph for IntegerCreate.cpp:

Functions

void printIntegerInfo (ImageFile imf, ustring pathName)
 Get and print some info about an IntegerNode.
int main (int, char **)
 Example use of IntegerNode functions.

Detailed Description

example: creating IntegerNodes

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

00001 /*** IntegerCreate.cpp example: creating IntegerNodes */
00004 #include <iostream>
00005 #include "E57Foundation.h"
00006 using namespace e57;
00007 using namespace std;
00008 
00010 void printIntegerInfo(ImageFile imf, ustring pathName)
00011 {
00012     cout << pathName << ":" << endl;
00013 
00014     StructureNode root = imf.root();
00015 
00016     if (root.isDefined(pathName)) {
00017         Node n = root.get(pathName);
00018         if (n.type() == E57_INTEGER) {
00019             IntegerNode iNode = static_cast<IntegerNode>(n);
00020             cout << "  value   = " << iNode.value()   << endl;
00021             cout << "  minimum = " << iNode.minimum() << endl;
00022             cout << "  maximum = " << iNode.maximum() << endl;
00023         } else
00024             cout << "oops " << n.pathName() << " isn't an Integer" << endl;
00025     }
00026 }
00027 
00029 int main(int /*argc*/, char** /*argv*/) {
00030     try {
00031         ImageFile imf("temp._e57", "w");
00032         StructureNode root = imf.root();
00033 
00034         // Create 4 example Integers 
00035         root.set("i1", IntegerNode(imf));
00036         root.set("i2", IntegerNode(imf, 123));
00037         root.set("i3", IntegerNode(imf, 123, 0, 1023));
00038         root.set("i4", IntegerNode(imf, 0, -128, 127));
00039 
00040         printIntegerInfo(imf, "/i1");
00041         printIntegerInfo(imf, "/i2");
00042         printIntegerInfo(imf, "/i3");
00043         printIntegerInfo(imf, "/i4");
00044 
00045         imf.close(); // don't forget to explicitly close the ImageFile
00046     } catch(E57Exception& ex) {
00047         ex.report(__FILE__, __LINE__, __FUNCTION__);
00048         return(-1);
00049     }
00050     return(0);
00051 }

This example program writes an ImageFile containing 4 IntegerNodes. It then prints out some basic information about the state of each IntegerNode. See the HelloWorld.cpp example for discussion of the use of include files, constructing an ImageFile, and the try/catch block to handle exceptions.

Source lines 35-38 construct the 4 IntegerNodes and attach them with distinct element names to the root node. In source line 35, the IntegerNode is constructed with a default value, default minimum, and default maximum. The defaults are specified in the E57Foundation.h include file. The default values create a zero value IntegerNode with minimum/maximum range set to the largest values that can be represented by a signed 64 bit number (which is the underlying representation in the reference implementation of the E57 Foundation API). Source line 36 attaches an IntegerNode with a given value but default min/max. Source line 37 creates a IntegerNode that can represent all values of an unsigned 10 bit number. Source line 38 creates a IntegerNode that can represent all values of an signed 8 bit number. The minimum/maximum specified don't have to be representable by an integral number of bits (e.g. minimum=1, maximum=100 is OK). In source lines 37-38, if the specified value had been chosen outside the given minimum/maximum bounds, an exception would have been thrown (since the ASTM spec requires the value be within the bounds).

Each IntegerNode is then fetched by absolute path name in source line 17. The call to StructureNode::get(const ustring&) const in source line 17 can throw an exception (E57_ERROR_PATH_UNDEFINED) if the path name is not defined. Source line 16 checks that the path name is actually defined before fetching the child node. The return type of the StructureNode::get(const ustring&) const function is a generic Node handle (because containers can hold any type of node, not just IntegerNode). A test on source line 18 makes sure we aren't surprised by an exception in source line 19, where the generic Node handle is downcast to a handle to IntegerNode. This downcast could have been specified in three other equivalent forms: "IntegerNode iNode(root.get(pathName));" or "IntegerNode iNode = IntegerNode(root.get(pathName));", "IntegerNode iNode = (IntegerNode)root.get(pathName);", resulting in exactly the same function call. The static_cast form used in the code makes it clear that a type conversion is being performed. It is not possible for the iNode variable to refer to any other type of node. If the downcast could not be performed, an exception would be thrown.

Once we have iNode handle in source line 19, we can then call IntegerNode specific functions (in source lines 20-22) to print out the state of the IntegerNode. The functions in source lines 20-22 can be called even though the ImageFile was opened in write mode. All read functions for objects stored in a write-mode ImageFile work correctly. The are some functions that are accessible with a generic Node handle (for example Node::pathName is called in source line 24). Note that in output listing lines 3-4 and 7-8 the minimum and maximum are very large numbers which are the defaults if they aren't explicitly specified. Also note that on XML line 4, the IntegerNode value, minimum, and maximum aren't given since the ASTM E57 standard specifies default values that match those specified in the API header file. Only in XML line 6 is it necessary to list all three numbers for the IntegerNode.

The following console output is produced: 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:

/*** IntegerCreate.cpp example: creating IntegerNodes */
#include <iostream>
#include "E57Foundation.h"
using namespace e57;
using namespace std;

void printIntegerInfo(ImageFile imf, ustring pathName)
{
    cout << pathName << ":" << endl;

    StructureNode root = imf.root();

    if (root.isDefined(pathName)) {
        Node n = root.get(pathName);
        if (n.type() == E57_INTEGER) {
            IntegerNode iNode = static_cast<IntegerNode>(n);
            cout << "  value   = " << iNode.value()   << endl;
            cout << "  minimum = " << iNode.minimum() << endl;
            cout << "  maximum = " << iNode.maximum() << endl;
        } else
            cout << "oops " << n.pathName() << " isn't an Integer" << endl;
    }
}

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

        // Create 4 example Integers 
        root.set("i1", IntegerNode(imf));
        root.set("i2", IntegerNode(imf, 123));
        root.set("i3", IntegerNode(imf, 123, 0, 1023));
        root.set("i4", IntegerNode(imf, 0, -128, 127));

        printIntegerInfo(imf, "/i1");
        printIntegerInfo(imf, "/i2");
        printIntegerInfo(imf, "/i3");
        printIntegerInfo(imf, "/i4");

        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