How to make an archive that is a parsing pointer?

I plan to create my own archive, for example boost :: archive :: xml_oachive, and I found good examples in the boost / libs / serialization / example folder.

See the following code (in the directory above):

// simple_log_archive.hpp
...
class simple_log_archive
{
    ...
    template <class Archive>
    struct save_primitive
    {
        template <class T>
        static void invoke(Archive& ar, const T& t)
        {
            // streaming
        }
    };

    template <class Archive>
    struct save_only
    {
        template <class T>
        static void invoke(Archive& ar, const T& t)
        {
            boost::serialization::serialize_adl(ar, const_cast<T&>(t),
                ::boost::serialization::version<T>::value);
        }
    };

    template <class T>
    void save(const T& t)
    {
        typedef BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<boost::is_enum<T>,
            boost::mpl::identity<save_enum_type<simple_log_archive> >,
        //else
        BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<
            // if its primitive
                boost::mpl::equal_to<
                    boost::serialization::implementation_level<T>,
                    boost::mpl::int_<boost::serialization::primitive_type>
                >,
                boost::mpl::identity<save_primitive<simple_log_archive> >,
        // else
            boost::mpl::identity<save_only<simple_log_archive> >
        > >::type typex;
        typex::invoke(*this, t);
    }  
public:
    // the << operators 
    template<class T>
    simple_log_archive & operator<<(T const & t){
        m_os << ' ';
        save(t);
        return * this;
    }
    template<class T>
    simple_log_archive & operator<<(T * const t){
        m_os << " ->";
        if(NULL == t)
            m_os << " null";
        else
            *this << * t;
        return * this;
    }
    ...
};

In the same way, I created my own archive. But my code and above is not automatically casting a base pointer to a derived pointer. For instance,

Base* base = new Derived;
{
    boost::archive::text_oarchive ar(std::cout);
    ar << base;// Base pointer is auto casted to derived pointer! It fine.
}

{
    simple_log_archive ar;
    ar << base;// Base pointer is not auto casting. This is my problem.
}

could you help me? How to get a base pointer to a derived pointer?

+3
source share
1 answer

If you only need to convert a pointer to the base class in the derived class, then he should do this: dynamic_cast<Derived*>(base).

0
source

Source: https://habr.com/ru/post/1786584/


All Articles