1 /*
2  * Copyright (C) 2017 Matthieu Gautier <mgautier@kymeria.fr>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
11  * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
12  * NON-INFRINGEMENT.  See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  */
19 
20 #ifndef ZIM_BUFFER_H_
21 #define ZIM_BUFFER_H_
22 
23 #include <cstddef>
24 #include <exception>
25 #include <memory>
26 #include <iostream>
27 
28 #include "config.h"
29 #include "zim_types.h"
30 #include "endian_tools.h"
31 #include "debug.h"
32 #include <zim/blob.h>
33 
34 namespace zim {
35 
36 class Buffer {
37   public: // types
38     typedef std::shared_ptr<const char> DataPtr;
39 
40   public: // functions
41     static const Buffer makeBuffer(const char* data, zsize_t size);
42     static const Buffer makeBuffer(const DataPtr& data, zsize_t size);
43     static Buffer makeBuffer(zsize_t size);
44 
45     const char* data(offset_t offset=offset_t(0)) const;
46 
at(offset_t offset)47     char at(offset_t offset) const {
48       return *(data(offset));
49     }
size()50     zsize_t size() const { return m_size; }
51     const Buffer sub_buffer(offset_t offset, zsize_t size) const;
Blob()52     operator Blob() const { return Blob(m_data, m_size.v); }
53 
54   private: // functions
55     Buffer(const DataPtr& data, zsize_t size);
56 
57   private: // data
58     zsize_t m_size;
59     DataPtr m_data;
60 };
61 
62 } // zim namespace
63 
64 #endif //ZIM_BUFFER_H_
65