ZeroBuf  0.5.0
Zero-copy, zero-serialize, zero-hassle protocol buffers
Zerobuf.h
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 {
27 class Zerobuf : public servus::Serializable
28 {
29 public:
30  ZEROBUF_API virtual ~Zerobuf();
31 
33  virtual size_t getZerobufStaticSize() const = 0;
34 
36  virtual size_t getZerobufNumDynamics() const = 0;
37 
47  ZEROBUF_API virtual void compact(float threshold = 0.1f);
48 
50  ZEROBUF_API Zerobuf& operator=(const Zerobuf& rhs);
51 
53  ZEROBUF_API Zerobuf(Zerobuf&& rhs);
54 
56  ZEROBUF_API Zerobuf& operator=(Zerobuf&& rhs);
57 
59  ZEROBUF_API bool operator==(const Zerobuf& rhs) const;
60 
62  ZEROBUF_API bool operator!=(const Zerobuf& rhs) const;
63 
65  ZEROBUF_API void reset(AllocatorPtr allocator);
66 
68  ZEROBUF_API void check() const;
69 
70 protected:
71  ZEROBUF_API explicit Zerobuf(AllocatorPtr alloc); // takes ownership of
72  // alloc
73 
75  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 inline std::ostream& operator<<(std::ostream& os, const Zerobuf& zerobuf)
102 {
103  return os << "\"" << zerobuf.getTypeName() << "\" : " << zerobuf.toJSON();
104 }
105 }
106 
107 #endif
Defines export visibility macros for library ZeroBuf.
Zerobuf & operator=(const Zerobuf &rhs)
Assignment operator.
Zero-copy, zero-serialize, zero-hassle protocol buffers.
Definition: Allocator.h:12
bool operator==(const Zerobuf &rhs) const
virtual size_t getZerobufNumDynamics() const =0
bool operator!=(const Zerobuf &rhs) const
virtual size_t getZerobufStaticSize() const =0
Allocator base class and interface.
Definition: Allocator.h:20
Base class for all Zerobuf serializable objects.
Definition: Zerobuf.h:27
virtual void notifyChanged()
Called if any data in this object has changed.
Definition: Zerobuf.h:75
virtual void compact(float threshold=0.1f)
Remove unused holes from the zerobuf.