Coder Social home page Coder Social logo

mattcontinisio / cereal-yaml Goto Github PK

View Code? Open in Web Editor NEW
23.0 2.0 3.0 1.6 MB

YAML archive for cereal library

License: BSD 3-Clause "New" or "Revised" License

CMake 1.02% C++ 89.28% HTML 0.02% Shell 0.48% Batchfile 0.08% C 0.55% Python 7.67% Makefile 0.43% M4 0.42% Objective-C 0.05%
cereal serialization yaml c-plus-plus yaml-cpp

cereal-yaml's Introduction

cereal-yaml Build Status Build status

YAML archive for cereal, powered by yaml-cpp.

How to use

Example

See example/example.cpp

#include <cereal-yaml/archives/yaml.hpp>

#include <cereal/types/memory.hpp>
#include <cereal/types/unordered_map.hpp>

#include <fstream>

struct MyRecord
{
    MyRecord() = default;
    MyRecord(uint8_t x_, uint8_t y_, float z_)
        : x(x_)
        , y(y_)
        , z(z_)
    {}

    uint8_t x, y;
    float z = 0;

    template <class Archive>
    void serialize(Archive& ar)
    {
        ar(x, y, z);
    }

    friend std::ostream& operator<<(std::ostream& os, const MyRecord& myRecord);
};

inline std::ostream& operator<<(std::ostream& os, const MyRecord& myRecord)
{
    os << "{";
    os << "x=" << int(myRecord.x) << ",";
    os << "y=" << int(myRecord.y) << ",";
    os << "z=" << myRecord.z;
    os << "}";
    return os;
}

struct SomeData
{
    int32_t id;
    std::shared_ptr<std::unordered_map<uint32_t, MyRecord>> data;

    template <class Archive>
    void save(Archive& ar) const
    {
        ar(data);
    }

    template <class Archive>
    void load(Archive& ar)
    {
        static int32_t idGen = 0;
        id = idGen++;
        ar(data);
    }

    friend std::ostream& operator<<(std::ostream& os, const SomeData& someData);
};

inline std::ostream& operator<<(std::ostream& os, const SomeData& someData)
{
    os << "{";
    os << "id=" << someData.id << ",";
    os << "data={";
    for (auto entry : *someData.data)
    {
        os << entry.first << ":" << entry.second << ",";
    }
    if (someData.data->size() > 0)
    {
        os << "\b\b";
    }
    os << "}";
    os << "}";
    return os;
}

int main()
{
    std::ostringstream os;
    {
        cereal::YAMLOutputArchive archive(os);
        SomeData myData;
        myData.id = 5;
        myData.data = std::make_shared<std::unordered_map<uint32_t, MyRecord>>();
        myData.data->emplace(1, MyRecord{1, 2, 3.0});
        myData.data->emplace(2, MyRecord{4, 5, 6.1});
        std::cout << "myData=" << myData << std::endl;
        archive(myData);
    }
    std::cout << "output=\n" << os.str() << std::endl;

    std::istringstream is(os.str());
    {
        cereal::YAMLInputArchive archive(is);
        SomeData myData;
        archive(myData);
        std::cout << "myData=" << myData << std::endl;
    }

    return 0;
}

Output:

myData={id=5,data={2:{x=4,y=5,z=6.1},1:{x=1,y=2,z=3}}
output=
value0:
  value0:
    ptr_wrapper:
      id: 2147483649
      data:
        - key: 2
          value:
            value0: 4
            value1: 5
            value2: 6.099999904632568
        - key: 1
          value:
            value0: 1
            value1: 2
            value2: 3
myData={id=0,data={1:{x=1,y=2,z=3},2:{x=4,y=5,z=6.1}}

cereal-yaml's People

Contributors

mattcontinisio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.