From 51f0e9bd0cb456b26be5b119a93ea587e732c6ff Mon Sep 17 00:00:00 2001 From: Adam Rankin Date: Fri, 11 Aug 2017 00:07:47 -0400 Subject: [PATCH] ENH: Adding support for C++11 range based for loops for polydata message --- Source/igtlPolyDataMessage.cxx | 210 ++++++++++++++++++++++++++++++++- Source/igtlPolyDataMessage.h | 73 +++++++++++- 2 files changed, 278 insertions(+), 5 deletions(-) diff --git a/Source/igtlPolyDataMessage.cxx b/Source/igtlPolyDataMessage.cxx index 879d0bd5..b50f9b4b 100644 --- a/Source/igtlPolyDataMessage.cxx +++ b/Source/igtlPolyDataMessage.cxx @@ -137,6 +137,75 @@ int PolyDataPointArray::GetPoint(unsigned int id, igtlFloat32 * point) return 1; } +/// Implement support for C++11 ranged for loops +std::vector::iterator PolyDataPointArray::begin() +{ + return m_Data.begin(); +} +std::vector::iterator PolyDataPointArray::end() +{ + return m_Data.end(); +} +std::vector::const_iterator PolyDataPointArray::begin() const +{ + return m_Data.begin(); +} +std::vector::const_iterator PolyDataPointArray::end() const +{ + return m_Data.end(); +} + +std::vector::reverse_iterator PolyDataPointArray::rbegin() +{ + return m_Data.rbegin(); +} +std::vector::reverse_iterator PolyDataPointArray::rend() +{ + return m_Data.rend(); +} +std::vector::const_reverse_iterator PolyDataPointArray::rbegin() const +{ + return m_Data.rbegin(); +} +std::vector::const_reverse_iterator PolyDataPointArray::rend() const +{ + return m_Data.rend(); +} + +/// Implement support for C++11 ranged for loops +std::vector::iterator begin(PolyDataPointArray& list) +{ + return list.begin(); +} +std::vector::iterator end(PolyDataPointArray& list) +{ + return list.end(); +} +std::vector::const_iterator begin(const PolyDataPointArray& list) +{ + return list.begin(); +} +std::vector::const_iterator end(const PolyDataPointArray& list) +{ + return list.end(); +} + +std::vector::reverse_iterator rbegin(PolyDataPointArray& list) +{ + return list.rbegin(); +} +std::vector::reverse_iterator rend(PolyDataPointArray& list) +{ + return list.rend(); +} +std::vector::const_reverse_iterator rbegin(const PolyDataPointArray& list) +{ + return list.rbegin(); +} +std::vector::const_reverse_iterator rend(const PolyDataPointArray& list) +{ + return list.rend(); +} // Description: // PolyDataCellArray class to pass vertices, lines, polygons, and triangle strips @@ -172,7 +241,7 @@ void PolyDataCellArray::AddCell(int n, igtlUint32 * cell) } } -void PolyDataCellArray::AddCell(std::list cell) +void PolyDataCellArray::AddCell(const Cell& cell) { this->m_Data.push_back(cell); } @@ -238,6 +307,75 @@ int PolyDataCellArray::GetCell(unsigned int id, std::list& cell) return 1; } +/// Implement support for C++11 ranged for loops +std::vector::iterator PolyDataCellArray::begin() +{ + return m_Data.begin(); +} +std::vector::iterator PolyDataCellArray::end() +{ + return m_Data.end(); +} +std::vector::const_iterator PolyDataCellArray::begin() const +{ + return m_Data.begin(); +} +std::vector::const_iterator PolyDataCellArray::end() const +{ + return m_Data.end(); +} + +std::vector::reverse_iterator PolyDataCellArray::rbegin() +{ + return m_Data.rbegin(); +} +std::vector::reverse_iterator PolyDataCellArray::rend() +{ + return m_Data.rend(); +} +std::vector::const_reverse_iterator PolyDataCellArray::rbegin() const +{ + return m_Data.rbegin(); +} +std::vector::const_reverse_iterator PolyDataCellArray::rend() const +{ + return m_Data.rend(); +} + +/// Implement support for C++11 ranged for loops +std::vector::iterator begin(PolyDataCellArray& list) +{ + return list.begin(); +} +std::vector::iterator end(PolyDataCellArray& list) +{ + return list.end(); +} +std::vector::const_iterator begin(const PolyDataCellArray& list) +{ + return list.begin(); +} +std::vector::const_iterator end(const PolyDataCellArray& list) +{ + return list.end(); +} + +std::vector::reverse_iterator rbegin(PolyDataCellArray& list) +{ + return list.rbegin(); +} +std::vector::reverse_iterator rend(PolyDataCellArray& list) +{ + return list.rend(); +} +std::vector::const_reverse_iterator rbegin(const PolyDataCellArray& list) +{ + return list.rbegin(); +} +std::vector::const_reverse_iterator rend(const PolyDataCellArray& list) +{ + return list.rend(); +} // Description: // Attribute class used for passing attribute data @@ -418,6 +556,76 @@ int PolyDataAttribute::GetNthData(unsigned int n, igtlFloat32 * data) return 1; } +/// Implement support for C++11 ranged for loops +std::vector::iterator PolyDataAttribute::begin() +{ + return m_Data.begin(); +} +std::vector::iterator PolyDataAttribute::end() +{ + return m_Data.end(); +} +std::vector::const_iterator PolyDataAttribute::begin() const +{ + return m_Data.begin(); +} +std::vector::const_iterator PolyDataAttribute::end() const +{ + return m_Data.end(); +} + +std::vector::reverse_iterator PolyDataAttribute::rbegin() +{ + return m_Data.rbegin(); +} +std::vector::reverse_iterator PolyDataAttribute::rend() +{ + return m_Data.rend(); +} +std::vector::const_reverse_iterator PolyDataAttribute::rbegin() const +{ + return m_Data.rbegin(); +} +std::vector::const_reverse_iterator PolyDataAttribute::rend() const +{ + return m_Data.rend(); +} + +/// Implement support for C++11 ranged for loops +std::vector::iterator begin(PolyDataAttribute& list) +{ + return list.begin(); +} +std::vector::iterator end(PolyDataAttribute& list) +{ + return list.end(); +} +std::vector::const_iterator begin(const PolyDataAttribute& list) +{ + return list.begin(); +} +std::vector::const_iterator end(const PolyDataAttribute& list) +{ + return list.end(); +} + +std::vector::reverse_iterator rbegin(PolyDataAttribute& list) +{ + return list.rbegin(); +} +std::vector::reverse_iterator rend(PolyDataAttribute& list) +{ + return list.rend(); +} +std::vector::const_reverse_iterator rbegin(const PolyDataAttribute& list) +{ + return list.rbegin(); +} +std::vector::const_reverse_iterator rend(const PolyDataAttribute& list) +{ + return list.rend(); +} + // Description: // PolyDataMessage class implementation PolyDataMessage::PolyDataMessage() diff --git a/Source/igtlPolyDataMessage.h b/Source/igtlPolyDataMessage.h index 6fa2f015..b75a99be 100644 --- a/Source/igtlPolyDataMessage.h +++ b/Source/igtlPolyDataMessage.h @@ -172,16 +172,40 @@ class IGTLCommon_EXPORT PolyDataPointArray : public Object { /// Gets the coordinates of the point specified by 'id' int GetPoint(unsigned int id, igtlFloat32 * point); + /// Implement support for C++11 ranged for loops + std::vector::iterator begin(); + std::vector::iterator end(); + std::vector::const_iterator begin() const; + std::vector::const_iterator end() const; + + std::vector::reverse_iterator rbegin(); + std::vector::reverse_iterator rend(); + std::vector::const_reverse_iterator rbegin() const; + std::vector::const_reverse_iterator rend() const; + private: /// A list of the points. std::vector< Point > m_Data; }; +/// Implement support for C++11 ranged for loops +IGTLCommon_EXPORT std::vector::iterator begin(PolyDataPointArray& list); +IGTLCommon_EXPORT std::vector::iterator end(PolyDataPointArray& list); +IGTLCommon_EXPORT std::vector::const_iterator begin(const PolyDataPointArray& list); +IGTLCommon_EXPORT std::vector::const_iterator end(const PolyDataPointArray& list); + +IGTLCommon_EXPORT std::vector::reverse_iterator rbegin(PolyDataPointArray& list); +IGTLCommon_EXPORT std::vector::reverse_iterator rend(PolyDataPointArray& list); +IGTLCommon_EXPORT std::vector::const_reverse_iterator rbegin(const PolyDataPointArray& list); +IGTLCommon_EXPORT std::vector::const_reverse_iterator rend(const PolyDataPointArray& list); // The PolyDataCellArray class is used to pass vertices, lines, polygons, and triangle strips class IGTLCommon_EXPORT PolyDataCellArray : public Object { public: + + typedef std::list Cell; + enum { NULL_POINT = 0xFFFFFFFF, }; @@ -211,7 +235,7 @@ class IGTLCommon_EXPORT PolyDataCellArray : public Object { void AddCell(int n, igtlUint32 * cell); /// Adds an array of cells stored in 'cell'. - void AddCell(std::list cell); + void AddCell(const Cell& cell); /// GetTotalSize() returns the total memory size of the cell data array in /// POLYDATA message. Cell data array is array of cell data, consisting of @@ -232,13 +256,34 @@ class IGTLCommon_EXPORT PolyDataCellArray : public Object { int GetCell(unsigned int id, igtlUint32 * cell); /// Gets the cell specified by the 'id'. A list of points in the cell will be stored in the 'cell'. - int GetCell(unsigned int id, std::list& cell); + int GetCell(unsigned int id, Cell& cell); + + /// Implement support for C++11 ranged for loops + std::vector::iterator begin(); + std::vector::iterator end(); + std::vector::const_iterator begin() const; + std::vector::const_iterator end() const; + + std::vector::reverse_iterator rbegin(); + std::vector::reverse_iterator rend(); + std::vector::const_reverse_iterator rbegin() const; + std::vector::const_reverse_iterator rend() const; private: /// A lists of the cells. Each cell consists of multiple points. - std::vector< std::list > m_Data; + std::vector m_Data; }; +/// Implement support for C++11 ranged for loops +IGTLCommon_EXPORT std::vector::iterator begin(PolyDataCellArray& list); +IGTLCommon_EXPORT std::vector::iterator end(PolyDataCellArray& list); +IGTLCommon_EXPORT std::vector::const_iterator begin(const PolyDataCellArray& list); +IGTLCommon_EXPORT std::vector::const_iterator end(const PolyDataCellArray& list); + +IGTLCommon_EXPORT std::vector::reverse_iterator rbegin(PolyDataCellArray& list); +IGTLCommon_EXPORT std::vector::reverse_iterator rend(PolyDataCellArray& list); +IGTLCommon_EXPORT std::vector::const_reverse_iterator rbegin(const PolyDataCellArray& list); +IGTLCommon_EXPORT std::vector::const_reverse_iterator rend(const PolyDataCellArray& list); /// Attribute class used for passing attribute data. class IGTLCommon_EXPORT PolyDataAttribute : public Object { @@ -316,8 +361,18 @@ class IGTLCommon_EXPORT PolyDataAttribute : public Object { /// Gets the Nth data. int GetNthData(unsigned int n, igtlFloat32 * data); - private: + /// Implement support for C++11 ranged for loops + std::vector::iterator begin(); + std::vector::iterator end(); + std::vector::const_iterator begin() const; + std::vector::const_iterator end() const; + std::vector::reverse_iterator rbegin(); + std::vector::reverse_iterator rend(); + std::vector::const_reverse_iterator rbegin() const; + std::vector::const_reverse_iterator rend() const; + + private: /// The attribute type. igtlUint8 m_Type; @@ -335,6 +390,16 @@ class IGTLCommon_EXPORT PolyDataAttribute : public Object { }; +/// Implement support for C++11 ranged for loops +IGTLCommon_EXPORT std::vector::iterator begin(PolyDataAttribute& list); +IGTLCommon_EXPORT std::vector::iterator end(PolyDataAttribute& list); +IGTLCommon_EXPORT std::vector::const_iterator begin(const PolyDataAttribute& list); +IGTLCommon_EXPORT std::vector::const_iterator end(const PolyDataAttribute& list); + +IGTLCommon_EXPORT std::vector::reverse_iterator rbegin(PolyDataAttribute& list); +IGTLCommon_EXPORT std::vector::reverse_iterator rend(PolyDataAttribute& list); +IGTLCommon_EXPORT std::vector::const_reverse_iterator rbegin(const PolyDataAttribute& list); +IGTLCommon_EXPORT std::vector::const_reverse_iterator rend(const PolyDataAttribute& list); /// A class for the POLYDATA message type. class IGTLCommon_EXPORT PolyDataMessage: public MessageBase