|
Boost : |
From: vonosan (5045-2h8p_at_[hidden])
Date: 2002-01-29 10:36:06
Some times ago I posted very fast experimental implementation of
formatter class:
http://groups.yahoo.com/group/boost/message/22212
It uses generalized function call concept. The only restriction of
this approach is that all parameters must be passed within one full
expression.
Let me explain this concept.
Consider object or function F. To meet these concepts it should: (a)
has overloaded function call operators that accept static part of
argument list and accept up to N (N=10 is reasonable) arguments of
arbitrary type (or some open set of types, e.g. non pointers); (b)
these operators must return the object of some type with overloaded
function call operators implemented that accept up to N parameters;
(c) returned object must also meet requirement (b).
Example:
// static part of argument list
int p1, p2;
// arguments of arbitrary type
std::string v1, v3;
const int * v2, v4;
boost:any v5, v6;
int * v7;
// generalized function calls for the F pseudo declared as F(int,
int,
);
F(p1, p2, v1, v2, v3);
F(p1, p2) (v1) (v2) (v3);
F(p1, p2, v1, v2, v3) (v4, v5);
F(p1, p2, v1, v2, v3) (v4, v5) (v6) (v7);
It isn't specified when real call happened.
1. It can be called step by step inside every operator().For example,
Generalized printf without support of positional parameters can be
implemented in such way:
generalized_printf(cout, "%s = %d") ("param2", 2);
// 1. generalized_printf put to cout all characters up to %s
// 2. first operator() put to cout "param2" and then all
characters
after %s up to %d
// 3. second operator() put to cout 2 and then put rest of the string
2. Temporary objects returned from every operator() can be collected
and used later. Real call can be activated in destructor or in some
function of last temporary object.
Example 1:
cout << generalized_format("%1 = %2", "param2", 2);
// 1. first operator() returns temporary that store reference to
the "param2"
// 2. second operator() returns temporary that store reference to the
2 and pointer to the previous temporary
// 3. operator<<(cout, last_temporary) does real formatting using
collection of temporaries
Example 2:
std::string s = generalized_format("%1 = %2",
"param2", 2);
// 1. first operator() returns temporary that store reference to
the "param2"
// 2. second operator() returns temporary that store reference to the
2 and pointer to the previous temporary
// 3. operator std::string for last temporary does real formatting
using collection of temporaries
Additionally, named parameters can be supported:
F(p1, p2, named_param("foo_param") = v1);
It will work if named_param has operator= template that accept any
type T that return named_param_holder<T> type. Object of this type
store reference to the original object and is recognized by
generalized function F.
Does somebody know about similar efforts?
-------------------
Alexander Nasonov
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk