/** * @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 #include //! Activ namespace. namespace Activ { /** * @brief Do-nothing function object. */ class NullObject { public: template 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 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 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 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 class C, template class S> class PairComparisonObject { public: typedef P Pair; typedef S Selector; typedef C 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 class LessThanPairComparisonObject : public PairComparisonObject { }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Equal to pair first member comparison function object. */ template class EqualToPairComparisonObject : public PairComparisonObject { }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Case-insensitive less than string comparison object. */ template 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