Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Buffer.h
1 /*
2  * Buffer class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2018, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_UTIL_BUFFER_H_
22 #define ELM_UTIL_BUFFER_H_
23 
24 #include <elm/array.h>
25 #include <elm/types.h>
26 #include <elm/util/Exception.h>
27 
28 namespace elm {
29 
30 class BufferException: public Exception { };
31 
32 class Buffer {
33 public:
34  static const t::size default_size = 4096;
35 
37  _size(size), _buf(new char[size]), _free(true) { }
38  inline Buffer(char *buf, t::size size)
39  : _size(size), _buf(buf), _free(false) { }
40  inline ~Buffer() { if(_free) delete [] _buf; }
41 
42  inline t::size size() const { return _size; }
43 
44  inline char operator[](t::size i) const
45  { check(i); return _buf[i]; }
46  inline char& operator[](t::size i)
47  { check(i); return _buf[i]; }
48 
49  inline void copyTo(void *p, t::size size, t::size offset = 0) const
50  { check(offset + size - 1); array::copy(static_cast<char *>(p), _buf + offset, _size); }
51  inline void copyFrom(void *p, t::size size, t::size offset = 0)
52  { check(offset + size - 1); array::copy(_buf + offset, static_cast<char *>(p), _size); }
53 
54  template <class F>
55  inline void read(F f, t::size size, t::size offset = 0) const
56  { check(offset + size - 1); f(static_cast<const char *>(_buf + offset), size); }
57  template <class F>
58  inline void write(F f, t::size size, t::size offset = 0)
59  { check(offset + size - 1); f(_buf + offset, size); }
60 
61 private:
62  inline void check(t::size i) const { if(i > _size) throw BufferException(); }
63 
64  t::size _size;
65  char *_buf;
66  bool _free;
67 };
68 
69 } // elm
70 
71 #endif /* ELM_UTIL_BUFFER_H_ */
elm::array::copy
void copy(T *target, const T *source, int size)
Definition: array.h:70
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::Buffer
Definition: Buffer.h:32
elm::Buffer::copyTo
void copyTo(void *p, t::size size, t::size offset=0) const
Definition: Buffer.h:49
elm::Buffer::read
void read(F f, t::size size, t::size offset=0) const
Definition: Buffer.h:55
elm::Exception
Definition: Exception.h:29
elm::Buffer::~Buffer
~Buffer()
Definition: Buffer.h:40
elm::Buffer::operator[]
char operator[](t::size i) const
Definition: Buffer.h:44
elm
Definition: adapter.h:26
elm::Buffer::size
t::size size() const
Definition: Buffer.h:42
elm::Buffer::operator[]
char & operator[](t::size i)
Definition: Buffer.h:46
elm::t::size
uint64 size
Definition: arch.h:35
elm::Buffer::default_size
static const t::size default_size
Definition: Buffer.h:34
elm::Buffer::write
void write(F f, t::size size, t::size offset=0)
Definition: Buffer.h:58
elm::Buffer::copyFrom
void copyFrom(void *p, t::size size, t::size offset=0)
Definition: Buffer.h:51
elm::Buffer::Buffer
Buffer(t::size size=default_size)
Definition: Buffer.h:36
elm::BufferException
Definition: Buffer.h:30
elm::Buffer::Buffer
Buffer(char *buf, t::size size)
Definition: Buffer.h:38
elm::t::offset
uint64 offset
Definition: arch.h:36