Comments (3)
Hello,
All extensions are located here.
Core components of an extension is:
- Extension class itself, which is defined by implementing two template functions
serialize
(save) anddeserialize
(load). 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.
This is very helpful, I'll try it with the first occasion. Thank you!
from bitsery.
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)
- Custom Extension HOT 1
- bug in apple libc++ ARM hash_code function HOT 2
- Windows and boost::json - compile error HOT 1
- Need help writing separate serializer and deserializer functions for a custom vector type HOT 1
- Easy serialization for POD types HOT 12
- It can't serialize a array member HOT 4
- How to serialize a pointer of map? HOT 2
- Vector of variants with a bit-packed type HOT 2
- Example usage of uint8_t* buffer? HOT 5
- Is bitsery faster than capnp? HOT 1
- Proper way to determine required buffer size before serializing? HOT 2
- Vector<String *> HOT 1
- Vector of bit-packed structs not measuring correctly? HOT 6
- Can I parse bitsery-serialized buffer using Python struct.unpack? HOT 2
- Are there any single header version available? HOT 2
- Does this use dynamic allocation? HOT 3
- How to find the reason of InvalidData? HOT 1
- Parallel read and write? HOT 1
- Writing to pointer to uint8_t array HOT 1
- Problem with deserialization of structure - Assertion newOffset <= _endReadOffset HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bitsery.