NVarSet - Set of Named Variables

The NVarSet class provides a "lite" implementation of EPOCH's nvarset (named variable set) class. A named variable set is essentially an associative array of named variables.

An empty set of named variables is created as follows:

    #include  "NVarSet.h"			// Named variable sets.
    NVarSet  set ;

Named variables are then added to the set using the Add() method:

    set.Add (new NVar (123.45, "THIS")) ;	// Add real number.
    set.Add (new NVar (6789, "THAT")) ;		// Add integer.

(The variables themselves, not copies, are added to the set.) The variables in a set can be retrieved by index:

    #include  <cstdio>			// Standard I/O definitions.
    int  i ;
    NVar  *variable ;
    for (i = 0 ;  i < set.Count () ;  i++) {
        variable = set.Get (i) ;
        printf ("%s = %s\n", variable->Name (), variable->String ()) ;

or by name:

    variable = set.Find ("THAT") ;		// Prints "THAT = 6789".
    printf ("%s = %s\n", variable->Name (), variable->String ()) ;

Individual variables can be deleted from a set:

    variable = set.Delete ("THIS") ;

Although the variable is removed from the set, the variable itself is not destroyed; the caller is then responsible for deleting the variable. In contrast, deleting the entire set in one fell swoop (as happens when set in the examples above goes out of scope) automatically destroys the remaining variables in the set.

Public Methods

NVarSet() - creates an empty named variable set.
NVarSet() - copy constructor.
~NVarSet() - destroys a named variable set.
=() - assignment operator.
Add() - adds a named variable to a set.
Count() - returns the number of named variables in a set.
Delete() - deletes a named variable from a set.
Find() - retrieves a variable by name from a set.
Get() - retrieves a variable by index from a set.
Name() - gets a set's name.
Name() - sets a set's name.

Private Methods

Copy() - copies the contents of one set to another.
Erase() - erases the contents of a set.

Source Files


Alex Measday  /  E-mail