diff --git a/library/PolyVoxCore/include/PolyVoxImpl/ArraySizesImpl.h b/library/PolyVoxCore/include/PolyVoxImpl/ArraySizesImpl.h index 7a155ca2..0471ce36 100644 --- a/library/PolyVoxCore/include/PolyVoxImpl/ArraySizesImpl.h +++ b/library/PolyVoxCore/include/PolyVoxImpl/ArraySizesImpl.h @@ -32,6 +32,14 @@ distribution. namespace PolyVox { + /* + This class provides the implementation details behind ArraySizes. It is actually + quite similar to ArraySizes, but an important difference is that it is templatised + whereas ArraySizes is not. This allows us to use a recursive template pattern without + exposing the use of templates to the user. + + It is based on the following article: http://www.drdobbs.com/cpp/184401319 + */ template class ArraySizesImpl { diff --git a/library/PolyVoxCore/include/PolyVoxImpl/SubArray.h b/library/PolyVoxCore/include/PolyVoxImpl/SubArray.h index 4b2d0d49..f81e5cbb 100644 --- a/library/PolyVoxCore/include/PolyVoxImpl/SubArray.h +++ b/library/PolyVoxCore/include/PolyVoxImpl/SubArray.h @@ -34,6 +34,14 @@ namespace PolyVox { template class Array; + /* + This class forms part of the implementation of the Array class. The operator[] + return a SubArray of the next size down, so that multiple []'s can be chained + together. It is a seperate class from Array so that it can have a reduced interface, + and also so that it never takes ownership of the memory to which it points. + + It is based on the following article: http://www.drdobbs.com/cpp/184401319 + */ template class SubArray { @@ -41,30 +49,12 @@ namespace PolyVox friend class SubArray; public: - SubArray operator [](uint32_t uIndex) - { - assert(uIndex(&m_pElements[uIndex*m_pOffsets[0]], - m_pDimensions+1, m_pOffsets+1); - } + SubArray operator [](uint32_t uIndex); - const SubArray operator [](uint32_t uIndex) const - { - assert(uIndex(&m_pElements[uIndex*m_pOffsets[0]], - m_pDimensions+1, m_pOffsets+1); - } + const SubArray operator [](uint32_t uIndex) const; private: - SubArray(ElementType * pElements, uint32_t * pDimensions, uint32_t * pOffsets) - :m_pElements(pElements) - ,m_pDimensions(pDimensions) - ,m_pOffsets(pOffsets) - ,m_uNoOfElements(0) - { - } + SubArray(ElementType * pElements, uint32_t * pDimensions, uint32_t * pOffsets); uint32_t * m_pDimensions; uint32_t * m_pOffsets; @@ -79,24 +69,12 @@ namespace PolyVox friend class SubArray<2, ElementType>; public: - ElementType & operator [] (uint32_t uIndex) - { - assert(uIndex(ElementType * pElements, uint32_t * pDimensions, uint32_t * /*pOffsets*/) - :m_pDimensions(pDimensions) - ,m_pElements(pElements) - { - } + SubArray<1, ElementType>(ElementType * pElements, uint32_t * pDimensions, uint32_t * /*pOffsets*/); uint32_t * m_pDimensions; ElementType * m_pElements; diff --git a/library/PolyVoxCore/include/PolyVoxImpl/SubArray.inl b/library/PolyVoxCore/include/PolyVoxImpl/SubArray.inl index e69de29b..687773d7 100644 --- a/library/PolyVoxCore/include/PolyVoxImpl/SubArray.inl +++ b/library/PolyVoxCore/include/PolyVoxImpl/SubArray.inl @@ -0,0 +1,76 @@ +#pragma region License +/******************************************************************************* +Copyright (c) 2005-2009 David Williams + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not +claim that you wrote the original software. If you use this software +in a product, an acknowledgment in the product documentation would be +appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and must not be +misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*******************************************************************************/ +#pragma endregion + +namespace PolyVox +{ + template + SubArray SubArray::operator[](uint32_t uIndex) + { + assert(uIndex(&m_pElements[uIndex*m_pOffsets[0]], + m_pDimensions+1, m_pOffsets+1); + } + + template + const SubArray SubArray::operator[](uint32_t uIndex) const + { + assert(uIndex(&m_pElements[uIndex*m_pOffsets[0]], + m_pDimensions+1, m_pOffsets+1); + } + + template + SubArray::SubArray(ElementType * pElements, uint32_t * pDimensions, uint32_t * pOffsets) + :m_pElements(pElements) + ,m_pDimensions(pDimensions) + ,m_pOffsets(pOffsets) + ,m_uNoOfElements(0) + { + } + + + template + ElementType& SubArray<1, ElementType>::operator[] (uint32_t uIndex) + { + assert(uIndex + const ElementType& SubArray<1, ElementType>::operator[] (uint32_t uIndex) const + { + assert(uIndex + SubArray<1, ElementType>::SubArray<1, ElementType>(ElementType * pElements, uint32_t * pDimensions, uint32_t * /*pOffsets*/) + :m_pDimensions(pDimensions) + ,m_pElements(pElements) + { + } +}//namespace PolyVox \ No newline at end of file