Skip to content

Instantly share code, notes, and snippets.

@kotoji
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kotoji/af5485cef6e98466328e to your computer and use it in GitHub Desktop.
Save kotoji/af5485cef6e98466328e to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
#include <type_traits>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/apply.hpp>
namespace sample {
namespace util {
namespace detail {
namespace mpl = boost::mpl;
template <typename Cond, typename Seq, std::size_t I>
struct any_impl {
static constexpr bool value = mpl::apply<Cond, typename std::tuple_element<I, Seq>::type>::type::value
&& any_impl<Cond, Seq, I - 1>::value;
};
template <typename Cond, typename Seq>
struct any_impl<Cond, Seq, 0> {
static constexpr bool value = mpl::apply<Cond, typename std::tuple_element<0, Seq>::type>::type::value;
};
} // namespace detail
template <typename Cond, typename... Types>
struct any_ : detail::any_impl<Cond, std::tuple<Types...> , sizeof...(Types) - 1> {};
} // namespace util
template <typename T,
typename U = decltype(std::declval<T>().x),
typename U1 = decltype(std::declval<T>().y),
std::enable_if<util::any_<std::is_floating_point<boost::mpl::_1> ,U, U1>::value
&& std::is_default_constructible<T>::value
>* = nullptr
>
struct point_traits {
static U x(const T& p) { return p.x; }
static U y(const T& p) { return p.y; }
static T sub(const T& a, const T& b) {
T t;
t.x = a.x - b.x;
t.y = a.y - b.y;
return t;
}
};
template <typename T>
struct rectangle_traits {
using point_type = typename T::point_type;
static const point_type& p1(const T& rect) {
return rect.p1;
}
static const point_type& p2(const T& rect) {
return rect.p2;
}
static const point_type& p3(const T& rect) {
return rect.p3;
}
static const point_type& p4(const T& rect) {
return rect.p4;
}
};
template <typename Point>
double distance(Point a, Point b) {
using traits = point_traits<Point>;
const Point d = traits::sub(a, b);
return std::sqrt(traits::x(d) * traits::x(d) + traits::y(d) * traits::y(d));
}
template <typename Rect>
bool is_intersect(Rect a, Rect b) {
using traits = rectangle_traits<Rect>;
using p_traits = point_traits<typename traits::point_type>;
if (p_traits(traits(a).p1).x) {
}
}
} // namespace sample
struct Point {
double x;
double y_;
};
struct iPoint {
int x;
int y;
};
int main() {
std::cout << "Point: " << std::is_default_constructible<Point>::value << std::endl;
Point a = {0.0, 0.0};
Point b = {3.0, 3.0};
double d = sample::distance(a, b);
std::cout << d << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment