PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Sharing a generic sollution (lots of boost)
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Sharing a generic sollution (lots of boost)

Réponse
 
LinkBack Outils de la discussion
Vieux 18/10/2007, 18h51   #1 (permalink)
Noah Roberts
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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::string> const& 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::string> t;
std::vector<std::string> d;
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.
  Réponse avec citation
Vieux 18/10/2007, 19h03   #2 (permalink)
Noah Roberts
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sharing a generic sollution (lots of boost)

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".
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h27.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,07822 seconds with 10 queries