1

如果有大量用户定义的类型实现operator<<写入std::ostream. 在使用 Pantheios 记录我的类型时如何使用这些?

4

2 回答 2

2

您需要为您自己的数据类型提供“垫片”。这似乎是有关如何执行此操作的文档:http: //www.pantheios.org/tutorials_code.html#types_without_shims。例子:

namespace stlsoft
{
  inline stlsoft::shim_string<char> c_str_data_a(Point const& point)
  {
    stlsoft::shim_string<char> s(101);

    int cch = ::sprintf(s, "{%d, %d; area=%d}",
                        point.x, point.y, point.x * point.y);

    s.truncate(static_cast<size_t>(cch));

    return s;
  }
  inline size_t c_str_len_a(Point const& point)
  {
    char buff[101];

    return static_cast<size_t>(::sprintf(&buff[0], "{%d, %d; area=%d}",
                               point.x, point.y, point.x * point.y));
  }

} // namespace stlsoft

在这种情况下,类型可以直接传递给日志语句:

pantheios::log_ERROR("Point: ", point);

祝你好运!

于 2010-12-02T13:30:59.570 回答
1

好吧,有一种方法可以重复使用,operator<<但它并不漂亮。我个人使用 boost::lexical_cast 库来将几乎任何数据类型转换为 pantheios 原生支持的 std::string 数据类型。因此,如果您已经operator<<为该类定义了,point那么您可以简单地键入:

pantheios::log_ERROR("Point: ", boost::lexical_cast<string>(point_object))

当然,这有一些警告。许多人抱怨 boost::lexical_cast 很慢。你可以谷歌它并找到一些相同的文章(http://stackoverflow.com/questions/1250795/very-poor-boostlexical-cast-performance,http://accu.org/index.php/journals/1375)。考虑到 Pantheios 拥有卓越的性能,您可能会失去一些优势。最明显的是,当你添加 boost::lexical_cast 时,你可以在你的项目中添加几百个头文件。您还必须为每次转换输入更多的字母(例如 boost::lexical_cast)(您可以使用宏将其最小化#define BLCS boost::lexical_cast<string>——但这比某些人可能接受的更多间接性)。

于 2010-12-27T07:15:41.410 回答