Created
October 29, 2013 15:58
-
-
Save erikerlandson/7217401 to your computer and use it in GitHub Desktop.
Test code for introspection and concept checking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******* | |
edit_distance: STL and Boost compatible edit distance functions for C++ | |
Copyright (c) 2013 Erik Erlandson | |
Author: Erik Erlandson <[email protected]> | |
Distributed under the Boost Software License, Version 1.0. | |
See accompanying file LICENSE or copy at | |
http://www.boost.org/LICENSE_1_0.txt | |
*******/ | |
#include <iostream> | |
// get the edit_distance() function | |
#include <boost/algorithm/sequence_alignment/edit_distance.hpp> | |
using boost::algorithm::sequence_alignment::edit_distance; | |
#include <boost/function_types/result_type.hpp> | |
#include <boost/function_types/function_arity.hpp> | |
#include <boost/function_types/parameter_types.hpp> | |
#include <boost/function_types/is_member_function_pointer.hpp> | |
#include <boost/function_types/is_member_object_pointer.hpp> | |
#include <boost/type_traits.hpp> | |
#include <boost/function.hpp> | |
#include <boost/typeof/std/utility.hpp> | |
#include <boost/mpl/has_xxx.hpp> | |
#include <boost/mpl/at.hpp> | |
#include <boost/mpl/if.hpp> | |
#include <boost/concept/requires.hpp> | |
#include <boost/mpl/assert.hpp> | |
BOOST_MPL_HAS_XXX_TRAIT_DEF(cost_type) | |
template <typename Z, bool F> struct ct { | |
typedef typename boost::function_types::result_type< BOOST_TYPEOF(&Z::m2) >::type type; | |
}; | |
template <typename Z> struct ct<Z, true> { | |
typedef typename Z::cost_type type; | |
}; | |
template <typename X> | |
struct get_cost_type { | |
typedef typename ct<X, has_cost_type<X>::value >::type type; | |
}; | |
template <typename X> | |
struct TTT { | |
BOOST_MPL_ASSERT((boost::function_types::is_member_function_pointer<BOOST_TYPEOF(&X::m1)>)); | |
BOOST_MPL_ASSERT((boost::function_types::is_member_function_pointer<BOOST_TYPEOF(&X::m2)>)); | |
BOOST_MPL_ASSERT((boost::function_types::is_member_function_pointer<BOOST_TYPEOF(&X::m3)>)); | |
typedef BOOST_TYPEOF(&X::m2) m2_type; | |
//typedef typename boost::function_types::result_type<m2_type>::type cost_type; | |
typedef typename get_cost_type<X>::type cost_type; | |
typedef typename boost::mpl::at_c<typename boost::function_types::parameter_types<m2_type>::type, 1>::type value_type; | |
//BOOST_CONCEPT_ASSERT((Arithmetic<cost_type>)); | |
BOOST_CONCEPT_USAGE(TTT) { | |
c = x.m2(v); | |
} | |
X x; | |
cost_type c; | |
value_type v; | |
}; | |
struct ttt { | |
//bool m1() { return false; } | |
int m2(int v) { return 17+v; } | |
bool m3(int v1, float v2) { return true; } | |
}; | |
template <typename T> | |
BOOST_CONCEPT_REQUIRES( | |
((TTT<T>)), | |
(int)) | |
fff(T t) { | |
return t.m2(0); | |
} | |
int main(int argc, char** argv) { | |
std::cout << fff(ttt()) << "\n"; | |
std::cout << typeid(typename get_cost_type<ttt>::type).name() << "\n"; | |
typedef BOOST_TYPEOF(&ttt::m3) m3_type; | |
// arity | |
std::cout << boost::function_types::function_arity<m3_type>::value << "\n"; | |
// arguments (arg 0 is 'this' for member funcs) | |
std::cout << typeid(boost::mpl::at_c<boost::function_types::parameter_types<m3_type>::type, 0>::type).name() << "\n"; | |
std::cout << typeid(boost::mpl::at_c<boost::function_types::parameter_types<m3_type>::type, 1>::type).name() << "\n"; | |
std::cout << typeid(boost::mpl::at_c<boost::function_types::parameter_types<m3_type>::type, 2>::type).name() << "\n"; | |
// return type | |
std::cout << typeid(boost::function_types::result_type<m3_type>::type).name() << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment