Sign in to follow this  
Dragon_Strike

compare all constants in mpl::vector against runtime value

Recommended Posts

ive got a boost::mpl::vector_c with some values... id like to compare all these constants against a runtime value right now ive got it like this.. if( msg == at_c<F::type,0>::type::value) where F is a vector_c however id like a way to compare against all constants in the vector_c and only then have it return true if all comparisons return true...... EDIT:: id also like a fallback... if F is NOT a vector_c but an UINT id like it to be replaced with if( msg == F) guess id have to use someting like enable_if... but im unsure how i would do that EDIT2:: this is what ive tried... but it doesnt work since i cant use runtime values... if(remove_if< F::type, equal_to<_,int_<msg /*ERROR runtimevalue*/> > >::type::empty::type) [Edited by - Dragon_Strike on March 22, 2008 7:48:00 AM]

Share this post


Link to post
Share on other sites

#include <boost/fusion/algorithm/query/all.hpp>
#include <boost/fusion/adapted/mpl.hpp>

template<typename Value>
struct compare_value
{
Value value;

compare_value(Value value) : value(value) { }

template<typename T>
bool operator()(T t) { return t == value; }
};

template<typename Vec, typename Value>
bool compare(Value value)
{
return boost::fusion::all(Vec(), compare_value<Value>(value));
}

int main()
{
typedef boost::mpl::vector_c<int, -1, 5, 2, 10> vec;
std::cout << compare<vec>(10) << '\n'
<< compare<vec>(3) << '\n';
}


To impliment the fall back you could have two compare functions, one which with boost::enable_if< boost::fusion::is_sequence<Vec> > to handle the boost::mpl::vector_c's and one with boost::disable_if< boost::fusion::is_sequence<Vec> > to handle to uint's.

Share this post


Link to post
Share on other sites
You can do it like this:

#include <iostream>

#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/for_each.hpp>

typedef boost::mpl::vector_c<int, 1, 2, 3, 2, 3, 2> various;
typedef boost::mpl::vector_c<int, 1, 1, 1, 1, 1, 1> all_ones;


// Functor that is passed each value in the sequence
// This value is compared to a stored value and the result
// of that comparison is AND-ed with an external boolean
struct compare
{
compare(bool& result, int value) : result_(result), value_(value) {}

template <typename T>
void operator()(T other_value) const
{
result_ = result_ && (value_ == other_value);
}

private:
int value_;
bool& result_;
};

template <typename ListT>
bool compare_to(int value)
{
bool result = true;

boost::mpl::for_each<ListT>(compare(result, value));

return result;
}

int main(int)
{
using namespace std;

cout << compare_to<various >(1) << endl; // '0' or false
cout << compare_to<all_ones>(2) << endl; // '0' or false
cout << compare_to<all_ones>(1) << endl; // '1' or true

return 0;
}



I just saw another reply as well using boost::fusion. Oh well...

Share this post


Link to post
Share on other sites
why doesnt this work?



void Run(const boost::any& sender, base::EventArgs* e, typename boost::enable_if<boost::mpl::is_sequence<F>>::type* p = 0)
{
UINT msg = static_cast<EventArgs*>(e)->message;
if( compare_to<F>(msg) )
{
BOOST_FOREACH(base::EventHandlerPtr f, handlers_)
{
(*f)(sender, e);
}
}
}

void Run(const boost::any& sender, base::EventArgs* e, typename boost::disable_if<boost::mpl::is_sequence<F>>::type* p = 0)
{
UINT msg = static_cast<EventArgs*>(e)->message;
if( F == msg )
{
BOOST_FOREACH(base::EventHandlerPtr f, handlers_)
{
(*f)(sender, e);
}
}
}



Error 1 error C2039: 'type' : is not a member of 'boost::disable_if<Cond>' p:\programmering\projects\drone\drone\drone\Win32Event.hpp 84 Drone
Error 2 error C2061: syntax error : identifier 'type' p:\programmering\projects\drone\drone\drone\Win32Event.hpp 84 Drone

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this