Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | Related Pages

GraceTMPL::Environment Class Reference

Hierarchical environment system. More...

#include <gracetmpl.h>

List of all members.

Public Member Functions

 Environment (Environment *parent=0)
 Default constructor.

void clear ()
 Clears all variables - not the contexts.

void setParent (Environment *parent)
 Set the parent Environment.

void setName (const std::string &name)
 Set the context name.

void set (const std::string &name, const std::string &value)
 Set an environment variable.

void add (const std::string &name, Environment *value)
 set a named context - ensures consistency

Environmentuse ()
 do we really need this?

int unuse ()
 do we really need this?

std::string expand (const std::string &, int nests=20)
 Expands a string containing variables.

std::string substitute (const std::string &context, const std::string &variable, const std::string &fallback)
 Substitutes a variable honouring its context.


Detailed Description

Hierarchical environment system.

A (named) Environment holds a number of variablename/value pairs. The environment's name can be seen as a context. As such, it can be contained in other Environment objects as a contextname/Environment pair.


Member Function Documentation

std::string GraceTMPL::Environment::expand const std::string &  ,
int  nests = 20
 

Expands a string containing variables.

Searches for syntactically correct variables in the provided string and substitutes them using substitute(context,variable,fallback).

Valid variable syntax is either:

  • $variable or ${variable} within the current context
  • ${::variable} or ${std::variable} for the topmost context
  • ${context::variable} for any context - if the context is not known to the current context it is passed to the parent context, and so on. The 'context' can also be written as 'context1::context2::context3[..]' to address nested contexts
  • In case a variable cannot be expanded according to the above methods substitution will not take place and the (unresolvable) variable specification will be included in the destination string. This kind of 'fallback' can be changed by appending ':fallback' to 'variable' (note this only works when the ${} format is used), i.e. ${context::variable:fallback} or ${variable:fallback}
  • If a variable cannot be resolved in the current context it is always passed to the parent context. This behaviour might be configurable in the (near or distant) future.

Nested variables are allowed: ${variable_${number}} will be expanded the following way: first, ${number} is expanded, say to 001. So, we get ${variable_001} as the result. Now, if nests is larger than 0, this method will recurse into itself (up to nests times) and expand ${variable_001} to whatever value $variable_001 yields.

std::string GraceTMPL::Environment::substitute const std::string &  context,
const std::string &  variable,
const std::string &  fallback
 

Substitutes a variable honouring its context.

This method also uses parent->substitute(context,variable,fallback) or context_[context]->substitute(context2,variable,fallback) for expansion, if this should be necessary.


The documentation for this class was generated from the following file:
Generated on Mon Sep 27 10:56:44 2004 for GraceTMPL by doxygen 1.3.2