Skip to content

Instantly share code, notes, and snippets.

@pfultz2
Created July 17, 2014 15:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfultz2/cfc447c7ccfa8ac2e76f to your computer and use it in GitHub Desktop.
Save pfultz2/cfc447c7ccfa8ac2e76f to your computer and use it in GitHub Desktop.
template<class T>
struct lazy_value
{
T x;
lazy_value(T x) : x(x)
{}
std::string to_string() const
{
return boost::lexical_cast<std::string(x);
}
};
template<class Left, class Right>
struct lazy_equal
{
Left left;
Right right;
std::string to_string() const
{
return left.to_string() + " = " + right.to_string();
}
};
template<class T, int N>
struct lazy_field
{
std::string to_string() const
{
return struct_member_name<T, N>::call();
}
template<class X>
friend lazy_equal<lazy_field<T, N>, lazy_value<X>>
operator==(const lazy_field<T, N>&self, const X& x)
{
return {self, lazy_value<X>(x)};
}
template<class X>
friend lazy_equal<lazy_value<X>, lazy_field<T, N>>
operator==(const lazy_field<T, N>&self, const X& x)
{
return {lazy_value<X>(x), self};
}
};
template<class Struct>
struct generate_table_map
{
struct make_pair
{
template<class I>
struct apply
{
typedef fusion::pair<
struct_assoc_key<Struct, I::value>,
lazy_field<Struct, I::value>>
type;
};
};
typedef mpl::range_c<int, 0, result_of::size<Struct>::type::value> range;
typedef mpl::transform_view<range, make_pair> pairs;
typedef typename result_of::as_map<pairs>::type type;
};
template<class Struct>
struct table : generate_table_map<Struct>::type
{
template<class Expression>
std::vector<Struct> where(const Expression& exp) const
{
// You still need to map the type names, I just use typeid for
// illustration
std::string query = std::string("select * from ") +
typeid(Struct).name() +
" where " +
exp.to_string();
// Then query database and return the vector
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment