1 /*========================================================================= 2 * 3 * Copyright Insight Software Consortium 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0.txt 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 *=========================================================================*/ 18 #ifndef itkRingBuffer_h 19 #define itkRingBuffer_h 20 21 #include "itkObject.h" 22 #include "itkObjectFactory.h" 23 #include "itkIntTypes.h" 24 25 namespace itk 26 { 27 /** \class RingBuffer 28 * \brief Templated ring buffer for holding anything 29 * 30 * This ring buffer can hold any type of itk class that supports smart 31 * pointers. A HEAD pointer is maintained and the buffer pointers can be 32 * accessed in order using either positive or negative offsets. The HEAD 33 * pointer can also be moved forward or backward in the ring. 34 * 35 * \ingroup ITKVideoCore 36 */ 37 38 template< typename TElement > 39 class ITK_TEMPLATE_EXPORT RingBuffer : public Object 40 { 41 public: 42 ITK_DISALLOW_COPY_AND_ASSIGN(RingBuffer); 43 44 /**-TYPEDEFS---------------------------------------------------------------*/ 45 46 /** Standard class type aliases */ 47 using Self = RingBuffer; 48 using Superclass = Object; 49 using Pointer = SmartPointer< Self >; 50 using ConstPointer = SmartPointer< const Self >; 51 52 /** Contained type */ 53 using ElementType = TElement; 54 using ElementPointer = typename ElementType::Pointer; 55 56 using SizeValueType = ::itk::SizeValueType; 57 using OffsetValueType = ::itk::OffsetValueType; 58 59 /** Method for creation through the object factory. */ 60 itkNewMacro(Self); 61 62 /** Run-time type information (and related methods). */ 63 itkTypeMacro(RingBuffer, Object); 64 65 /** Set the number of buffers 66 * WARNING: If the size is set smaller than the current buffer size, the tail 67 * of the buffer will be chopped off */ 68 void SetNumberOfBuffers(SizeValueType sz); 69 70 /** Get the buffer size */ 71 SizeValueType GetNumberOfBuffers(); 72 73 /** Move the Head pointer along the ring using the given offset */ 74 void MoveHead(OffsetValueType offset); 75 76 /** Convenience methods for moving Head +/- 1 */ 77 void MoveHeadForward(); 78 void MoveHeadBackward(); 79 80 /** Report whether or not the indicated buffer is full */ 81 bool BufferIsFull(OffsetValueType offset); 82 83 /** Report the current position of Head (mostly used for testing) */ GetHeadIndex()84 SizeValueType GetHeadIndex() { 85 return this->m_HeadIndex; 86 } 87 88 /** Access the data from the indicated buffer */ 89 typename ElementType::Pointer GetBufferContents(OffsetValueType offset); 90 91 /** Set the buffer contents of a buffer */ 92 void SetBufferContents(OffsetValueType offset, ElementPointer element); 93 94 protected: 95 96 /**-PROTECTED METHODS------------------------------------------------------*/ 97 RingBuffer(); 98 ~RingBuffer() override = default; 99 void PrintSelf(std::ostream &os, Indent indent) const override; 100 101 /** Get the proper buffer index from an offset */ 102 OffsetValueType GetOffsetBufferIndex(OffsetValueType offset); 103 104 /**-PROTECTED MEMBERS------------------------------------------------------*/ 105 106 /** Pointer to the current active buffer */ 107 SizeValueType m_HeadIndex{0}; 108 109 /** Vector of pointers to elements */ 110 std::vector<ElementPointer> m_PointerVector; 111 }; // end RingBuffer class 112 113 } // end namespace itk 114 115 #ifndef ITK_MANUAL_INSTANTIATION 116 #include "itkRingBuffer.hxx" 117 #endif 118 119 #endif 120