rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage > Class Template Reference

#include <rulesmanager.h>

Inheritance diagram for rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >:
configuration::ModeController

List of all members.

Public Types

typedef IRuleMatcher< RuleType > RuleMatcherType
typedef RuleStorage::iterator iterator
typedef RuleStorage::const_iterator const_iterator
typedef RuleStorage::value_type MappingType

Public Member Functions

 RulesManager ()
 RulesManager (const RuleMatcherType *m)
 RulesManager (const RuleMatcherType *m, RuleStorage map)
virtual ~RulesManager ()
virtual const RuleMatcherTypesetRuleMatcher (const RuleMatcherType *newMatcher)
virtual void addRule (RuleType ruleDef, RuleTarget target)
template<typename Parser >
int loadFromFile (const std::string &confFile, Parser &parser)
virtual bool delRule (RuleType ruleDef, RuleTarget *target)
virtual void delMatching (const RuleType &ruleDef, RuleStorage *removed)
virtual void clear (RuleStorage *removed)
virtual bool findMatching (const RuleType &rule, RuleTarget *target) const

Private Member Functions

iterator find (RuleType &rule, iterator &start, iterator &end)

Private Attributes

RuleStorage mapping
const RuleMatcherTypematcher

Detailed Description

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
class rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >

Manager for rules.

Template class to maintain mapping from rules to rules targets (specified by template parameters RuleType and RuleTarget) and query for RuleTargets from given rules with most specific matching one.
RuleType template parameter specifies rule structure. It has to implement at least bool operator==(const RuleType &)const which returns true for two same rules (Note same are allways mathich but oposit implication doesn't have to be true).
RuleTarget template parameter specifies target of the rule. There are no constrains (except for correct allocation, deallocation and copying) on type. RulesManager user has to understand usage and meaning.
Uses IRuleMatcher implementation for specific rule decisions, defined in constructor. This can be changed in runtime to better fit different strathegies.
This class is responsible for correct rules storing and mapping to rule targets. All matching logic is delegated to the IRuleMatcher and just best candidate is returned. Mapping is stored in container defined as RuleStorage template paramter. This has to provide: forward iterators (both const and non const), clear method, push_back method, erase method.


Member Typedef Documentation

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
typedef RuleStorage::const_iterator rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::const_iterator

Alias for Rules storage constant iterator.

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
typedef RuleStorage::iterator rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::iterator

Alias for Rules storage iterator.

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
typedef RuleStorage::value_type rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::MappingType

Alias for Rules storage mapping entry.

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
typedef IRuleMatcher<RuleType> rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::RuleMatcherType

Alias for Rules matcher.


Constructor & Destructor Documentation

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::RulesManager (  )  [inline]

Empty constructor. Initializes matcher to NULL.

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::RulesManager ( const RuleMatcherType m  )  [inline]

Constructor.

Parameters:
m Implementation of rules matcher.

Sets matcher field and creates empty mapping.

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::RulesManager ( const RuleMatcherType m,
RuleStorage  map 
) [inline]

Constructor with initial mapping.

Parameters:
m Implementation of rules matcher.
map Initial rules mapping.

Sets matcher field and initializes mapping with given storage.

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
virtual rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::~RulesManager (  )  [inline, virtual]

Virtual destructor. This method is empty, because no special treatment is required.


Member Function Documentation

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
virtual void rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::addRule ( RuleType  ruleDef,
RuleTarget  target 
) [inline, virtual]

Adds given rule, target mapping.

Parameters:
ruleDef Rule definition.
target Target for given rule.

Inserts mapping with given ruleDef, target pair. Mapping is inserted even if ruleDef is already in. It depends on implementation of IRuleMatcher which one is used (by default one specified later because IRuleMatcher::betterMatch returns true for same priority by default).

Referenced by rulesmanager::RulesManager< std::string, std::string >::loadFromFile().

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
virtual void rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::clear ( RuleStorage *  removed  )  [inline, virtual]

Clears whole mapping.

Parameters:
removed Place where to store removed mapping (only if non NULL).

If removed parameter is non NULL, copies mapping content to it befor clearing (given container is cleared before).

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
virtual void rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::delMatching ( const RuleType &  ruleDef,
RuleStorage *  removed 
) [inline, virtual]

Removes all mappings matching given rule.

Parameters:
ruleDef Rule to match.
removed RuleStorage with all removed rule, target pairs (only if non NULL).

Goes through whole mapping and checks all rules whether they match given rule. If yes, removes them from mapping and if removed parameter is non NULL, inserts rule, target pair to it (container is cleared at the begining in any case).
If matcher is not specified (it is NULL), returns immediately after removed container is cleared.

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
virtual bool rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::delRule ( RuleType  ruleDef,
RuleTarget *  target 
) [inline, virtual]

Removes mapping for given ruleDef.

Parameters:
ruleDef Rule definition.
target Place where to place associated target.

First occurence in mapping is removed.
If mapping is found and target parameter is non NULL, rule target is set to place where it points.

Returns:
true if rule was removed, false otherwise.
template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
iterator rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::find ( RuleType &  rule,
iterator start,
iterator end 
) [inline, private]

Searches for given rule in [start, end) range.

Parameters:
rule Rule to search.
start Iterator to first element to search.
end Iterator behind last element to consider.

Checks for all elements in range [start, end) and compares it with given rule (using RuleType::== operator). If found, returns iterator.

Returns:
iterator to element which has same rule or end iterator if not found.

Referenced by rulesmanager::RulesManager< std::string, std::string >::delRule().

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
virtual bool rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::findMatching ( const RuleType &  rule,
RuleTarget *  target 
) const [inline, virtual]

Finds best fit mapping.

Parameters:
rule Rule to find.
target Place where to put target if rule matches (only if non NULL).

Goes through whole mapping and compares given rule with those from mapping using IRuleMatcher implementation. If multiple rules matches use one which matches the best according priority.
If at least one mapping matches and target parameter is non NULL, associated target is stored to place where it points.
if no matcher is specified (it is NULL), immediately returns with false.

Returns:
true if rule matches at least one from mapping, false otherwise.

Referenced by configuration::ModeController::getMode().

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
template<typename Parser >
int rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::loadFromFile ( const std::string &  confFile,
Parser &  parser 
) [inline]

Reads given configuration file.

Parameters:
confFile Configuration file name.
parser Parser to be used for file parsing.

Uses given parser to get key (RuleType) and value (RuleTarget) from given file. Given parser has to support given file format. Parsed rules are registered using addRule method.
Note that Parser template type has to provide correct types for key and value. IConfigurationParser descendant should be used.

Returns:
number of successfully added rules or -1 if error occured during parsing.

Referenced by gui::PropertyModeController::PropertyModeController().

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
virtual const RuleMatcherType* rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::setRuleMatcher ( const RuleMatcherType newMatcher  )  [inline, virtual]

Sets new rule matcher implementation.

Parameters:
newMatcher New matcher implementation.
Returns:
Old matcher implmentation.

Referenced by configuration::ModeController::ModeController().


Member Data Documentation

template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
RuleStorage rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::mapping [private]
template<typename RuleType, typename RuleTarget, typename RuleStorage = std::vector<std::pair<RuleType,RuleTarget> >>
const RuleMatcherType* rulesmanager::RulesManager< RuleType, RuleTarget, RuleStorage >::matcher [private]

IRuleMatcher implementator for rule matching.

First implementation is set in constructor and can be changed by setRuleMatcher method. If NULL is provided, findMatching always return false.

Reimplemented in configuration::ModeController.

Referenced by rulesmanager::RulesManager< std::string, std::string >::delMatching(), rulesmanager::RulesManager< std::string, std::string >::findMatching(), and rulesmanager::RulesManager< std::string, std::string >::setRuleMatcher().


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