Schnek
databuffer.hpp
1 /*
2  * databuffer.hpp
3  *
4  * Created on: 16 Apr 2013
5  * Author: Holger Schmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 
28 #ifndef SCHNEK_DATABUFFER_HPP_
29 #define SCHNEK_DATABUFFER_HPP_
30 
31 #include "../grid/grid.hpp"
32 #include "../grid/gridstorage.hpp"
33 #include "../grid/gridcheck.hpp"
34 
35 namespace schnek {
36 template<class T>
38 {
39  public:
40  //typedef Grid<unsigned char, 1, GridAssertCheck, LazyArrayGridStorage> BufferType;
42  private:
43  BufferType buffer;
44  public:
45  class iterator : public std::iterator<std::forward_iterator_tag, T>
46  {
47  private:
48  friend class DataBuffer;
49  unsigned char *data;
50 
51  iterator(unsigned char *data_) : data(data_) {}
52 
53  public:
54  iterator() : data(NULL) {}
55 
56  iterator(const iterator &it) { data = it.data; }
57 
58  iterator& operator++()
59  {
60  data += sizeof(T);
61  return *this;
62  }
63 
64  iterator operator++(int)
65  {
66  iterator tmp(*this);
67  operator++();
68  return tmp;
69  }
70 
71  bool operator==(const iterator& rhs)
72  {
73  return (data == rhs.data);
74  }
75 
76  bool operator!=(const iterator& rhs)
77  {
78  return (data != rhs.data);
79  }
80 
81  T& operator*()
82  {
83  return *((T*)(data));
84  }
85 
86  T* operator->()
87  {
88  return (T*)(data);
89  }
90  };
91 
98  template<class ContainerType>
99  void makeBuffer(ContainerType &container);
100 
101  BufferType &getBuffer()
102  {
103  return buffer;
104  }
105 
106  iterator begin()
107  {
108  return iterator(buffer.getRawData());
109  }
110 
111  iterator end()
112  {
113  return iterator(buffer.getRawData() + buffer.getSize());
114  }
115 
116 };
117 
118 } // namespace schnek
119 
120 #include "databuffer.t"
121 
122 #endif
Definition: databuffer.hpp:45
Definition: databuffer.hpp:37
Definition: algo.hpp:30
void makeBuffer(ContainerType &container)