ZeroBuf  0.4.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:
31  ZEROBUF_API virtual ~Zerobuf();
32 
34  virtual size_t getZerobufStaticSize() const = 0;
35 
37  virtual size_t getZerobufNumDynamics() const = 0;
38 
48  ZEROBUF_API virtual void compact( float threshold = 0.1f );
49 
51  ZEROBUF_API Zerobuf& operator = ( const Zerobuf& rhs );
52 
54  ZEROBUF_API Zerobuf( Zerobuf&& rhs );
55 
57  ZEROBUF_API Zerobuf& operator = ( Zerobuf&& rhs );
58 
60  ZEROBUF_API bool operator == ( const Zerobuf& rhs ) const;
61 
63  ZEROBUF_API bool operator != ( const Zerobuf& rhs ) const;
64 
66  ZEROBUF_API void reset( AllocatorPtr allocator );
67 
69  ZEROBUF_API void check() const;
70 
71 protected:
72  ZEROBUF_API explicit Zerobuf( AllocatorPtr alloc ); // takes ownership of alloc
73 
75  ZEROBUF_API virtual void notifyChanged() {}
76 
77  // used by generated ZeroBuf objects
78  ZEROBUF_API const Allocator& getAllocator() const;
79  ZEROBUF_API Allocator& getAllocator();
80 
81  ZEROBUF_API void _copyZerobufArray( const void* data, size_t size,
82  size_t arrayNum );
83 
84  ZEROBUF_API virtual void _parseJSON( const Json::Value& json );
85  ZEROBUF_API virtual void _createJSON( Json::Value& json ) const;
86  ZEROBUF_API friend void fromJSON( const Json::Value&,Zerobuf& );
87  ZEROBUF_API friend void toJSON( const Zerobuf&, Json::Value& );
88 
89  ZEROBUF_API bool _fromBinary( const void* data, const size_t size ) override;
90 
91 private:
92  AllocatorPtr _allocator;
93 
94  Zerobuf() = delete;
95  Zerobuf( const Zerobuf& zerobuf ) = delete;
96 
97  ZEROBUF_API Data _toBinary() const final;
98  ZEROBUF_API bool _fromJSON( const std::string& json ) final;
99  ZEROBUF_API std::string _toJSON() const final;
100 };
101 
102 inline std::ostream& operator << ( std::ostream& os, const Zerobuf& zerobuf )
103 {
104  return os << "\"" << zerobuf.getTypeName() << "\" : " << zerobuf.toJSON();
105 }
106 
107 }
108 
109 #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:28
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.