msgpack::object const& operator()(msgpack::object const& o, my_class& v) const {
     if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
     if (o.via.array.size != 2) throw msgpack::type_error();
     v = my_class(
         o.via.array.ptr[0].as<std::string>(),
         o.via.array.ptr[1].as<int>());
     return o;
 }
예제 #2
0
// The test checks that default formatting work
BOOST_AUTO_TEST_CASE_TEMPLATE(default_formatting, CharT, char_types)
{
    typedef logging::record_view record_view;
    typedef logging::attribute_set attr_set;
    typedef std::basic_string< CharT > string;
    typedef logging::basic_formatting_ostream< CharT > osstream;
    typedef logging::basic_formatter< CharT > formatter;
    typedef test_data< CharT > data;

    attrs::constant< int > attr1(10);
    attrs::constant< double > attr2(5.5);
    attrs::constant< my_class > attr3(my_class(77));

    attr_set set1;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;
    set1[data::attr3()] = attr3;

    record_view rec = make_record_view(set1);

    // Check for various modes of attribute value type specification
    {
        string str1, str2;
        osstream strm1(str1), strm2(str2);
        formatter f = expr::stream << expr::attr< int >(data::attr1()) << expr::attr< double >(data::attr2());
        f(rec, strm1);
        strm2 << 10 << 5.5;
        BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
    }
    {
        string str1, str2;
        osstream strm1(str1), strm2(str2);
        formatter f = expr::stream << expr::attr< logging::numeric_types >(data::attr1()) << expr::attr< logging::numeric_types >(data::attr2());
        f(rec, strm1);
        strm2 << 10 << 5.5;
        BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
    }
    // Check that custom types as attribute values are also supported
    {
        string str1, str2;
        osstream strm1(str1), strm2(str2);
        formatter f = expr::stream << expr::attr< my_class >(data::attr3());
        f(rec, strm1);
        strm2 << my_class(77);
        BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
    }
    // Check how missing attribute values are handled
    {
        string str1;
        osstream strm1(str1);
        formatter f = expr::stream
            << expr::attr< int >(data::attr1())
            << expr::attr< int >(data::attr4()).or_throw()
            << expr::attr< double >(data::attr2());
        BOOST_CHECK_THROW(f(rec, strm1), logging::runtime_error);
    }
    {
        string str1, str2;
        osstream strm1(str1), strm2(str2);
        formatter f = expr::stream
            << expr::attr< int >(data::attr1())
            << expr::attr< int >(data::attr4())
            << expr::attr< double >(data::attr2());
        f(rec, strm1);
        strm2 << 10 << 5.5;
        BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
    }
}