Giter Club home page Giter Club logo

Comments (3)

fraillt avatar fraillt commented on July 18, 2024 1

Hello,
All extensions are located here.
Core components of an extension is:

  1. Extension class itself, which is defined by implementing two template functions serialize (save) and deserialize (load).
  2. ExtensionTraits specialization for an extension, which specifies how it should be used.

I wrote a simple implementation that accepts serialize and deserialize callable objects.

namespace bitsery {
    namespace ext {

        template <typename SerFnc, typename DesFnc>
        class SaveAndLoad{
        public:
            SaveAndLoad(SerFnc serFnc, DesFnc desFnc)
                :_serFnc{std::move(serFnc)},
                _desFnc{std::move(desFnc)} {}

            template<typename Ser, typename T, typename Fnc>
            void serialize(Ser& ser, const T& obj, Fnc&& ) const {
                _serFnc(ser,obj);
            }

            template<typename Des, typename T, typename Fnc>
            void deserialize(Des& des, T& obj, Fnc&& ) const {
                _desFnc(des, obj);
            }
        private:
            std::decay_t<SerFnc> _serFnc;
            std::decay_t<DesFnc> _desFnc;
        };
    }

    namespace traits {
        template<typename T, typename SerFnc, typename DesFnc>
        struct ExtensionTraits<ext::SaveAndLoad<SerFnc, DesFnc>, T> {
            using TValue = void;
            static constexpr bool SupportValueOverload = false;
            static constexpr bool SupportObjectOverload = true;
            static constexpr bool SupportLambdaOverload = false;
        };

    }

}

And if you use C++17, you can use it like this (using with C++11 is not convenient because it requires to specify template parameters for SaveAndLoad):

    s.ext(o.my_type, bitsery::ext::SaveAndLoad(
        [](auto& ser, const MyType& obj) { ... }, // save function
        [](auto& des, MyType& obj) { ... })); //load function

If you only want this functionality only for a few types, you can write extension exactly for that type.

namespace bitsery {
    namespace ext {

        class SaveLoadMyType {
        public:
            template<typename Ser, typename Fnc>
            void serialize(Ser& ser, const MyType& obj, Fnc&& ) const {
               ...
            }

            template<typename Des, typename Fnc>
            void deserialize(Des& des, MyType& obj, Fnc&& ) const {
               ...
            }
        };
    }

    namespace traits {
        template<typename T>
        struct ExtensionTraits<ext::SaveLoadMyType, T> {
            using TValue = void;
            static constexpr bool SupportValueOverload = false;
            static constexpr bool SupportObjectOverload = true;
            static constexpr bool SupportLambdaOverload = false;
        };

    }
}

You can use this like this.

s.ext(o.my_type, bitsery::ext::SaveLoadMyType{});

Hope that helped ;)

from bitsery.

cdcseacave avatar cdcseacave commented on July 18, 2024

This is very helpful, I'll try it with the first occasion. Thank you!

from bitsery.

fraillt avatar fraillt commented on July 18, 2024

Since extensions are at the core of bitsery, I wrote more detailed information on their design here.
I believe it will be useful for many users :)

from bitsery.

Related Issues (20)

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.