Files
externals/linux/include/ActivMiddleware/Misc/FunctionObjects.h
T

424 lines
13 KiB
C++

/**
* @file include/ActivMiddleware/Misc/FunctionObjects.h
*
* @brief Header file for function objects.
*/
#if (!defined ACTIV_MISC_FUNCTION_OBJECTS_H)
#define ACTIV_MISC_FUNCTION_OBJECTS_H
#include "ActivMiddleware/Misc/StlHelper.h"
#include "ActivMiddleware/CxxYZ/type_traits.h"
#include ACTIV_BOOST_INCLUDE(algorithm/string/predicate.hpp)
#include <algorithm>
#include <functional>
//! Activ namespace.
namespace Activ
{
/**
* @brief Do-nothing function object.
*/
class NullObject
{
public:
template<typename T>
void operator()(const T *ptr) const
{
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Delete object.
*
* Can be used in conjunction with for_each() to destroy a sequence container of pointers, for instance.
*/
class DeleteObject
{
public:
/**
* @brief Function operator.
*
* @param ptr a pointer to the object to delete.
*/
template<typename T>
void operator()(const T *ptr) const
{
delete ptr;
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Delete object array.
*
* Can be used in conjunction with for_each() to destroy a sequence container of array pointers, for instance.
*/
class DeleteObjectArray
{
public:
/**
* @brief Function operator.
*
* @param ptr a pointer to the object array to delete.
*/
template<typename T>
void operator()(const T *ptr) const
{
delete [] ptr;
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Delete associative object.
*
* Can be used in conjunction with for_each() to destroy an associative container of pointers, for instance.
*/
class DeleteAssociativeObject
{
public:
/**
* @brief Function operator.
*
* @param pair a reference to the object to delete.
*/
template<typename Pair>
void operator()(const Pair &pair) const
{
delete pair.second;
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Pair member comparison function object.
*
* P the pair type.
* C the comparison to perform (std::less, std::equal_to, etc.).
* S function object class to select first or second member (SelectFirst, SelectSecond).
*/
template <typename P, template<typename> class C, template<typename> class S>
class PairComparisonObject
{
public:
typedef P Pair;
typedef S<Pair> Selector;
typedef C<typename Selector::result_type> Comparator;
typedef P first_argument_type;
typedef P second_argument_type;
typedef bool result_type;
/**
* @brief Function operator.
*
* @param lhs a reference to the lhs object.
* @param rhs a reference to the rhs object.
*
* @return Whether Comparator condition is true.
*/
bool operator()(const Pair &lhs, const Pair &rhs) const
{
return Comparator()(Selector()(lhs), Selector()(rhs));
}
/**
* @brief Function operator.
*
* @param lhs a reference to the lhs object.
* @param rhs a reference to the rhs object.
*
* @return Whether Comparator condition is true.
*/
bool operator()(const Pair &lhs, const typename Selector::result_type &rhs) const
{
return Comparator()(Selector()(lhs), rhs);
}
/**
* @brief Function operator.
*
* @param lhs a reference to the lhs object.
* @param rhs a reference to the rhs object.
*
* @return Whether Comparator condition is true.
*/
bool operator()(const typename Selector::result_type &lhs, const Pair &rhs) const
{
return Comparator()(lhs, Selector()(rhs));
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Less than pair first member comparison function object.
*/
template <typename Pair>
class LessThanPairComparisonObject : public PairComparisonObject<Pair, std::less, SelectFirst>
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Equal to pair first member comparison function object.
*/
template <typename Pair>
class EqualToPairComparisonObject : public PairComparisonObject<Pair, std::equal_to, SelectFirst>
{
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Case-insensitive less than string comparison object.
*/
template<typename T = String>
class CaseInsensitiveLessThanStringComparisonObject
{
public:
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
/**
* @brief Function operator.
*
* @param lhs a reference to the lhs object.
* @param rhs a reference to the rhs object.
*
* @return whether the lhs object is less than rhs object.
*/
bool operator()(const T &lhs, const T &rhs) const
{
return Boost::ilexicographical_compare(lhs, rhs);
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Case insensitive string equality comparison functor.
*/
class CaseInsensitiveStringEqualComparisonObject
{
public:
typedef String first_argument_type;
typedef String second_argument_type;
typedef bool result_type;
/**
* @brief Function operator.
*
* @param lhs a reference to the lhs object.
* @param rhs a reference to the rhs object.
*
* @return whether the lhs object is equivalent to the rhs object (ignoring case).
*/
bool operator()(const String &lhs, const String &rhs) const
{
return Boost::iequals(lhs, rhs);
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief NaturalOrder relational function object.
*
* This function object compares strings naturally (ie. "abc10" being greater than "abc9") using another relational
* function object template, the default being std::less.
*/
template <template <typename Type> class Comparator = std::less>
class NaturalOrderComparisonObject
{
public:
/**
* @brief Default constructor.
*/
NaturalOrderComparisonObject()
{
// Here to remove warnings with SunPro.
}
/**
* @brief operator() compares two strings to determine natural order.
*
* @param leftString the left hand string for comparison.
* @param rightString the right hand string for comparison.
*
* @return whether the strings satisfied the Comparator predicate.
*/
bool operator()(const String &leftString, const String &rightString) const
{
String::const_iterator leftIter = leftString.begin(), rightIter = rightString.begin();
const String::const_iterator leftEndIter = leftString.end(), rightEndIter = rightString.end();
for (; ((leftEndIter != leftIter) && (rightEndIter != rightIter)); ++leftIter, ++rightIter)
{
// Iterate over the chars of the two strings, if they are not both digits do a char comparison.
if ((0 != ::isdigit(*leftIter)) && (0 != ::isdigit(*rightIter)))
{
uint64_t leftUintValue(0), rightUintValue(0);
const String::const_iterator leftPreDigitIter = leftIter, rightPreDigitIter = rightIter;
bool isLeftUintValid = ProcessDigits(leftIter, leftEndIter, leftUintValue);
bool isRightUintValid = ProcessDigits(rightIter, rightEndIter, rightUintValue);
if (isLeftUintValid && isRightUintValid)
{
if (Comparator<uint64_t>()(leftUintValue, rightUintValue))
return true;
if (Comparator<uint64_t>()(rightUintValue, leftUintValue))
return false;
}
else
{
typedef String::difference_type IterDifferenceType;
IterDifferenceType leftDigitCount(leftIter - leftPreDigitIter), rightDigitCount(rightIter - rightPreDigitIter);
// Pad both strings with 0's to the size of the larger string.
String leftDigitString(std::max<IterDifferenceType>(rightDigitCount - leftDigitCount, 0), '0');
String rightDigitString(std::max<IterDifferenceType>(leftDigitCount - rightDigitCount, 0), '0');
(void) leftDigitString.append(leftIter - leftDigitCount, leftIter);
(void) rightDigitString.append(rightIter - rightDigitCount, rightIter);
// Perform a string comparison.
if (Comparator<String>()(leftDigitString, rightDigitString))
return true;
if (Comparator<String>()(rightDigitString, leftDigitString))
return false;
}
}
// Compare the next chars if the end of the string has not been reached.
if ((leftEndIter == leftIter) || (rightEndIter == rightIter))
break;
if (Comparator<char>()(*leftIter, *rightIter))
return true;
if (Comparator<char>()(*rightIter, *leftIter))
return false;
}
// If iteration is not complete for either string, compare the number of remaining chars.
if ((leftEndIter != leftIter) || (rightEndIter != rightIter))
return Comparator<int64_t>()(leftEndIter - leftIter, rightEndIter - rightIter);
return false;
}
private:
static bool ProcessDigits(String::const_iterator &charIter, const String::const_iterator &endIter,
uint64_t &uintValue)
{
bool isValidInt = true;
// Iterate over the current consecutive digits forming a uint64_t.
for (size_t digitCount = 0; (endIter != charIter) && (0 != ::isdigit(*charIter)); ++charIter, ++digitCount)
{
// If the uint64_t will not be overflowed incorporate the current char digit.
if ((digitCount >= static_cast<size_t>(Boost::integer_traits<uint64_t>::digits10)) &&
(uintValue > ((Boost::integer_traits<uint64_t>::const_max - static_cast<uint64_t>(*charIter - '0')) / 10)))
{
isValidInt = false;
}
else
{
uintValue = (uintValue * 10) + (static_cast<uint64_t>(*charIter - '0'));
}
}
return isValidInt;
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Meta-function to convert a pointer type to a const pointer type (e.g. int * -> const int *)
*/
template <typename T>
struct ConstPointer
{
ACTIV_BOOST_STATIC_ASSERT_MSG(CxxYZ::is_pointer<T>::value, "T must be a pointer type");
// Remove the pointer, add const, re-add the pointer.
typedef typename CxxYZ::add_pointer<typename CxxYZ::add_const<typename CxxYZ::remove_pointer<T>::type>::type>::type Type;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Convert a smart pointer to a raw pointer.
*/
struct SmartPtrToRawPtr
{
template<typename F>
struct result;
// Required for compatability with Boost::result_of
template<typename F, typename T>
struct result<F(T)>
{
typedef typename CxxYZ::remove_reference<T>::type U;
typedef typename U::pointer type;
};
template <typename T>
inline typename result<SmartPtrToRawPtr(T)>::type operator()(const T &ptr) const ACTIV_BOOST_NOEXCEPT_IF(ACTIV_BOOST_NOEXCEPT_EXPR(ptr.get()))
{
return ptr.get();
}
};
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Convert a smart pointer to a raw const pointer.
*/
struct SmartPtrToConstRawPtr
{
template<typename F>
struct result;
// Required for compatability with Boost::result_of
template<typename F, typename T>
struct result<F(T)>
{
typedef typename CxxYZ::remove_reference<T>::type U;
typedef typename ConstPointer<typename U::pointer>::Type type;
};
template <typename T>
inline typename result<SmartPtrToConstRawPtr(T)>::type operator()(const T &ptr) const ACTIV_BOOST_NOEXCEPT_IF(ACTIV_BOOST_NOEXCEPT_EXPR(ptr.get()))
{
return ptr.get();
}
};
} // namespace Activ
#endif // (!defined ACTIV_MISC_FUNCTION_OBJECTS_H)