[Bf-blender-cvs] [d08fbfacb21] soc-2019-fast-io: [Fast import/export] Removed the boost::iterator_facade dependency for iterators

Hugo Sales noreply at git.blender.org
Wed Jun 19 18:20:59 CEST 2019


Commit: d08fbfacb211848729a8db18a8520dddee15a4e6
Author: Hugo Sales
Date:   Wed Jun 19 17:19:36 2019 +0100
Branches: soc-2019-fast-io
https://developer.blender.org/rBd08fbfacb211848729a8db18a8520dddee15a4e6

[Fast import/export] Removed the boost::iterator_facade dependency for iterators

===================================================================

M	source/blender/editors/io/intern/common.cpp
M	source/blender/editors/io/intern/iterators.hpp
M	source/blender/editors/io/intern/obj.cpp
M	source/blender/editors/io/intern/stl.cpp

===================================================================

diff --git a/source/blender/editors/io/intern/common.cpp b/source/blender/editors/io/intern/common.cpp
index 3f29341071a..a5085a303f2 100644
--- a/source/blender/editors/io/intern/common.cpp
+++ b/source/blender/editors/io/intern/common.cpp
@@ -231,6 +231,7 @@ bool export_end(bContext *UNUSED(C), ExportSettings *const settings)
   return true;
 }
 
+/* clang-format off */
 bool time_export(bContext *C,
                  ExportSettings *const settings,
                  void (*start)(bContext *C, ExportSettings *const settings),
@@ -239,9 +240,12 @@ bool time_export(bContext *C,
   auto f = std::chrono::steady_clock::now();
   start(C, settings);
   auto ret = end(C, settings);
-  std::cout << "Took " << (std::chrono::steady_clock::now() - f).count() << "ns\n";
+  std::cout << "Took "
+            << std::chrono::duration_cast<std::chrono::milliseconds>
+               (std::chrono::steady_clock::now() - f).count() << "ms\n";
   return ret;
 }
+/* clang-format on */
 
 const std::array<float, 3> calculate_normal(const Mesh *const mesh, const MPoly &mp)
 {
diff --git a/source/blender/editors/io/intern/iterators.hpp b/source/blender/editors/io/intern/iterators.hpp
index 4d41bc7bf8d..cbd825d37f4 100644
--- a/source/blender/editors/io/intern/iterators.hpp
+++ b/source/blender/editors/io/intern/iterators.hpp
@@ -29,408 +29,179 @@ extern "C" {
 
 namespace common {
 
-// /* clang-format off */
-// template<typename T>
-// struct pointer_iterator {
-//   using difference_type = ptrdiff_t;
-//   using value_type = T;
-//   using pointer    = T *;
-//   using reference  = T &;
-//   using iterator_category = std::random_access_iterator_tag;
-//   pointer_iterator() : first(nullptr), curr(nullptr), size(0) {}
-//   pointer_iterator(T *p) : curr(p), first(p), size(0) {}
-//   pointer_iterator(T *p, size_t size) : it(p), first(p), size(size) {}
-//   operator T *() const { return curr; }
-//   pointer_iterator &operator=(const pointer_iterator<T, iterator_category> &p)
-//   {
-//     // Placement new: construct a new object in the position of `this`
-//     // Doesn't actually allocate memory
-//     new (this) pointer_iterator(p);
-//     return *this;
-//   }
-//   pointer_iterator begin() const { return {first, size}; }
-//   pointer_iterator end()   const { return {first + size, size}; }
-//   pointer_iterator &operator++() { ++curr; }
-//   pointer_iterator &operator--() { --curr; }
-//   pointer_iterator &operator+(ptrdiff_t n) { curr += n; return *this; }
-//   ptrdiff_t operator-(const pointer_iterator &other) const { return other.curr - curr; }
-//   bool operator==(const pointer_iterator &other) const { return curr == other.curr; }
-//   const T & operator*() const { return *curr; }
-//   T *first;
-//   T *curr;
-//   size_t size;
-// };
-// /* clang-format on */
-
+/* clang-format off */
 // Adapt a pointer-size pair as a random access iterator
 // This makes use of `boost::iterator_facade` and makes it possible to use
 // for each style loops, as well as cleanly hiding how the underlying Blender
 // data structures are accessed
-template<typename SourceT, typename Tag = std::random_access_iterator_tag>
-struct pointer_iterator
-    : public boost::iterator_facade<pointer_iterator<SourceT, Tag>, SourceT &, Tag> {
-  pointer_iterator() : first(nullptr)
-  {
-  }
-  pointer_iterator(const pointer_iterator<SourceT, Tag> &) = default;
-  pointer_iterator(pointer_iterator<SourceT, Tag> &&) = default;
-  explicit pointer_iterator(SourceT *p) : it(p), first(p), size(0)
-  {
-  }
-  explicit pointer_iterator(SourceT *p, size_t size) : it(p), first(p), size(size)
-  {
-  }
-  operator SourceT *() const
-  {
-    return it;
-  }
-  pointer_iterator &operator=(const pointer_iterator<SourceT, Tag> &p)
-  {
+template<typename T, typename Tag = std::random_access_iterator_tag>
+struct pointer_iterator_base {
+  using difference_type = ptrdiff_t;
+  using value_type = T;
+  using pointer    = T *;
+  using reference  = T &;
+  using iterator_category = Tag;
+  pointer_iterator_base(pointer p, size_t size) : first(p), curr(p), size(size) {}
+  pointer_iterator_base(const pointer_iterator_base &pib) : first(pib.first), curr(pib.curr), size(pib.size) {}
+  operator pointer() const { return curr; }
+  pointer_iterator_base &operator=(const pointer_iterator_base &p) {
     // Placement new: construct a new object in the position of `this`
     // Doesn't actually allocate memory
-    new (this) pointer_iterator(p);
+    new (this) pointer_iterator_base(p);
     return *this;
   }
-  // pointer_iterator & operator=(pointer_iterator<SourceT, Tag> &&p) = default;//  {
-  // 	return pointer_iterator<SourceT, Tag>(p);
-  // }
-  pointer_iterator begin() const
-  {
-    return pointer_iterator{first, size};
-  }
-  const pointer_iterator cbegin() const
-  {
-    return pointer_iterator{first, size};
-  }
-  pointer_iterator end() const
-  {
-    return pointer_iterator{first + size, size};
-  }
-  const pointer_iterator cend() const
-  {
-    return pointer_iterator{first + size, size};
-  }
-  friend class boost::iterator_core_access;
-  void increment()
-  {
-    ++it;
-  }
-  void decrement()
-  {
-    --it;
-  }
-  void advance(ptrdiff_t n)
-  {
-    it += n;
-  }
-  ptrdiff_t distance_to(const pointer_iterator &other)
-  {
-    return other.it - it;
-  }
-  bool equal(const pointer_iterator &other) const
-  {
-    return it == other.it;
-  }
-  SourceT &dereference() const
-  {
-    return *this->it;
-  }
-  SourceT *it;
-  SourceT *const first;
+  pointer_iterator_base begin() const { return {this->first, this->size}; }
+  pointer_iterator_base end()   const { return {this->first + this->size, this->size}; }
+  pointer_iterator_base & operator++() { ++curr; return *this; }
+  pointer_iterator_base & operator--() { --curr; return *this; }
+  pointer_iterator_base & operator+(difference_type n) { curr += n; return *this; }
+  ptrdiff_t operator-(const pointer_iterator_base &other) const { return other.curr - curr; }
+  bool operator==(const pointer_iterator_base &other)     const { return curr == other.curr; }
+  pointer first;
+  pointer curr;
   size_t size;
 };
 
-// This is another iterator, whcih makes use of `boost::iterator_adaptor` to change  how the
-// underlying iterator behaves. Specifically, it uses the derived class' `dereference` method. This
-// makes use of CRTP, the curiously recurring template pattern This means that the base class has,
-// as a template paramater, the type of the derived class which means that it can hold a pointer to
-// said derived class and call it's `dereference` method.
-template<typename SourceT,
-         typename ResT,
-         typename CRTP,
-         typename Base = pointer_iterator<SourceT>,
-         typename Tag = typename std::iterator_traits<Base>::iterator_category>
-struct dereference_iterator
-    : public boost::iterator_adaptor<dereference_iterator<SourceT, ResT, CRTP, Base>,
-                                     Base,
-                                     ResT,
-                                     Tag,
-                                     ResT> {
-  using dereference_iterator_ = dereference_iterator<SourceT, ResT, CRTP, Base>;
-  dereference_iterator() : dereference_iterator::iterator_adaptor_(), crtp(nullptr)
-  {
-  }
-  dereference_iterator(const dereference_iterator &di, CRTP *crtp)
-      : dereference_iterator::iterator_adaptor_(di.base()), crtp(crtp)
-  {
-  }
-  dereference_iterator(dereference_iterator &&di, CRTP *crtp)
-      : dereference_iterator::iterator_adaptor_(di.base()), crtp(crtp)
-  {
-  }
-  dereference_iterator(Base const &other, CRTP *crtp)
-      : dereference_iterator::iterator_adaptor_(other), crtp(crtp)
-  {
-  }
-  template<typename Size = size_t>
-  explicit dereference_iterator(SourceT *p, Size size, CRTP *crtp)
-      : dereference_iterator(Base{p, (size_t)size}, crtp)
-  {
-  }
-  explicit dereference_iterator(SourceT *p, CRTP *crtp)  // For list_iterator
-      : dereference_iterator(Base{p}, crtp)
-  {
-  }
-  dereference_iterator begin() const
-  {
-    return {this->base().begin(), crtp};
-  }
-  const dereference_iterator cbegin() const
-  {
-    return {this->base().cbegin(), crtp};
-  }
-  dereference_iterator end() const
-  {
-    return {this->base().end(), crtp};
-  }
-  const dereference_iterator cend() const
-  {
-    return {this->base().cend(), crtp};
-  }
-  friend class boost::iterator_core_access;
-  ResT dereference() const
-  {
-    return crtp->dereference(this->base());
-  }
-  CRTP *crtp;
+template<typename T>
+struct pointer_iterator : pointer_iterator_base<T, std::random_access_iterator_tag> {
+  using pointer_iterator_base<T, std::random_access_iterator_tag>
+      ::pointer_iterator_base;
+  inline const T & operator*() const { return *this->curr; }
 };
 
-// Another iterator that iterates over doubly linked lists
+// An iterator that iterates over doubly linked lists
 template<typename SourceT,
          typename ResT = SourceT &,
          typename Tag = std::bidirectional_iterator_tag>
-struct list_iterator : public boost::iterator_adaptor<list_iterator<SourceT, ResT, Tag>,
-                                                      pointer_iterator<SourceT, Tag>,
-                                                      ResT,
-                                                      Tag,
-                                                      ResT> {
-  list_iterator() : list_iterator::iterator_adaptor_(), first(nullptr)
-  {
-  }
-  list_iterator(const pointer_iterator<SourceT, Tag> &other)
-      : list_iterator::iterator_adaptor_(other), first(other.first)
-  {
-  }
-  explicit list_iterator(SourceT *first)
-      : list_iterator::iterator_adaptor_(pointer_iterator<SourceT, Tag>{first}), first(first)
-  {
-  }
-  list_iterator begin() const
-  {
-    return list_iterator{first};
-  }
-  list_iterator end() const
-  {
-    return list_iterator{nullptr};
-  }
-  const list_iterator cbegin() const
-  {
-    return list_iterator{first};
-  }
-  const list_iterator cend() const
-  {
-    return list_iterator{nullptr};
-  }
-  friend class boost::iterator_core_access;
-  void increment()
-  {
-    this->base_reference().it = this->base_reference()->next;
-  }

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list