ZeroBuf  0.2.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 <zerobuf/api.h>
11 #include <zerobuf/Allocator.h> // MSVC needs it for std::unique_ptr
12 #include <zerobuf/types.h>
13 #include <zerobuf/json.h> // friend
14 #include <servus/serializable.h> // base class
15 #include <servus/uint128_t.h> // used inline in operator <<
16 
17 namespace zerobuf
18 {
19 
28 class Zerobuf : public servus::Serializable
29 {
30 public:
32  virtual size_t getZerobufStaticSize() const = 0;
33 
35  virtual size_t getZerobufNumDynamics() const = 0;
36 
38  virtual void notifyChanging() {}
39 
49  ZEROBUF_API virtual void compact( float threshold = 0.1f );
50 
52  ZEROBUF_API Zerobuf& operator = ( const Zerobuf& rhs );
53 
55  ZEROBUF_API Zerobuf( Zerobuf&& rhs );
56 
58  ZEROBUF_API Zerobuf& operator = ( Zerobuf&& rhs );
59 
61  ZEROBUF_API bool operator == ( const Zerobuf& rhs ) const;
62 
64  ZEROBUF_API bool operator != ( const Zerobuf& rhs ) const;
65 
67  ZEROBUF_API void reset( AllocatorPtr allocator );
68 
70  ZEROBUF_API void check() const;
71 
72 protected:
73  ZEROBUF_API explicit Zerobuf( AllocatorPtr alloc ); // takes ownership of alloc
74  ZEROBUF_API virtual ~Zerobuf();
75 
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 private:
89  AllocatorPtr _allocator;
90 
91  Zerobuf() = delete;
92  Zerobuf( const Zerobuf& zerobuf ) = delete;
93 
94  ZEROBUF_API bool _fromBinary( const void* data, const size_t size ) final;
95  ZEROBUF_API Data _toBinary() const final;
96  ZEROBUF_API bool _fromJSON( const std::string& json ) final;
97  ZEROBUF_API std::string _toJSON() const final;
98 };
99 
100 inline std::ostream& operator << ( std::ostream& os, const Zerobuf& zerobuf )
101 {
102  return os << "\"" << zerobuf.getTypeName() << "\" : " << zerobuf.toJSON();
103 }
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
virtual size_t getZerobufStaticSize() const =0
virtual void notifyChanging()
Called if any data in this object is about to change.
Definition: Zerobuf.h:38
bool operator==(const Zerobuf &rhs) const
bool operator!=(const Zerobuf &rhs) const
Allocator base class and interface.
Definition: Allocator.h:20
Base class for all Zerobuf serializable objects.
Definition: Zerobuf.h:28
virtual size_t getZerobufNumDynamics() const =0
virtual void compact(float threshold=0.1f)
Remove unused holes from the zerobuf.