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  Authors: Ansgar Philippsen, Andreas Schenk
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 "index.hh"
36 #include "type_fw.hh"
37 
38 namespace value_holder_test {
39 
40 template <typename V>
42 
43 }
44 
45 #define USE_ROW_ORDER 1
46 
47 
48 
49 namespace ost { namespace img { namespace image_state {
50 
51 /*
52  Value Holder
53 
54  Provide a dynamically allocated 3D array with
55  optimized access, based on a zero based Index, which
56  in turn encapsulates an unsigned integer triplet
57 
58  Should provide reasonable exception safety, ie an exception
59  during construction will not cause a memory leak.
60 */
61 template <typename V>
62 class TEMPLATE_EXPORT ValueHolder {
63 public:
64  typedef V* VPtr;
65  typedef const V* ConstVPtr;
66  typedef VPtr* VPtrPtr;
67 
71 
72  ValueHolder(const Size& s);
73 
75  ValueHolder(const Size& s, const Size& ps);
76 
78  ValueHolder(const ValueHolder<V>& h);
79 
80 
82 
87  ValueHolder& operator=(const ValueHolder<V>& h);
88 
90  ~ValueHolder();
91 
92 
94  void Swap(ValueHolder& vh);
96 
97  static DataType GetDataType();
98 
99  std::size_t MemSize() const;
100 
101 
102 
104 
108  V& Value(const Index& i)
109  {
110  #ifdef USE_ROW_ORDER
111  assert(i.w<depth_);
112  return data_[i.u*height_depth_ + i.v*depth_ +i.w];
113  #else
114  assert(i.v<height_);
115  return data_[i.w*width_height_ + i.v*height_ +i.u];
116  #endif
117  }
118 
120 
124  const V& Value(const Index& i) const
125  {
126  #ifdef USE_ROW_ORDER
127  assert(i.w<depth_);
128  return data_[i.u*height_depth_ + i.v*depth_ +i.w];
129  #else
130  assert(i.v<height_);
131  return data_[i.w*width_height_ + i.v*height_ +i.u];
132  #endif
133  }
134 
136  VPtr GetData() {return &data_[0];}
138  ConstVPtr GetData() const {return &data_[0];}
139 
140 
141  ConstVPtr GetEnd() const {return &data_[0]+data_.size();}
142 
143 
144 private:
145 #ifdef USE_ROW_ORDER
146  std::size_t depth_;
147  std::size_t height_depth_;
148 #else
149  std::size_t height_;
150  std::size_t width_height_;
151 #endif
152 
153  // actual data storage
154  std::vector<V> data_;
155 
156 };
157 
158 }}} // namespaces
159 
160 #endif