OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
value_holder.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 // Copyright (C) 2003-2010 by the IPLT authors
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 3.0 of the License, or (at your option)
10 // any later version.
11 // This library is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 // details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this library; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 //------------------------------------------------------------------------------
20 
21 /*
22  value holder for image state
23 
24  Author: Ansgar Philippsen
25 */
26 
27 #ifndef VALUE_HOLDER_H
28 #define VALUE_HOLDER_H
29 
30 #include <ost/base.hh>
31 #include <ost/img/module_config.hh>
32 #include <ost/img/data_types.hh>
33 #include <ost/img/size.hh>
34 
35 #include "type_fw.hh"
36 
37 namespace value_holder_test {
38 
39 template <typename V>
41 
42 }
43 
44 namespace ost { namespace img { namespace image_state {
45 
46 /*
47  Value Holder
48 
49  Provide a dynamically allocated 3D array with
50  optimized access, based on a zero based Index, which
51  in turn encapsulates an unsigned integer triplet
52 
53  Should provide reasonable exception safety, ie an exception
54  during construction will not cause a memory leak.
55 */
56 template <typename V>
57 class TEMPLATE_EXPORT ValueHolder {
58 public:
59  typedef V* VPtr;
60  typedef VPtr* VPtrPtr;
61 
65 
66  ValueHolder(unsigned int wi, unsigned int he, unsigned int de);
67 
69  ValueHolder(const Size& s);
70 
72  ValueHolder(const Size& s, const Size& ps);
73 
75  ValueHolder(const ValueHolder<V>& h);
76 
78  template <typename U>
80  width_(s[0]),
81  height_(s[1]),
82  depth_(s[2]),
83  volume_(width_*height_*depth_),
84  volume2_(v.GetPhysicalSize()*sizeof(U)/sizeof(V)),
85  data_(reinterpret_cast<V*>(v.ReleaseData())),
86  row_(0),
87  slice_(0)
88  {
89  try {
90  setup();
91  } catch(...) {
92  delete []data_;
93  }
94  }
95 
96 
98 
103  ValueHolder& operator=(const ValueHolder<V>& h);
104 
106  ~ValueHolder();
107 
109 
115  VPtr ReleaseData();
116 
118  void Swap(ValueHolder& vh);
120 
122 
123 
124  // retrieve width, at least 1
125  unsigned int GetWidth() const {return width_;}
126  // retrieve height, at least 1
127  unsigned int GetHeight() const {return height_;}
128  // retrieve depth, at least 1
129  unsigned int GetDepth() const {return depth_;}
130  // retrieve overall size (width*height*depth), at least 1
131  unsigned int GetVolume() const {return volume_;}
132  // retrieve dimensions as Size object
133  Size GetSize() const;
134 
135  static DataType GetDataType();
136 
137  unsigned int GetPhysicalSize() const {return volume2_;}
138 
139  long MemSize() const;
140 
142 
149 
150 
154  V& Value(const Index& i);
155 
157 
161  const V& Value(const Index& i) const;
162 
164 
167  V& Value(unsigned int i);
168 
170 
173  const V& Value(unsigned int i) const;
174 
175 
177  VPtr GetData() {return data_;}
179  const VPtr GetData() const {return data_;}
180 
182  int DataCount() const {return volume_;}
183 
185 
188  int GetDataCount() const {return volume_;}
189 
190  const VPtr GetEnd() const {return &data_[volume_];}
191 
193  VPtr* GetRows() {return row_;}
195  const VPtr* GetRows() const {return row_;}
197 
200  int GetRowCount() const {return width_*height_;}
201 
203  VPtrPtr* GetSlices() {return slice_;}
205  const VPtrPtr* GetSlices() const {return slice_;}
207 
210  int GetSliceCount() const {return width_;}
211 
213 private:
214  unsigned int width_, height_, depth_, volume_;
215  // this is a hack to get padding for fftw to work...
216  unsigned int volume2_;
217 
218  // actual data storage
219  V* data_;
220  // optimization
221  V** row_;
222  // optimization
223  V*** slice_;
224 
225  void setup();
226  void clear();
227 };
228 
229 }}} // namespaces
230 
231 #endif