473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Sharing a generic sollution (lots of boost)

A while back I posted a question either here or to the boost user list
about how to iterate through a vector of strings and perform a lexical
cast on them into elements of a tuple. I was working with sqlite3 and
found myself repeatedly writing code like so:

t.get<0>() = boost::lexical_cast<double>(vect[0]);
t.get<1>() = boost::lexical_cast<int>(vect[1]);
....

For each query I would invent. Certainly there seemed there should be a
way to use metaprogramming to approach this problem. I could get here:

t.get<N>() = boost::lexical_cast< boost::tuples::element<N, TUP>::type
>(vect[N]);
However, I couldn't figure how to insert that into a metaprogram. I
figured it would have something to do with for_each but couldn't figure
it out. When I approached whatever list I posted my question to I was
sent to some library...I forget which one but it wasn't the answer.
Well, last night a light in my brain turned on. It's actually very
simple once it dawns on one how to do it:

template < typename TUPLE >
struct tuple_assign
{
TUPLE & t;
std::vector< std::string const& data;

tuple_assign(TUPLE & to, std::vector<std::stringconst& from) :
t(to), data(from) {}

template < typename T>
void operator() (T) // T must be an mpl::int_
{
boost::tuples::get<T::value>(t) =
boost::lexical_cast< boost::tuples::element<T::value,
TUPLE>::type>(data[T::value]);
}
};

Your calling code looks like so:

boost::tuple<double, int, std::stringt;
std::vector<std::stringd;
d += "5.2","42","HELLO!";

boost::mpl::for_each< boost::mpl::range<0,3(tuple_assign<
boost::tuple<double,int,std::string(t,d));

The range can also be derived through the template system like so:

boost::mpl::range< 0,
boost::tuples::length<boost::tuple<double,int,std: :string

Much safety can be placed on this system. I haven't done so here. This
problem solved though, there's nothing stopping a generic query
interface that could be used something like so:

tie(x, y, z) = query.run();

as well as an iterative interface that provides a similar tuple interface.
Oct 18 '07 #1
1 1661
Noah Roberts wrote:
template < typename T>
void operator() (T) // T must be an mpl::int_
{
boost::tuples::get<T::value>(t) =
boost::lexical_cast< boost::tuples::element<T::value,
TUPLE>::type>(data[T::value]);
}
In my shortcut writing I neglected "typename". The above call to the
element metafunction must be preceded by "typename".
Oct 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: gong | last post by:
hi i recently looked at alexandrescu's book on c++, and i found it pretty much unintelligible. i have a few points which i wonder about. 1. as a developer, it is important, from a bottom line...
0
by: Simon Elliott | last post by:
I have a class which uses a singleton to output progress messages for logging or display. It's typically used deep within a hierarchy of aggregations, inheritances and containers. The singleton...
15
by: Matthias Kaeppler | last post by:
Hi, as a result of my last posting about an adaption class template for invoking algorithms on a container of iterators or pointers to elements in another container, I have come up with this...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
7
by: Kamran | last post by:
Hi, I have hundreds of variables inside a class (integer, float) and a long range of set/get functions to manipulate their values. What I am wondering about is whether there is a way in C++ to...
0
by: Emily | last post by:
Imagine a world where everybody shares and has faith in each other. We have all struggled at one time or another with our jobs or careers and have wondered if there was a better way to make a...
3
by: Frederick Gotham | last post by:
For objects, we have "void*" as the generic pointer type. For instance: enum ParamType { Int, Double }; void Func(void *const p,ParamType const pt) { switch (pt) { case Int: *(int*)p = 42;...
1
by: Bit Byte | last post by:
I was thinking of writing a generic container (i.e. collection) class in C++. It would have iterators etc to allow traversing and acessing the dat items. The only problem arose when I tried to...
1
by: David Belohrad | last post by:
Dear All, could someone give a hint? I'd like to share the resources as follows: A shared class which works as counter of number of shared resources: class Shared { public: Q_PCBShared()...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.