boost fusion and/or boost parameter: open ended question about constructing object from a string
hi, here's what i'd like to do A] hpp: struct my{ template<typename ArgPack> my(const ArgPack& args):n_(args[tag::n]),k_(args[tag::k]){} } struct argpack{ arglist<???> operator()(const string& str){???} } cpp: string str("n=2; k=1"); my(argpack(str)); In words,I'm looking to construct an ArgPack from a string, what's the appropriate library for this? Does it seem like I should invest in Boost Spirit? B] The above is a simplified version of what i really need, which is, potentially, 1) having to construct a large number of objects (to be pushed at the back of a container) 2) each object taking a large number arguments (not just n and k as above) 3) each object an instance of one of a set of classes (my0,my1,my2,...,myN) each with its specific argument tags (keywords). So I'd like to put all these arguments in a file, each line corresponding to an object, rather than have a messy cpp file. this is all very open-ended, but so reads the subject title... thanks!
on Wed Sep 10 2008, e r
hi,
here's what i'd like to do
A]
hpp:
struct my{ template<typename ArgPack> my(const ArgPack& args):n_(args[tag::n]),k_(args[tag::k]){} }
struct argpack{ arglist<???> operator()(const string& str){???} }
cpp:
string str("n=2; k=1");
my(argpack(str));
In words,I'm looking to construct an ArgPack from a string, what's the appropriate library for this?
Well, unless the types of all your arguments are the same *and* you have a limited set of parameter names, all known at compile time, Boost.Parameter can not help at all... and even if you meet all those conditions, using Boost.Parameter would probably be more trouble than it is worth. The problem is that Boost.Parameter establishes an association between keywords and argument values at compile time, and your strings have to be parsed at runtime.
Does it seem like I should invest in Boost Spirit?
Almost any of Boost's text processing libraries might be useful to you. In your case, it might be easiest to build something on Boost.Regex or Boost.XPressive, or even tokenizer, since the job you're doing is so simple.
The above is a simplified version of what i really need, which is, potentially, 1) having to construct a large number of objects (to be pushed at the back of a container) 2) each object taking a large number arguments (not just n and k as above) 3) each object an instance of one of a set of classes (my0,my1,my2,...,myN) each with its specific argument tags (keywords).
So I'd like to put all these arguments in a file, each line corresponding to an object, rather than have a messy cpp file.
Well, if you're willing to compile that file as C++, you could go back to using Boost.Parameter... ;-) -- Dave Abrahams BoostPro Computing http://www.boostpro.com
David Abrahams wrote:
on Wed Sep 10 2008, e r
wrote: hi,
here's what i'd like to do
A]
hpp:
struct my{ template<typename ArgPack> my(const ArgPack& args):n_(args[tag::n]),k_(args[tag::k]){} }
struct argpack{ arglist<???> operator()(const string& str){???} }
cpp:
string str("n=2; k=1");
my(argpack(str));
In words,I'm looking to construct an ArgPack from a string, what's the appropriate library for this?
Well, unless the types of all your arguments are the same *and* you have a limited set of parameter names, all known at compile time, Boost.Parameter can not help at all... and even if you meet all those
Thanks. - Actually, no the arguments are not all the same type. - A limited set of parameter names: limited as in small or known? Known, but not necessarily small.
conditions, using Boost.Parameter would probably be more trouble than it is worth. The problem is that Boost.Parameter establishes an association between keywords and argument values at compile time, and your strings have to be parsed at runtime.
Quite a painful dilemma because I use Boost.Parameter (not unlike many folks I guess) to pass down arguments into nested objects (whose types may be parameterized in the host class) and I couldn't deal with positional arguments.
Does it seem like I should invest in Boost Spirit?
Almost any of Boost's text processing libraries might be useful to you. In your case, it might be easiest to build something on Boost.Regex or Boost.XPressive, or even tokenizer, since the job you're doing is so simple.
The above is a simplified version of what i really need, which is, potentially, 1) having to construct a large number of objects (to be pushed at the back of a container) 2) each object taking a large number arguments (not just n and k as above) 3) each object an instance of one of a set of classes (my0,my1,my2,...,myN) each with its specific argument tags (keywords).
So I'd like to put all these arguments in a file, each line corresponding to an object, rather than have a messy cpp file.
Well, if you're willing to compile that file as C++, you could go back to using Boost.Parameter... ;-)
since the arguments to be passed into object constructors are known at compile time, i guess i could still do this? a) have some sort of cpp template i.e. some, not all the code intentionally left out (say marked by "NA"), to be filled from a text file, via a text processor b) compile the modified cpp file. i'm not all that experienced with parsers and such. is this where Boost.Xpressive etc fit in or should i just go ahead and use perl?
e r wrote:
David Abrahams wrote:
on Wed Sep 10 2008, e r
wrote: hi,
here's what i'd like to do
A]
hpp:
struct my{ template<typename ArgPack> my(const ArgPack& args):n_(args[tag::n]),k_(args[tag::k]){} }
struct argpack{ arglist<???> operator()(const string& str){???} }
cpp:
string str("n=2; k=1");
my(argpack(str));
In words,I'm looking to construct an ArgPack from a string, what's the appropriate library for this? Well, unless the types of all your arguments are the same *and* you have a limited set of parameter names, all known at compile time, Boost.Parameter can not help at all... and even if you meet all those
Thanks.
- Actually, no the arguments are not all the same type. - A limited set of parameter names: limited as in small or known? Known, but not necessarily small.
conditions, using Boost.Parameter would probably be more trouble than it is worth. The problem is that Boost.Parameter establishes an association between keywords and argument values at compile time, and your strings have to be parsed at runtime.
Quite a painful dilemma because I use Boost.Parameter (not unlike many folks I guess) to pass down arguments into nested objects (whose types may be parameterized in the host class) and I couldn't deal with positional arguments.
Does it seem like I should invest in Boost Spirit? Almost any of Boost's text processing libraries might be useful to you. In your case, it might be easiest to build something on Boost.Regex or Boost.XPressive, or even tokenizer, since the job you're doing is so simple.
The above is a simplified version of what i really need, which is, potentially, 1) having to construct a large number of objects (to be pushed at the back of a container) 2) each object taking a large number arguments (not just n and k as above) 3) each object an instance of one of a set of classes (my0,my1,my2,...,myN) each with its specific argument tags (keywords).
So I'd like to put all these arguments in a file, each line corresponding to an object, rather than have a messy cpp file. Well, if you're willing to compile that file as C++, you could go back to using Boost.Parameter... ;-)
since the arguments to be passed into object constructors are known at compile time, i guess i could still do this?
a) have some sort of cpp template i.e. some, not all the code intentionally left out (say marked by "NA"), to be filled from a text file, via a text processor b) compile the modified cpp file.
i'm not all that experienced with parsers and such. is this where Boost.Xpressive etc fit in or should i just go ahead and use perl?
PS: I've just noticed there was no need for mentioning boost::fusion in the title. Totally out of context (I guess got confused with another post I wrote).
on Wed Sep 10 2008, e r
So I'd like to put all these arguments in a file, each line corresponding to an object, rather than have a messy cpp file.
Well, if you're willing to compile that file as C++, you could go back to using Boost.Parameter... ;-)
since the arguments to be passed into object constructors are known at compile time, i guess i could still do this?
I guess. Why, exactly, did you want to use strings, then?
a) have some sort of cpp template i.e. some, not all the code intentionally left out (say marked by "NA"), to be filled from a text file, via a text processor b) compile the modified cpp file.
i'm not all that experienced with parsers and such. is this where Boost.Xpressive etc fit in or should i just go ahead and use perl?
If you're going to go back to using Boost.Parameter, you probably won't have any use for parsers and such. If you're going to use parsers and such, there probably is no role for Boost.Parameter. I guess you could set up a toolchain where you parse some text file, generate C++ code, and compile that... is that where you are heading? And if so, why bother with the text file? -- Dave Abrahams BoostPro Computing http://www.boostpro.com
David Abrahams wrote:
on Wed Sep 10 2008, e r
wrote: So I'd like to put all these arguments in a file, each line corresponding to an object, rather than have a messy cpp file. Well, if you're willing to compile that file as C++, you could go back to using Boost.Parameter... ;-) since the arguments to be passed into object constructors are known at compile time, i guess i could still do this?
I guess. Why, exactly, did you want to use strings, then?
I guess in my first post wanted something like this: std::string line; while(std::getline(is,line) ){ } But your remarks have made it irrelevant and I wasn't even thinking of this in my second post.
a) have some sort of cpp template i.e. some, not all the code intentionally left out (say marked by "NA"), to be filled from a text file, via a text processor b) compile the modified cpp file.
i'm not all that experienced with parsers and such. is this where Boost.Xpressive etc fit in or should i just go ahead and use perl?
If you're going to go back to using Boost.Parameter, you probably won't have any use for parsers and such. If you're going to use parsers and such, there probably is no role for Boost.Parameter.
I guess you could set up a toolchain where you parse some text file, generate C++ code, and compile that... is that where you are heading? And if so, why bother with the text file?
generate C++ code from a text file: yes, that's what I meant in my
second post.
Why bother? Because I'd like to have the text file to have a simpler
syntax and a cleaner aspect, particularly if I'm going to change it
often. for example:
args.txt:
{Type=type0,tag::key00=arg00,tag::key01=arg01,...}
.
.
.
{Type=typeN,tag::keyN0=argN0,tag::keyN1=argN1,...}
*.cpp
//Some code here
typedef fusion::vector
participants (2)
-
David Abrahams
-
e r