Карта сайта Kansoftware
НОВОСТИУСЛУГИРЕШЕНИЯКОНТАКТЫ
Разработка программного обеспечения

libs/log/example/doc/util_dynamic_type_disp.cpp

Boost , ,

Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

libs/log/example/doc/util_dynamic_type_disp.cpp

/*
 *          Copyright Andrey Semashev 2007 - 2015.
 * Distributed under the Boost Software License, Version 1.0.
 *    (See accompanying file LICENSE_1_0.txt or copy at
 *          http://www.boost.org/LICENSE_1_0.txt)
 */
#include <cassert>
#include <cstddef>
#include <string>
#include <iostream>
#include <boost/log/utility/type_dispatch/dynamic_type_dispatcher.hpp>
namespace logging = boost::log;
// Base interface for the custom opaque value
struct my_value_base
{
    virtual ~my_value_base() {}
    virtual bool dispatch(logging::type_dispatcher& dispatcher) const = 0;
};
// A simple attribute value
template< typename T >
struct my_value :
    public my_value_base
{
    T m_value;
    explicit my_value(T const& value) : m_value(value) {}
    // The function passes the contained type into the dispatcher
    bool dispatch(logging::type_dispatcher& dispatcher) const
    {
        logging::type_dispatcher::callback< T > cb = dispatcher.get_callback< T >();
        if (cb)
        {
            cb(m_value);
            return true;
        }
        else
            return false;
    }
};
//[ example_util_dynamic_type_dispatcher
// Visitor functions for the supported types
void on_int(int const& value)
{
    std::cout << "Received int value = " << value << std::endl;
}
void on_double(double const& value)
{
    std::cout << "Received double value = " << value << std::endl;
}
void on_string(std::string const& value)
{
    std::cout << "Received string value = " << value << std::endl;
}
logging::dynamic_type_dispatcher disp;
// The function initializes the dispatcher object
void init_disp()
{
    // Register type visitors
    disp.register_type< int >(&on_int);
    disp.register_type< double >(&on_double);
    disp.register_type< std::string >(&on_string);
}
// Prints the supplied value
bool print(my_value_base const& val)
{
    return val.dispatch(disp);
}
//]
int main(int, char*[])
{
    init_disp();
    // These two attributes are supported by the dispatcher
    bool res = print(my_value< std::string >("Hello world!"));
    assert(res);
    res = print(my_value< double >(1.2));
    assert(res);
    // This one is not
    res = print(my_value< float >(-4.3f));
    assert(!res);
    return 0;
}

Статья libs/log/example/doc/util_dynamic_type_disp.cpp раздела может быть полезна для разработчиков на c++ и boost.




Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.



:: Главная :: ::


реклама


©KANSoftWare (разработка программного обеспечения, создание программ, создание интерактивных сайтов), 2007
Top.Mail.Ru

Время компиляции файла: 2024-08-30 11:47:00
2025-05-19 21:50:26/0.027508974075317/1