|
/* Gamelon(tm) File I/O Library Sample Code C++ Language */ /* (C)19941996 Menai Corporation(tm) Released for public use */
/* Example 11. How to create and use a Reference object */ /* 11a. Creating a Reference Object directly with an OID */
/* A Reference object may be created by supplying an * * Object Identifier (OID) when the Reference object is * * first written. In the following example, a few objects * * are written into a new file by using the Append() * * alteration from the PreFirst position. The first of * * these objects is given an OID and the last is a * * Reference Object that takes that OID as its value. * * Because Append() is used from the Prefirst position, * * the objects end up in reverse order so that the last * * written object (the Reference object) takes the first * * position within the file. * * (assume an empty file) */
#include <stdlib.h> #include "gfcursor.hpp"
int main(void) { int valid; CURSOR * c1 = new CURSOR("f1.g"); c1>eCursorQueryValid(&valid); if (!valid) exit(1); c1>eCursorMoveIn(); c1>eCursorMovePreFirst(); /* At this point, the programmer initiates an alteration and writes an object for each of the objects in the series. OIDs are artificially assumed to start with number 1. */ c1>eAltAppend(); c1>eObjApplyIdentity();/* oid == 1 */ c1>eObjWrite('a'); /* Append another and save the applied identity into oid */ unsigned long oid; c1>eAltAppend(); c1>eObjApplyIdentity(&oid);/* oid == 2 */ c1>eObjWrite("This is a string"); c1>eAltAppend(); c1>eObjApplyIdentity();/* oid == 3 */ c1>eObjWrite(123.456); /* Now create a reference to the second object */ c1>eAltAppend(); c1>eObjWriteReference(oid); /* The objects in the file are now a Reference object followed by other objects bearing OIDs 3, 2 and 1, respectively, and the Reference object holds the value of 2. Result (with object OIDs not shown, except as the value of the Reference object, and with OIDs shown as # notations above the objects which bear them):
{ #3 #2 #1 } [2][123.456]["This is a string"]['a'] */
/* 11b. Creating a Reference Object using a second Cursor */
/* A Reference object also may be created by supplying to * * the creating Cursor a second Cursor that has focus on * * an object that bears an OID. Assuming the file * * structure just created, this example continues with * * a second Cursor being created and moved onto the object * * that bears an OID of 3 (the numerical float object with * * a value of 123.456). Remember that the first Cursor is * * still in the PreFirst position. */
CURSOR * c2 = new CURSOR("f1.g"); c2>eCursorQueryValid(&valid); if (!valid) exit(1); c2>eCursorMoveIn(); /* c2 moves to the 'a' object with OID of 1 */ c1>eCursorMoveLast(); /* At this point, the programmer resumes use of Cursor c1, which is still in the Prefirst position, to write another Reference object using the Append() alteration. The new Reference object picks up the OID of the 'a' object, which is 1, because Cursor c2 is presented as an argument to Cursor c1 when c1 creates the Reference object. The new Reference object becomes the first object in the file. */ c1>eAltAppend(); c1>eObjWriteReference(c2); /* Result (with object OIDs not shown, except as the values of Reference objects, and with OIDs shown as # notations above the objects which bear them):
{ #3 #2 #1 } [1][2][123.456]["This is a string"]['a'] */
/* 11c. Moving the focus of a Cursor from a Reference object to the referenced object */
/* Now the programmer moves Cursor c1 off the PreFirst * * position and onto the first object, which is the * * Reference Object that holds the value of 1, and then * * jumps to the referenced object (the jump would fail if * * the object in focus were not a Reference object). * * Since the 'a' object bears OID 1, Cursor c1 ends up * * with focus on the 'a' object. */
c1>eCursorMoveFirst(); c1>eCursorJumpTo() /* As usual, however, our programmer cleans up properly to conclude the sequence. Note that the Cursors can be deleted regardless of their current positions in the file. */ delete c1; delete c2; return 0; } |
|||||||||||||
| |
|||||||||||||
|
Home | Product | Consulting | Programming | Reviews | Company | Site Map | Guest Book |
|||||||||||||
|
Menai Corporation, 1010 El Camino Real, Suite 300, Menlo Park, California 94025-4335 |
|||||||||||||
|
Copyright © 1996-98 Menai Corporation. All Rights Reserved. Menai, Gamelon and gamelon(stylized) are worldwide trademarks of Menai Corporation, registered in the United States of America, and the .[g] logo is a worldwide trademark of Menai Corporation. All other trademarks are owned by their respective owners. |
|||||||||||||