我很难找到如何使用boost::any
创建一个可以先使用模板打印任何类型的打印功能。
template <typename T>
struct printer {
void print(ostream& os, const boost::any& a);
};
我需要先定义print()
。
我希望拥有真正的operator <<
,这个想法很简单:将每个任何对象附加到一个类的实例
printer<T>
使用合适的T并在any
的值类型更改时更改此对象。
第一个技术问题是打印机对象依赖于T,而任何不是(也不应该是)类模板。
我真的需要一只手为今晚或明天我有一个明天的截止日期,但我希望今晚继续努力。
答案 0 :(得分:3)
“Beyond the C++ Standard Library: An Introduction to Boost”中描述了很简单的方法:
struct streamer {
virtual void print(ostream &o, const boost::any &a) const =0;
virtual streamer * clone() const = 0;
virtual ~streamer() {}
};
template <class T>
struct streamer_impl: streamer{
void print(ostream &o, const boost::any &a) const { o << *boost::any_cast<T>(a); }
streamer *clone() const { return new streamer_impl<T>(); }
};
class any_out {
streamer *streamer_;
boost::any o_;
void swap(any_out & r){
std::swap(streamer_, r.streamer_);
std::swap(o_, r.o_);
}
public:
any_out(): streamer_(0) {}
template<class T> any_out(const T& value)
: streamer_(new streamer_impl<T>()), o_(value) {}
any_out(const any_out& a)
: streamer_(a.streamer_ ? a.streamer_->clone() : 0), o_(a.o_) {}
template <class T>
any_out & operator=(const T& r) {
any_out(r).swap(*this);
return *this;
}
~any_out() { delete streamer_; }
friend std::ostream &operator<<(std::ostream& o, const any_out & a) {
if(a.streamer_)
a.streamer_->print(o, a);
return o;
}
};
然后您使用any_out
代替boost::any
。
答案 1 :(得分:1)
我是这样做的,我认为这是干净安全的:
any_extension.hpp:
namespace cpputil
{
struct AnyWriter
{
/// Register a type with the AnyWriter.
/// @pre T must have an ostream << operator available somewhere
template<class T> static bool registerType()
{
return registeredTypes().emplace(std::type_index(typeid(T)),
std::bind(&AnyWriter::write<T>,
std::placeholders::_1,
std::placeholders::_2)).second;
}
/// Write any registred object to a stream
/// @pre Underlying type must have been registered with a call to AnyWriter::registerType<T>
/// @param os is reference to a std::ostream
/// @param anyObject is a reference to a boost::any
static void writeAny(std::ostream& os, const boost::any& anyObject);
private:
// A function object that converts an any to a type and streams it to an ostream
using WriteFunction = std::function<void (std::ostream&, const boost::any&)>;
// a map of typeinfo to WriteFunction
using RegisteredTypes = std::unordered_map<std::type_index, WriteFunction >;
// retrieve the WriteFunction map in a safe way
static RegisteredTypes& registeredTypes();
// Convert an any to a type, and write it to a stream
template<class T> static void write(std::ostream& os, const boost::any& anyObject) {
try {
const T& typedObject = boost::any_cast<const T&>(anyObject);
os << typedObject;
}
catch(boost::bad_any_cast& e) {
os << "<exception in conversion: " << e.what() << ">";
}
}
};
}
namespace std {
ostream& operator<<(ostream& os, const ::boost::any& anyObject);
}
any_extension.cpp:
#include "any_extension.h"
#include <string>
namespace cpputil {
namespace AnyWriterRegistration {
const bool stringRegistered = AnyWriter::registerType<std::string>();
const bool intRegistered = AnyWriter::registerType<int>();
const bool doubleRegistered = AnyWriter::registerType<double>();
}
AnyWriter::RegisteredTypes& AnyWriter::registeredTypes()
{
static RegisteredTypes _registrationMap;
return _registrationMap;
}
void AnyWriter::writeAny(std::ostream &os, const boost::any &anyObject)
{
auto registered = registeredTypes();
auto iFind = registered.find(anyObject.type());
if(iFind == registered.end()) {
os << "<unregistered type: " << anyObject.type().name() << ">";
}
else {
iFind->second(os, anyObject);
}
}
}
namespace std {
ostream& operator<<(ostream& os, const ::boost::any& anyObject)
{
if(anyObject.empty()) {
os << "<empty>";
}
else {
cpputil::AnyWriter::writeAny(os, anyObject);
}
return os;
}
}
对于您支持的任何类型,只需确保已为其类型调用AnyWriter :: register()并且运算符&lt;&lt;为它存在。
例如:
any_test.cpp:
struct chicken {};
std::operator<<(std::ostream& os, const chicken& aChicken) {
os << "cluck!";
return os;
}
namespace {
const bool chickenRegistered = AnyWriter::register<Chicken>();
}
void chickenTest() {
boost::any animal = chicken();
std::cout << animal << std::endl;
}
输出: 咕!
答案 2 :(得分:0)
在Boost邮件列表中查看此主题: http://lists.boost.org/Archives/boost/2005/01/79232.php
它有一些想法,其中一些似乎没有问题,其中一些没有(对我而言)。但总的来说,这似乎是一项难以完成的任务,因为(如该线程所述),某些类型永远不会被ostream,但可能包含在boost::any
对象中。