1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCollectionRange.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#ifndef vtkCollectionRange_h
#define vtkCollectionRange_h
#include "vtkCollection.h"
#include "vtkMeta.h"
#include "vtkRange.h"
#include "vtkSmartPointer.h"
#include <cassert>
namespace vtk
{
namespace detail
{
VTK_ABI_NAMESPACE_BEGIN
template <typename CollectionType>
struct CollectionRange;
template <typename CollectionType>
struct CollectionIterator;
//------------------------------------------------------------------------------
// Detect vtkCollection types
template <typename T>
struct IsCollection : std::is_base_of<vtkCollection, T>
{
};
template <typename CollectionType, typename T = CollectionType>
using EnableIfIsCollection = typename std::enable_if<IsCollection<CollectionType>::value, T>::type;
//------------------------------------------------------------------------------
// Detect the type of items held by the collection by checking the return type
// of GetNextItem(), or GetNextItemAsObject() as a fallback.
template <typename CollectionType>
struct GetCollectionItemType
{
static_assert(IsCollection<CollectionType>::value, "Invalid vtkCollection subclass.");
private:
// The GetType methods are only used in a decltype context and are left
// unimplemented as we only care about their signatures. They are used to
// determine the type of object held by the collection.
//
// By passing literal 0 as the argument, the overload taking `int` is
// preferred and returns the same type as CollectionType::GetNextItem, which
// is usually the exact type held by the collection (e.g.
// vtkRendererCollection::GetNextItem returns vtkRenderer*).
//
// If the collection class does not define GetNextItem, SFINAE removes the
// preferred `int` overload, and the `...` overload is used instead. This
// method returns the same type as vtkCollection::GetNextItemAsObject, which
// is vtkObject*. This lets us define a more derived collection item type
// when possible, while falling back to the general vtkObject if a more
// refined type is not known.
// not implemented
template <typename T>
static auto GetType(...) -> decltype(std::declval<T>().GetNextItemAsObject());
// not implemented
template <typename T>
static auto GetType(int) -> decltype(std::declval<T>().GetNextItem());
using PointerType = decltype(GetType<CollectionType>(0));
public:
// Just use std::remove pointer, vtk::detail::StripPointer is overkill.
using Type = typename std::remove_pointer<PointerType>::type;
};
//------------------------------------------------------------------------------
// Collection iterator. Reference, value, and pointer types are all ItemType
// pointers, since:
// a) values: ItemType* instead of ItemType because vtkObjects can't be
// copied/assigned.
// b) references: No good usecase to change the pointers held by the collection
// by returning ItemType*&, nor would returning ItemType& be useful, since
// it'd have to be dereferenced anyway to pass it anywhere, and vtkObjects
// are conventionally held by address.
// c) pointers: Returning ItemType** from operator-> would be useless.
//
// There are no const_reference, etc, since VTK is not const correct and marking
// vtkObjects consts makes them unusable.
template <typename CollectionType>
struct CollectionIterator
{
static_assert(IsCollection<CollectionType>::value, "Invalid vtkCollection subclass.");
private:
using ItemType = typename GetCollectionItemType<CollectionType>::Type;
public:
using iterator_category = std::forward_iterator_tag;
using value_type = typename GetCollectionItemType<CollectionType>::Type*;
using difference_type = int;
using pointer = typename GetCollectionItemType<CollectionType>::Type*;
using reference = typename GetCollectionItemType<CollectionType>::Type*;
CollectionIterator() noexcept
: Element(nullptr)
{
}
CollectionIterator(const CollectionIterator& o) noexcept = default;
CollectionIterator& operator=(const CollectionIterator& o) noexcept = default;
CollectionIterator& operator++() noexcept // prefix
{
this->Increment();
return *this;
}
CollectionIterator operator++(int) noexcept // postfix
{
auto elem = this->Element;
this->Increment();
return CollectionIterator{ elem };
}
reference operator*() const noexcept { return this->GetItem(); }
pointer operator->() const noexcept { return this->GetItem(); }
friend bool operator==(const CollectionIterator& lhs, const CollectionIterator& rhs) noexcept
{
return lhs.Element == rhs.Element;
}
friend bool operator!=(const CollectionIterator& lhs, const CollectionIterator& rhs) noexcept
{
return lhs.Element != rhs.Element;
}
friend void swap(CollectionIterator& lhs, CollectionIterator& rhs) noexcept
{
using std::swap;
swap(lhs.Element, rhs.Element);
}
friend struct CollectionRange<CollectionType>;
protected:
CollectionIterator(vtkCollectionElement* element) noexcept
: Element(element)
{
}
private:
void Increment() noexcept
{ // incrementing an invalid iterator is UB, no need to check for non-null.
this->Element = this->Element->Next;
}
ItemType* GetItem() const noexcept { return static_cast<ItemType*>(this->Element->Item); }
vtkCollectionElement* Element;
};
//------------------------------------------------------------------------------
// Collection range proxy.
// The const_iterators/references are the same as the non-const versions, since
// vtkObjects marked const are unusable.
template <typename CollectionType>
struct CollectionRange
{
static_assert(IsCollection<CollectionType>::value, "Invalid vtkCollection subclass.");
using ItemType = typename GetCollectionItemType<CollectionType>::Type;
// NOTE: The const items are the same as the mutable ones, since const
// vtkObjects are generally unusable.
using size_type = int; // int is used by the vtkCollection API.
using iterator = CollectionIterator<CollectionType>;
using const_iterator = CollectionIterator<CollectionType>;
using reference = ItemType*;
using const_reference = ItemType*;
using value_type = ItemType*;
CollectionRange(CollectionType* coll) noexcept
: Collection(coll)
{
assert(this->Collection);
}
CollectionType* GetCollection() const noexcept { return this->Collection; }
size_type size() const noexcept { return this->Collection->GetNumberOfItems(); }
iterator begin() const
{
vtkCollectionSimpleIterator cookie;
this->Collection->InitTraversal(cookie);
// The cookie is a linked list node pointer, vtkCollectionElement:
return iterator{ static_cast<vtkCollectionElement*>(cookie) };
}
iterator end() const { return iterator{ nullptr }; }
// Note: These return mutable objects because const vtkObject are unusable.
const_iterator cbegin() const
{
vtkCollectionSimpleIterator cookie;
this->Collection->InitTraversal(cookie);
// The cookie is a linked list node pointer, vtkCollectionElement:
return const_iterator{ static_cast<vtkCollectionElement*>(cookie) };
}
// Note: These return mutable objects because const vtkObjects are unusable.
const_iterator cend() const { return const_iterator{ nullptr }; }
private:
vtkSmartPointer<CollectionType> Collection;
};
VTK_ABI_NAMESPACE_END
}
} // end namespace vtk::detail
#endif // vtkCollectionRange_h
// VTK-HeaderTest-Exclude: vtkCollectionRange.h
|