At Perconaâ€™s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.
Become a Premium Member and unlock a new, free course in leading technologies each month.
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
#include <list>
#include <algorithm>
template<class TYPE>
class List
{
public:
template<typename PARAMETER>
class FindFunctor
{
public:
FindFunctor( PARAMETER p1 ) : m_value( p1 ){}
virtual bool operator()( TYPE& t1 ) = 0;
protected:
PARAMETER m_value;
};
public:
List(void){};
virtual ~List(void){};
inline bool IsEmpty(void) { return m_list.empty(); }
template <typename PARAMETER>
bool FindElement( FindFunctor<PARAMETER>, typename std::list<TYPE>::iterator pos )
{
pos = std::find_if( m_list.begin(), m_list.end(), FindFunctor<PARAMETER>() );
return true; // for the sake of degugging
}
private:
std::list<TYPE> m_list;
};
struct SomeObject{ int GetCount() const {return 0;} };
int main()
{
List<SomeObject> myList;
typedef typename std::list<SomeObject>::iterator ListIndex;
class DerivedFindFunctor : public List<SomeObject>::FindFunctor<int>
{
public:
DerivedFindFunctor( int p1 ) : List<SomeObject>::FindFunctor<int>( p1 ){}
bool operator()( SomeObject& item ) { return item.GetCount() == m_value; }
};
}
#include <list>
#include <algorithm>
template<class TYPE>
class List
{
public:
typedef typename std::list<TYPE>::iterator iterator;
typedef typename std::list<TYPE>::const_iterator const_iterator;
public:
inline bool IsEmpty() const { return m_list.empty(); }
template <typename Predicate>
bool FindElement( Predicate pred, iterator & pos )
{
pos = std::find_if( m_list.begin(), m_list.end(), pred );
return pos != m_list.end();
}
template <typename Predicate>
bool FindElement( Predicate pred, const_iterator & pos ) const
{
pos = std::find_if( m_list.begin(), m_list.end(), pred );
return pos != m_list.end();
}
private:
std::list<TYPE> m_list;
};
struct SomeObject
{
int GetCount() const {return 0;}
};
class FindFunctor
{
public:
FindFunctor( int p1 ) : m_value(p1) {}
bool operator()( SomeObject const & item ) const
{
return item.GetCount() == m_value;
}
private:
int m_value;
};
int main()
{
List<SomeObject> myList;
List<SomeObject>::iterator pos;
bool OK = myList.FindElement( FindFunctor( 10 ), pos );
}
#include <list>
#include <algorithm>
template<class TYPE>
class List
{
public:
typedef typename std::list<TYPE>::iterator iterator;
typedef typename std::list<TYPE>::const_iterator const_iterator;
public:
inline bool IsEmpty() const { return m_list.empty(); }
template <typename Predicate>
bool FindElement( Predicate pred, iterator & pos )
{
pos = std::find_if( m_list.begin(), m_list.end(), pred );
return pos != m_list.end();
}
template <typename Predicate>
bool FindElement( Predicate pred, const_iterator & pos ) const
{
pos = std::find_if( m_list.begin(), m_list.end(), pred );
return pos != m_list.end();
}
private:
std::list<TYPE> m_list;
};
struct SomeObject
{
int GetCount() const {return 0;}
};
template <typename T> // <--- it's templated :)
class FindFunctor
{
public:
FindFunctor( T const & p1 ) : m_value(p1) {}
template <typename U>
bool operator()(U const & item ) const
{
return item.GetCount() == m_value;
}
private:
T m_value;
};
int main()
{
List<SomeObject> myList;
List<SomeObject>::iterator pos;
bool OK = myList.FindElement( FindFunctor<int>( 10 ), pos );
}
#include <list>
#include <algorithm>
#include <functional> // for bind1st and equal
using namespace std;
template<class TYPE>
class List
{
public:
typedef typename std::list<TYPE>::iterator iterator;
typedef typename std::list<TYPE>::const_iterator const_iterator;
public:
inline bool IsEmpty() const { return m_list.empty(); }
template <typename Predicate>
bool FindElement(Predicate pred, iterator & pos )
{
pos = std::find_if( m_list.begin(), m_list.end(), pred );
return pos != m_list.end();
}
template <typename Predicate>
bool FindElement(Predicate pred, const_iterator & pos ) const
{
pos = std::find_if( m_list.begin(), m_list.end(), pred );
return pos != m_list.end();
}
private:
std::list<TYPE> m_list;
};
struct SomeObject
{
int GetCount() const {return 0;}
};
template <
typename T, // this is the value type
template <typename> class P, // this is a template template type
typename U // this is the class type we are supporting
>
class FindFunctor
{
public:
typedef const_mem_fun_ref_t<T, U> func_t;
FindFunctor(
T const & p1, // the value we wish to match
func_t const & func // a functor that will get value from item
)
: m_value(p1)
, m_func(func) {}
bool operator()(U const & item ) const
{
return P<T>()( // create a predicate and call it
m_func(item), // call the member function on item to get value
m_value); // and compare it with my value
}
private:
T m_value; // the value to match
func_t m_func; // the function to get that value
};
int main()
{
List<SomeObject> myList;
List<SomeObject>::iterator pos;
FindFunctor<
int, // value type
equal_to, // predicate type
SomeObject // object type we're getting value from
>
pred(10, mem_fun_ref(&SomeObject::GetCount));
myList.FindElement(pred, pos);
}
#include <list>
#include <algorithm>
#include <functional> // for bind1st and equal
#include <boost/bind.hpp>
using namespace std;
template<class TYPE>
class List
{
public:
typedef typename std::list<TYPE>::iterator iterator;
typedef typename std::list<TYPE>::const_iterator const_iterator;
public:
inline bool IsEmpty() const { return m_list.empty(); }
template <typename Predicate>
bool FindElement(Predicate pred, iterator & pos )
{
pos = std::find_if( m_list.begin(), m_list.end(), pred );
return pos != m_list.end();
}
template <typename Predicate>
bool FindElement(Predicate pred, const_iterator & pos ) const
{
pos = std::find_if( m_list.begin(), m_list.end(), pred );
return pos != m_list.end();
}
private:
std::list<TYPE> m_list;
};
struct SomeObject
{
int GetCount() const {return 0;}
};
int main()
{
List<SomeObject> myList;
List<SomeObject>::iterator pos;
myList.FindElement(boost::bind(equal_to<int>(), 10,
bind(&SomeObject::GetCount, _1)), pos);
}
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.