Giriş
Boost bazı STL sınıfları için serialization desteği veriyor. Tek yapmamız gereken, bazı dosyaları include etmek. boost serialization ile kaydedilen stl collection sınıflarında ilk önce collection'ın boyutu yazılıyor.
Şu satırı dahil ederiz.
Şu satırı dahil ederiz.
Elimizde şöyle bir kod olsun. Parent ve Child sınıfları çift yönlü ilişkiye sahip olsun
Şu satırı dahil ederiz.
Boost bazı STL sınıfları için serialization desteği veriyor. Tek yapmamız gereken, bazı dosyaları include etmek. boost serialization ile kaydedilen stl collection sınıflarında ilk önce collection'ın boyutu yazılıyor.
std::dequeue
Şu satırı dahil ederiz.
Şu satırı dahil ederiz.
Şu satırı dahil ederiz.
#include <boost/serialization/deque.hpp>Şu satırı dahil ederiz.
#include <boost/serialization/list.hpp>
Örnek
Elimizde şöyle bir kod olsun. Bu kod verilen her tipi kaydedip okuyabilir.
Elimizde şöyle bir map olsun.
Örnek
Elimizde std::pair içeren bir vector olsun
std::shared_ptr
Şu satırı dahil ederiz.
Şu satırı dahil ederiz.Elimizde şöyle bir kod olsun. Bu kod verilen her tipi kaydedip okuyabilir.
template <typename SaveClass>
void saveData(const std::string filename, SaveClass const& c)
{
  std::remove(filename.c_str());
  std::ofstream ofs(filename);
  boost::archive::text_oarchive ta(ofs);
  ta << c;
}
template <typename LoadClass>
void loadData(const std::string filename, LoadClass& c)
{
  std::ifstream ifs(filename);
  boost::archive::text_iarchive ta(ifs);
  ta >> c;
}std::string filename = "userandPassBackup.txt";
{
  std::map<std::string, std::string> userandPass {
    { "John", "pa$$w0rd" },
    { "Jane", "welcome01" } };
    saveData(filename, userandPass);
  
}
{
  std::map<std::string, std::string> userandPass;
  loadData(filename, userandPass);
}Elimizde şöyle bir map olsun.
typedef   map<int,std::string> groups;
groups  e_group;template<class archive>
void serialize(archive& ar, const unsigned int version)
{
  using boost::serialization::make_nvp;
  ar & make_nvp("Connections", e_group);
}<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="14">
<Connections class_id="0" tracking_level="0" version="0">
  <Connections class_id="1" tracking_level="0" version="0">
    <count>2</count>
    <item_version>0</item_version>
      <item class_id="2" tracking_level="0" version="0">
        <first>1</first>
        <second>ETOTO</second>
      </item>
      <item>
        <first>2</first>
        <second>ETOTO</second>
      </item>
    </Connections>
</Connections>
</boost_serialization>namespace boost {
  namespace serialization { 
    template <typename Ar>
    void serialize(Ar& ar, std::pair<int const, std::string>& p, unsigned) {
      ar & make_nvp("groupid", p.first) & make_nvp("groupType", p.second);
    }
  }
}<Connections class_id="1" tracking_level="0" version="0">
  <count>2</count>
  <item_version>0</item_version>
  <item class_id="2" tracking_level="0" version="0">
    <groupid>1</groupid>
    <groupType>ETOTO</groupType>
  </item>
  <item>
    <groupid>2</groupid>
    <groupType>ETOTO</groupType>
  </item>
</Connections>Örnek
Elimizde std::pair içeren bir vector olsun
template <class TKey, class TValue>
class DataModel
{
public:
  DataModel() = default;
  ~DataModel() = default;
private:
  std::vector<boost::shared_ptr<std::pair<TKey, TValue>>> mData = {};
}friend class boost::serialization::access;
template <typename Archive>
void serialize( Archive &ar, const unsigned int version )
{
  ar &boost::serialization::make_nvp( "Data", mData );
}std::shared_ptr
Şu satırı dahil ederiz.
#include <boost/serialization/shared_ptr.hpp>Bir sürü shared_ptr nesnesi göndermek için bunları bir vector'e doldurup sonra göndermek daha iyi. Yazan tarafta şöyle yaparız.Depending on how the class is used and other factors, serialized objects may be tracked by memory address. This prevents the same object from being written to or read from an archive multiple times. These stored addresses can also be used to delete objects created during a loading process that has been interrupted by throwing of an exception.
std::vector<std::shared_ptr<Message>> messages;
for (int i = 0; i < 10; i++) {
  std::shared_ptr<Message> dl = std::make_shared<Message>();
  ...
  messages.emplace_back(dl);
}
boost::archive::text_oarchive archive(stream);
archive << messages;
while (true) {
  try {
    std::vector<std::shared_ptr<Message>> messages;
    archive >> messages;
    ...
  } catch (std::exception &ex) {
    cout << ex.what() << endl;
    ...
}#include <boost/serialization/string.hpp>Şu satırı dahil ederiz.
#include <boost/serialization/unordered_map.hpp>class foo {
  std::unordered_map<int, int> m_map;
public:
  friend class boost::serialization::access;
  template<class Archive>
  void serialize(Archive & ar, const unsigned int) {
    ar & m_map;
  }
};Şu satırı dahil ederiz.
#include <boost/serialization/unique_ptr.hpp>Elimizde şöyle bir kod olsun. Parent ve Child sınıfları çift yönlü ilişkiye sahip olsun
class Parent;
class Child {
public:
  Child(Parent *parent) :
    m_parent(parent)  {}
  Parent *m_parent          { nullptr };
  
private:
  friend class boost::serialization::access;
  template <class Archive> void serialize(Archive &ar, unsigned) {
    ar & m_parent; 
  }
  Child() = default;
};
class Parent {
public:
  Parent() : m_child(std::make_unique<Child>(this)) {}
  friend class boost::serialization::access;
  template <class Archive> void serialize(Archive &ar, unsigned) {
    ar & m_child; 
  }
  std::unique_ptr<Child> m_child;
};std::stringstream ss;
{
  boost::archive::text_oarchive oa(ss);
  Parent p;
  assert(&p == p.m_child->m_parent);
  oa << p;
}
std::cout << ss.str();
{
  boost::archive::text_iarchive ia(ss);
  Parent p;
  // let's purposelly break the invariants so we know deserialization
  // does restore them as required, and we not just "getting lucky":
  p.m_child->m_parent = nullptr;
  
  assert(&p != p.m_child->m_parent);
  
  p.m_child.reset();
  // now deserialize
  ia >> p;
  assert(&p == p.m_child->m_parent);
  
}Şu satırı dahil ederiz.
#include <boost/serialization/vector.hpp> 
Hiç yorum yok:
Yorum Gönder