2011-01-20 7 views
3

boost :: archive :: xml_oachiveのようなカスタムアーカイブを作成する予定です。boost/libs/serialization/exampleフォルダの良い例が見つかりました。パースポインタのアーカイブを作成するにはどうすればよいですか?

// 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; 
    } 
    ... 
}; 

同様に、私は私のカスタムアーカイブを作成した。

は、次のコード(上記のディレクトリにあります)を参照してください。しかし、鉱山以上のコードは、派生ポインタへのベースポインタの自動キャストではありません。たとえば、

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

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

お手伝いできますか?ベースポインタから派生ポインタへの到達方法を教えてください。

+0

解決策が見つかりました。 simple_log_archive.hppは基本的なmplフローのサンプルです。自動キャスト可能なカスタムアーカイブを作成する場合は、まず継承detail :: common_oarchiveまたはdetail :: common_iarchiveを実装します。 basic_binary/text/xml_archiveを参照してください。 detail :: common_archiveはすでにポインタキャストプロセスを実装しています。 次に、BOOST_CLASS_EXPORT(Derived)を定義します。このマクロはmake guidです。このguidは、find "true type"のために、詳細:common_archiveで使用されます。 –

+0

最後に、text_oarchiveがregister_typeことなく可能である理由私はまだ)(知らないが、あなたはアーカイブのタイプを登録する必要があります。(ar.register_type ()) –

答えて

0

基本クラスポインタを派生クラスに変換する方法が必要な場合は、dynamic_cast<Derived*>(base)を実行します。

+0

ブーストシリアル化がシングルトン型特性マップを持っています。だから、それは手動で鋳造する必要はありません。 –

関連する問題