|
Boost : |
From: Howard Hinnant (hinnant_at_[hidden])
Date: 2005-04-23 19:41:59
I think I may have to disagree with John's premise:
> algorithms go in facets.
> configuration data goes in the iostream object itself.
It is easy enough to store data in a facet. Perhaps even easier than
into iword/pword.
And you can use manipulators to store into the facet, or even create
them:
#include <iostream>
#include <locale>
struct my_facet
: public std::locale::facet
{
explicit my_facet(const std::string s) : data_(s) {}
std::string data_;
static std::locale::id id;
};
std::locale::id my_facet::id;
struct my_manip
{
explicit my_manip(const std::string& s) : data_(s) {}
std::string data_;
};
std::ostream&
operator<< (std::ostream& os, const my_manip& m)
{
os.imbue(std::locale(os.getloc(), new my_facet(m.data_)));
return os;
}
struct A
{
};
std::ostream&
operator<< (std::ostream& os, const A&)
{
const my_facet& mf = std::use_facet<my_facet>(os.getloc());
return os << mf.data_;
}
int main()
{
A a1, a2;
std::cout << my_manip("some data");
std::cout << a1 << '\n';
std::cout << a2 << '\n';
}
It seems to me you can have both facets and manipulators, and have them
all work seamlessly together (unless I'm missing something, which is of
course completely possible). The manipulator can check if a facet
exists, and if it does read data from it, and at any rate either
replace the existing data or create a whole new facet.
I'm not a big fan of the whole locale/facet thing. But for this
purpose it works a whole lot better for storing data than the
pword/iword thing, unless all you really need to store is a non-heap
pointer or an int.
-Howard
PS: Look Ma, no blimey virtual functions in my facet. Just data!
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk