GlsMenu
GlsBoolExpression Class Reference

#include <gls_bool_expression.h>

Public Member Functions

 GlsBoolExpression (GlsMenuDictionary &evalDict, bool defaultVal=false)
 
virtual ~GlsBoolExpression ()
 
bool operator() () const
 
virtual void Set (const std::string &val)
 
GlsBoolExpressionoperator= (const GlsBoolExpression &r)
 
bool operator== (const GlsBoolExpression &r) const
 
std::string ExpressionString () const
 

Protected Types

enum  Operator_t {
  OR,
  AND,
  EQ,
  NEQ,
  LT,
  LT_EQ,
  GT,
  GT_EQ,
  NOT,
  OPEN_PAREN,
  CLOSE_PAREN,
  MAX_OPERATORS
}
 
typedef std::vector< std::string > TokenCont_t
 
typedef std::map< std::string, Operator_tOperatorMap_t
 

Protected Member Functions

virtual std::string Evaluate (Operator_t op, const std::string &operand1, const std::string &operand2) const
 
virtual bool IsTrue (const std::string &word) const
 

Static Protected Member Functions

static bool IsAnOperator (const std::string &token)
 
static TokenCont_t InfixToRPN (const TokenCont_t &infixTokens)
 
static void Tokenize (const std::string &expr, TokenCont_t &tokens)
 
static std::string::size_type VarLength (const std::string &input, std::string::size_type start)
 
static std::string Uppercase (const std::string &str)
 
static bool ValidInfix (const TokenCont_t &infixTokens)
 

Protected Attributes

GlsMenuDictionary_evalDict
 The dictionary to use to evaluate meta-data variables found in the expression. More...
 
std::string _expressionString
 Storage of the original string for the expression. More...
 
bool _latestValue
 The latest calculated result of the expression. More...
 
TokenCont_t _RPNTokens
 These are the tokens that were extracted from the expression string and are stored in Reverse Polish Notation (RPN) order. More...
 

Static Protected Attributes

static bool _initializedStaticData
 Whether or not the static class data has been initialized. This needs to be done only once by the first instance. More...
 
static OperatorMap_t OP
 A map to convert from operator strings to their enum equivalent. More...
 
static const unsigned PRECEDENCE [MAX_OPERATORS]
 Operator precedence values used when evaluating the expression. More...
 

Detailed Description

The GlsBoolExpression provides a string-based boolean expression parser and evaluator for use with simulation meta-data variables. The expressions can be read in from an input stream so that boolean simulation meta-data can be set as a boolean expression rather than just literal boolean values (true and false).

Variables in the expression are also meta-data items from an associated evaluation data dictionary. Variables should be enclosed in the variable delimiters $( and ), eg Literals and variable comparisons will always be done on a string basis, so care must be taken to achieve the desired results. This could be improved later if the need ever arises. So far, string comparisons have been sufficient.

One limitation is that an expression must be contained all on a single line. There is currently no support for a multiline expression. This can be changed by modifying the stream extraction operator.

Definition at line 78 of file gls_bool_expression.h.

Member Typedef Documentation

typedef std::map<std::string, Operator_t> GlsBoolExpression::OperatorMap_t
protected

Definition at line 169 of file gls_bool_expression.h.

typedef std::vector<std::string> GlsBoolExpression::TokenCont_t
protected

Definition at line 168 of file gls_bool_expression.h.

Member Enumeration Documentation

Enumerator
OR 
AND 
EQ 
NEQ 
LT 
LT_EQ 
GT 
GT_EQ 
NOT 
OPEN_PAREN 
CLOSE_PAREN 
MAX_OPERATORS 

Definition at line 151 of file gls_bool_expression.h.

Constructor & Destructor Documentation

GlsBoolExpression::GlsBoolExpression ( GlsMenuDictionary evalDict,
bool  defaultVal = false 
)

Class constructor.

Parameters
evalDictdata dictionary used for evaluating values of variables used within the expression.
defaultValvalue to initialize the expression with.
virtual GlsBoolExpression::~GlsBoolExpression ( )
inlinevirtual

Class destructor.

Definition at line 96 of file gls_bool_expression.h.

Member Function Documentation

virtual std::string GlsBoolExpression::Evaluate ( Operator_t  op,
const std::string &  operand1,
const std::string &  operand2 
) const
protectedvirtual

Evaluates the operation.

std::string GlsBoolExpression::ExpressionString ( ) const
inline

Returns the uppercase version of a string.

Definition at line 145 of file gls_bool_expression.h.

References _expressionString.

Referenced by operator<<().

static TokenCont_t GlsBoolExpression::InfixToRPN ( const TokenCont_t infixTokens)
staticprotected

Converts the tokens into Reverse Polish Notation (RPN) order from infix. This makes evaluating the expression trivial.

static bool GlsBoolExpression::IsAnOperator ( const std::string &  token)
inlinestaticprotected

Returns true if the token is a logical operator.

Definition at line 216 of file gls_bool_expression.h.

virtual bool GlsBoolExpression::IsTrue ( const std::string &  word) const
protectedvirtual

Determines if the string represents a boolean value of true.

bool GlsBoolExpression::operator() ( ) const

This operator evaluates the boolean expression and returns true or false.

GlsBoolExpression& GlsBoolExpression::operator= ( const GlsBoolExpression r)
inline

Assignment operator.

Definition at line 118 of file gls_bool_expression.h.

References _expressionString, _latestValue, and _RPNTokens.

bool GlsBoolExpression::operator== ( const GlsBoolExpression r) const
inline

Equality operator.

Definition at line 135 of file gls_bool_expression.h.

References _latestValue.

virtual void GlsBoolExpression::Set ( const std::string &  val)
virtual

Sets the boolean expression. This changes the entire expression.

Referenced by operator>>().

static void GlsBoolExpression::Tokenize ( const std::string &  expr,
TokenCont_t tokens 
)
staticprotected

Parses the input string expression and adds found tokens to the token container.

static std::string GlsBoolExpression::Uppercase ( const std::string &  str)
staticprotected

Returns the uppercase version of a string.

static bool GlsBoolExpression::ValidInfix ( const TokenCont_t infixTokens)
staticprotected

Determines if the infix expression is syntactically correct.

static std::string::size_type GlsBoolExpression::VarLength ( const std::string &  input,
std::string::size_type  start 
)
staticprotected

Determines the length of the variable starting at start position in the input string.

Field Documentation

GlsMenuDictionary& GlsBoolExpression::_evalDict
protected

The dictionary to use to evaluate meta-data variables found in the expression.

Definition at line 183 of file gls_bool_expression.h.

std::string GlsBoolExpression::_expressionString
protected

Storage of the original string for the expression.

Definition at line 186 of file gls_bool_expression.h.

Referenced by ExpressionString(), and operator=().

bool GlsBoolExpression::_initializedStaticData
staticprotected

Whether or not the static class data has been initialized. This needs to be done only once by the first instance.

Definition at line 173 of file gls_bool_expression.h.

bool GlsBoolExpression::_latestValue
mutableprotected

The latest calculated result of the expression.

Definition at line 189 of file gls_bool_expression.h.

Referenced by operator=(), and operator==().

TokenCont_t GlsBoolExpression::_RPNTokens
protected

These are the tokens that were extracted from the expression string and are stored in Reverse Polish Notation (RPN) order.

Definition at line 193 of file gls_bool_expression.h.

Referenced by operator=().

OperatorMap_t GlsBoolExpression::OP
staticprotected

A map to convert from operator strings to their enum equivalent.

Definition at line 176 of file gls_bool_expression.h.

const unsigned GlsBoolExpression::PRECEDENCE[MAX_OPERATORS]
staticprotected

Operator precedence values used when evaluating the expression.

Definition at line 179 of file gls_bool_expression.h.


The documentation for this class was generated from the following file: