Matching.h revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/** @file
*
* Declaration of template classes that provide simple API to
* do matching between values and value filters constructed from strings.
*/
/*
* Copyright (C) 2006 InnoTek Systemberatung GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License as published by the Free Software Foundation,
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
* distribution. VirtualBox OSE is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY of any kind.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
#ifndef ____H_MATCHING
#define ____H_MATCHING
#include <list>
#include <limits>
#include <algorithm>
// min and max don't allow us to use numeric_limits::min() and max()
#if defined (_MSC_VER)
#endif
{
{
/**
* Returns the error position from the beginning of the filter
* string if #isValid() is false. Positions are zero-based.
*/
/**
* Returns true if current isNull() and isValid() values make further
* detailed matching meaningful, otherwise returns false.
* Must be called as a first method of every isMatch() implementation,
* so that isMatch() will immediately return false if isPreMatch() retuns
* false.
*/
bool isPreMatch() const
{
return false;
return true;
}
bool mValid : 1;
bool mNull : 1;
};
{
union Widest
{
signed long long ll;
unsigned long long ull;
};
struct Limits
{
};
/**
* Called by #parse when a value token is encountered.
* This method can modify mNull, mValid and mErrorPosition when
* appropriate. Parsing stops if mValid is false after this method
* returns (mErrorPosition most point to the error position in this case).
*/
};
/**
* Represents a parsed interval filter.
* The string format is: "int:(<m>|([<m>]-[<n>]))|(<m>|([<m>]-[<n>]))+"
* where <m> and <n> are numbers in the decimal, hex (0xNNN) or octal (0NNN)
* form, and <m> < <n>. Spaces are allowed around <m> and <n>.
*
* @param T type of values to match. Must be a fundamental integer type.
*/
{
typedef ParsedIntervalFilter_base Base;
typedef numeric_limits <T> Lim;
ParsedIntervalFilter() {}
{
mIntervals.clear();
return *this;
}
{
if (!isPreMatch())
return false;
{
return true;
}
{
return true;
}
return false;
}
{
Limits()
{
{
}
else
{
}
}
{
else
}
};
{
(void) 0);
{
mValid = false;
return;
}
switch (aMode)
{
/// @todo (dmik): future optimizations:
// 1) join intervals when they overlap
// 2) ignore single values that are within any existing interval
{
{
// an empty string (contains only spaces after "int:")
mValid = false;
break;
}
break;
}
{
// aStart == aEnd means smth. like "-[NNN]"
break;
}
{
// aStart == aEnd means smth. like "[NNN]-"
{
// error at the beginning of N
mValid = false;
break;
}
break;
}
}
}
};
/**
* Represents a boolean filter.
* The string format is: "true|false|yes|no|1|0" or an empty string (any match).
*/
{
{
return *this;
}
{
if (!isPreMatch())
return false;
}
{
}
bool mValue : 1;
bool mValueAny : 1;
};
{
ParsedRegexpFilter_base (bool aDefIgnoreCase = false,
{}
{
}
{
return *this;
}
bool mDefIgnoreCase : 1;
bool mIgnoreCase : 1;
};
/**
* Represents a parsed regexp filter.
* The string format is: "rx:<regexp>" or "<string>"
* where <regexp> is a valid regexp and <string> is the exact match.
*
* @param Conv
* class that must define a public static function
* <tt>Bstr toBstr (T aValue)</tt>, where T is the
* type of values that should be accepted by #isMatch().
* This function is used to get the string representation of T
* for regexp matching.
* @param aIgnoreCase
* true if the case insensitive comparison should be done by default
* and false otherwise
* @param aMinLen
* minimum string length, or 0 if not limited.
* Used only when the filter string represents the exact match.
* @param aMaxLen
* maximum string length, or 0 if not limited.
* Used only when the filter string represents the exact match.
*/
{
{
return *this;
}
{
if (!this->isPreMatch())
return false;
}
};
/**
* Joins two filters into one.
* Only one filter is active (i.e. used for matching or for error reporting)
* at any given time. The active filter is chosen every time when a new
* filter string is assigned to an instance of this class -- the filter
* for which isNull() = false after parsing the string becomes the active
* one (F1 is tried first).
*
* Both filters must have <tt>bool isMatch (const T&)</tt>
* methods where T is the same type as used in #isMatch().
*
* @param F1 first filter class
* @param F2 second filter class
*/
{
TwoParsedFilters() {}
{
}
{
else
return *this;
}
{
}
size_t errorPosition() const
{
}
};
/**
* Inherits from the given parsed filter class and keeps the string used to
* construct the filter as a member.
*
* @param F parsed filter class
*/
{
Matchable() {}
/**
* Creates a new parsed filter from the given filter string.
* If the string format is invalid, #isValid() will return false.
*/
/**
* Assings a new filter string to this object and recreates the parser.
* If the string format is invalid, #isValid() will return false.
*/
{
return *this;
}
{
return *this;
}
/**
* Returns the filter string allowing to use the instance where
* Str can be used.
*/
/** Returns the filter string */
};
}
#endif // ____H_MATCHING