나는 boost :: archive :: xml_oachive와 같은 사용자 정의 아카이브를 만들 계획을 가지고 있으며 boost/libs/serialization/example 폴더에서 좋은 예제를 발견했다.어떻게 파스 (parse) 포인터 인 아카이브를 만드나요?
// 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.
}
도와 주시겠습니까? 파생 포인터에 기본 포인터를 어떻게 가져 옵니까?
나는 해결책이되었다. simple_log_archive.hpp는 기본 mpl 플로우의 샘플입니다. 자동 캐스팅 할 수있는 사용자 정의 아카이브를 만들려면 먼저 상속 detail :: common_oarchive 또는 detail :: common_iarchive를 구현하고 구현하십시오. basic_binary/text/xml_archive를 참조하십시오. detail :: common_archive는 이미 포인터 캐스팅 프로세스를 구현했습니다. 둘째, BOOST_CLASS_EXPORT (파생 됨)를 정의하십시오. 이 매크로는 make guid입니다.이 guid는 find : "true type"을 찾기 위해 common_archive에서 자세히 사용됩니다. –
마지막으로, 왜 여전히 text_oarchive가 register_type()없이 가능한지 알지 못하지만 보관 용으로 유형을 등록해야합니다 (ar.register_type()) –