PolyVox  0.2.1
Open source voxel management library
SubArray.h
Go to the documentation of this file.
1 /*******************************************************************************
2 Copyright (c) 2005-2009 David Williams
3 
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7 
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11 
12  1. The origin of this software must not be misrepresented; you must not
13  claim that you wrote the original software. If you use this software
14  in a product, an acknowledgment in the product documentation would be
15  appreciated but is not required.
16 
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19 
20  3. This notice may not be removed or altered from any source
21  distribution.
22 *******************************************************************************/
23 
24 #ifndef __PolyVox_SubArray_H__
25 #define __PolyVox_SubArray_H__
26 
28 
29 namespace PolyVox
30 {
31  template <uint32_t noOfDims, typename ElementType> class Array;
32 
33  /*
34  This class forms part of the implementation of the Array class. The operator[]
35  return a SubArray of the next size down, so that multiple []'s can be chained
36  together. It is a seperate class from Array so that it can have a reduced interface,
37  and also so that it never takes ownership of the memory to which it points.
38 
39  It is based on the following article: http://www.drdobbs.com/cpp/184401319
40  */
41  template <uint32_t noOfDims, typename ElementType>
42  class SubArray
43  {
44  friend class Array<noOfDims+1, ElementType>;
45  friend class SubArray<noOfDims+1, ElementType>;
46 
47  public:
48  SubArray<noOfDims-1, ElementType> operator [](uint32_t uIndex);
49 
50  const SubArray<noOfDims-1, ElementType> operator [](uint32_t uIndex) const;
51 
52  private:
53  SubArray<noOfDims, ElementType>(ElementType * pElements, uint32_t * pDimensions, uint32_t * pOffsets);
54 
55  uint32_t * m_pDimensions;
56  uint32_t * m_pOffsets;
57  uint32_t m_uNoOfElements;
58  ElementType * m_pElements;
59  };
60 
61  template <typename ElementType>
62  class SubArray<1, ElementType>
63  {
64  friend class Array<2, ElementType>;
65  friend class SubArray<2, ElementType>;
66 
67  public:
68  ElementType & operator [] (uint32_t uIndex);
69 
70  const ElementType & operator [] (uint32_t uIndex) const;
71 
72  private:
73  SubArray<1, ElementType>(ElementType * pElements, uint32_t * pDimensions, uint32_t * /*pOffsets*/);
74 
75  uint32_t * m_pDimensions;
76  ElementType * m_pElements;
77  };
78 
79  template <typename ElementType>
80  class SubArray<0, ElementType>
81  {
82  //Zero dimensional subarray is meaningless.
83  };
84 }//namespace PolyVox
85 
87 
88 #endif //__PolyVox_SubArray_H__