Line data Source code
1 :
2 : /* Copyright (c) 2015-2016, Human Brain Project
3 : * Stefan.Eilemann@epfl.ch
4 : * grigori.chevtchenko@epfl.ch
5 : */
6 :
7 : #ifndef ZEROBUF_ZEROBUF_H
8 : #define ZEROBUF_ZEROBUF_H
9 :
10 : #include <servus/serializable.h> // base class
11 : #include <servus/uint128_t.h> // used inline in operator <<
12 : #include <zerobuf/Allocator.h> // MSVC needs it for std::unique_ptr
13 : #include <zerobuf/api.h>
14 : #include <zerobuf/json.h> // friend
15 : #include <zerobuf/types.h>
16 :
17 : namespace zerobuf
18 : {
19 : /**
20 : * Base class for all Zerobuf serializable objects.
21 : *
22 : * The zerobufCxx.py code generator creates subclasses of this class. Zerobuf
23 : * objects can serialize/deserialize directly from their member storage and from
24 : * and to JSON. All members are zero-initialized, unless they do have a default
25 : * value. fromJSON() will compact the Zerobuf.
26 : */
27 : class Zerobuf : public servus::Serializable
28 : {
29 : public:
30 : ZEROBUF_API virtual ~Zerobuf();
31 :
32 : /** @return the static size of this object in bytes. */
33 : virtual size_t getZerobufStaticSize() const = 0;
34 :
35 : /** @return the number of dynamics fields of this object. */
36 : virtual size_t getZerobufNumDynamics() const = 0;
37 :
38 : /**
39 : * Remove unused holes from the zerobuf.
40 : *
41 : * Compaction only occurs if the current allocation exceeds the relative
42 : * threshold to the optimal size, that is, if unusedMemory divided by
43 : * optimalMemory is above the threshold.
44 : *
45 : * @param threshold the compaction threshold
46 : */
47 : ZEROBUF_API virtual void compact(float threshold = 0.1f);
48 :
49 : /** Assignment operator. */
50 : ZEROBUF_API Zerobuf& operator=(const Zerobuf& rhs);
51 :
52 : /** Move ctor. */
53 : ZEROBUF_API Zerobuf(Zerobuf&& rhs);
54 :
55 : /** Move operator. May copy data if zerobuf is not movable */
56 : ZEROBUF_API Zerobuf& operator=(Zerobuf&& rhs);
57 :
58 : /** @return true if both objects contain the same data */
59 : ZEROBUF_API bool operator==(const Zerobuf& rhs) const;
60 :
61 : /** @return true if both objects contain different data */
62 : ZEROBUF_API bool operator!=(const Zerobuf& rhs) const;
63 :
64 : /** @internal */
65 : ZEROBUF_API void reset(AllocatorPtr allocator);
66 :
67 : /** @internal Check consistency of zerobuf */
68 : ZEROBUF_API void check() const;
69 :
70 : protected:
71 : ZEROBUF_API explicit Zerobuf(AllocatorPtr alloc); // takes ownership of
72 : // alloc
73 :
74 : /** Called if any data in this object has changed. */
75 2954 : ZEROBUF_API virtual void notifyChanged() {}
76 : // used by generated ZeroBuf objects
77 : ZEROBUF_API const Allocator& getAllocator() const;
78 : ZEROBUF_API Allocator& getAllocator();
79 :
80 : ZEROBUF_API void _copyZerobufArray(const void* data, size_t size,
81 : size_t arrayNum);
82 :
83 : ZEROBUF_API virtual void _parseJSON(const Json::Value& json);
84 : ZEROBUF_API virtual void _createJSON(Json::Value& json) const;
85 : ZEROBUF_API friend void fromJSON(const Json::Value&, Zerobuf&);
86 : ZEROBUF_API friend void toJSON(const Zerobuf&, Json::Value&);
87 :
88 : ZEROBUF_API bool _fromBinary(const void* data, const size_t size) override;
89 :
90 : private:
91 : AllocatorPtr _allocator;
92 :
93 : Zerobuf() = delete;
94 : Zerobuf(const Zerobuf& zerobuf) = delete;
95 :
96 : ZEROBUF_API Data _toBinary() const final;
97 : ZEROBUF_API bool _fromJSON(const std::string& json) final;
98 : ZEROBUF_API std::string _toJSON() const final;
99 : };
100 :
101 1 : inline std::ostream& operator<<(std::ostream& os, const Zerobuf& zerobuf)
102 : {
103 1 : return os << "\"" << zerobuf.getTypeName() << "\" : " << zerobuf.toJSON();
104 : }
105 : }
106 :
107 : #endif
|