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

example: creating VectorNodes More...

Include dependency graph for VectorCreate.cpp:

Functions

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

Detailed Description

example: creating VectorNodes

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

00001 /*** VectorCreate.cpp example: creating 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 /v1, with 2 child elements: /v1/0, /v1/1
00016         VectorNode v1(imf, false);
00017         root.set("v1", v1);
00018         v1.append(StringNode(imf, "some string"));
00019         v1.append(StringNode(imf, "another string"));
00020 
00021         // Create vector /v2, with 2 child elements: /v2/0, /v2/1
00022         VectorNode v2(imf, true);
00023         root.set("v2", v2);
00024         v2.append(StringNode(imf, "yet another string"));
00025         v2.append(FloatNode(imf, 1.234));
00026         
00027         if (!v1.allowHeteroChildren())
00028             cout << "/v1 cannot contain children with different types" << endl;
00029         if (v2.allowHeteroChildren())
00030             cout << "/v2 can contain children with different types" << endl;
00031 
00032         imf.close(); // don't forget to explicitly close the ImageFile
00033     } catch(E57Exception& ex) {
00034         ex.report(__FILE__, __LINE__, __FUNCTION__);
00035         return(-1);
00036     }
00037     return(0);
00038 }

This example program writes an ImageFile containing two VectorNodes, one VectorNode must contain children of the same type, and the second VectorNode can contain children of differing types. 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 16-17 create a homogeneous VectorNode (can only contain children of the same type) and attaches it the ImageFile root node. In source line 18 the first StringNode is added to the VectorNode by appending to the end. Since the VectorNode was initially empty, the first string is put in the 0 child index position. After the first child is appended to the VectorNode, any further children appended must be the same type as the first. The second append, on source line 19, is in fact the same type, and it is put in the 1 child index position.

Source lines 22-23 create a heterogeneous VectorNode and attach it to the ImageFile root node. Source lines 24-25 show that it is not an error to append child nodes of different types to the heterogeneous VectorNode. Source lines 27-30 show that the heterogeneous attribute can be interrogated.

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:

/*** VectorCreate.cpp example: creating 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 /v1, with 2 child elements: /v1/0, /v1/1
        VectorNode v1(imf, false);
        root.set("v1", v1);
        v1.append(StringNode(imf, "some string"));
        v1.append(StringNode(imf, "another string"));

        // Create vector /v2, with 2 child elements: /v2/0, /v2/1
        VectorNode v2(imf, true);
        root.set("v2", v2);
        v2.append(StringNode(imf, "yet another string"));
        v2.append(FloatNode(imf, 1.234));
        
        if (!v1.allowHeteroChildren())
            cout << "/v1 cannot contain children with different types" << endl;
        if (v2.allowHeteroChildren())
            cout << "/v2 can contain children with different types" << 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