[Tuple] Help getting started with Tuple-based meta-programming
Hi All. I outline below code I keep writing which I'd like to replace
with a sql_get_rows template function which would use the meta-type
information bundled in the tuple declaration to automatically make as
many calls to the get<T> function based on the tuple length, and
"pack" the result of these calls into a tuple instance added to a
container (vector in this case, but if it could be generalized to any
container, all the better, although I'd be happy just with vector).
I have experience with basic template stuff, and a little less basic
stuff like CRTP, enable_if, and the like, but meta-programming is
still new to me, which is why I'm seeking advice from experts to find
out whether the below is : 1) possible, 2) would perform ok, and 3)
where I should start looking to see examples doing this particular
"tuple-enrolling" or perhaps even pseudo code outlining how one would
go about implementing this, provided answers to (1) and (2) are
positive of course. I suspect mpl and phoenix would need to be used,
from the reading I've done, but the path to follow is quite unclear to
me still. Any help in this matter would be appreciated.
Thanks, --DD
// Context: retrieving all the rows (result set) from a given SQL query.
// pseudo code of boilerplate code I keep repeating right now
struct { int id; std::string name; } Row;
void doit() {
ResultSet rset = sql_exec("select id, name from t");
std::vector<Row> rows;
while (rset.hasMoreRows()) {
Row row;
row.id = rset.get<int>(0);
row.name = rset.get
AMDG Dominique Devienne wrote:
// Context: retrieving all the rows (result set) from a given SQL query. // pseudo code of boilerplate code I keep repeating right now struct { int id; std::string name; } Row; void doit() { ResultSet rset = sql_exec("select id, name from t"); std::vector<Row> rows; while (rset.hasMoreRows()) { Row row; row.id = rset.get<int>(0); row.name = rset.get
(1); rows.push_back(row); } // process rows } // Code I'd like to write instead void doit() { typedef boost::tuple
Row; std::vector<Row> rows; sql_get_rows("select id, name from t", rows); // optional arg to get only first N elements // process rows } PS: returning the vector would be better, but because of the copy involved I guess I must pass it as a non-const ref instead, to fill it up. Will the new move semantic from the upcoming standard allow a function to return the vector without the copy? Just curious
In this case, I think Boost.Fusion can help (warning untested).
#include
On Fri, Mar 20, 2009 at 3:01 PM, Steven Watanabe
In this case, I think Boost.Fusion can help (warning untested).
#include
#include struct get_t { ResultSet& r; int& n; template<class T> void operator()(T& t) const { t = r.get<T>(n++); } };
template<Row> void get_rows(const char* query, std::vector<Row>& out) { ResultSet rset = sql_exec(query); while(rset.hasMoreRows()) { Row row; int n = 0; get_t getter = { rset, n }; boost::fusion::for_each(row, getter); out.push_back(row); } }
Simply amazing Steven :) Thanks a lot!
Here's the code I ended up with, which reveals more context.
Many thanks. I can't believe the quality and speed of your answer frankly.
You made me day. Sincerely, --DD
#include
participants (2)
-
Dominique Devienne
-
Steven Watanabe