This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository efl.

View the commit online.

commit bf4a6fb790bdef143f6fcde90230b970c52af9c0
Author: Cedric BAIL <[email protected]>
AuthorDate: Sun Nov 15 12:52:28 2026 -0600

    eina_cxx: an iterator of string views reads the strings C gives out
    
    A native string iterator gives out const char*, but
    iterator<string_view> read what it was given as if it were string_view
    objects, which are a pointer and a length: each element was read past
    the string it pointed to.
    
    iterator<string_view> is specialised to read the pointers and build the
    views, valid until the iterator moves on, and lends itself to C the way
    the other iterators do.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/bindings/cxx/eina_cxx/eina_iterator.hh   | 95 ++++++++++++++++++++++++++++
 src/tests/eina_cxx/eina_cxx_test_iterator.cc | 25 ++++++++
 2 files changed, 120 insertions(+)

diff --git a/src/bindings/cxx/eina_cxx/eina_iterator.hh b/src/bindings/cxx/eina_cxx/eina_iterator.hh
index 0cf1dab2b9..fccad0b6dd 100644
--- a/src/bindings/cxx/eina_cxx/eina_iterator.hh
+++ b/src/bindings/cxx/eina_cxx/eina_iterator.hh
@@ -19,6 +19,7 @@
 #include <Eina.h>
 
 #include <eina_eo_concrete_fwd.hh>
+#include <eina_string_view.hh>
 
 #include <cstdlib>
 #include <iterator>
@@ -452,6 +453,100 @@ public:
   }
 };
 
+/**
+ * An iterator over the @c const @c char* a native @c Eina_Iterator gives out.
+ *
+ * A @c string_view is a pointer and a length: reading the elements of a
+ * native string iterator as if they were @c string_view objects would read
+ * past each string. This one reads the pointers and builds the views.
+ */
+template <>
+struct iterator<string_view, void>
+  : _common_iterator_base<string_view const>
+{
+private:
+  typedef _common_iterator_base<string_view const> base_type; /**< Type for the base class. */
+  typedef iterator<string_view> self_type; /**< Type for the iterator itself. */
+  const char* _value; /**< @internal The string currently pointed to. */
+  string_view _view; /**< @internal The view on @c _value. */
+public:
+  typedef base_type::value_type value_type; /**< Type for elements returned by the iterator. */
+  typedef base_type::pointer pointer; /**< Type for a pointer to an element. */
+  typedef base_type::reference reference; /**< Type for a reference to an element. */
+  typedef base_type::difference_type difference_type; /**< Type to represent the distance between two iterators. */
+  typedef base_type::iterator_category iterator_category; /**< Defines the iterator category as the same of the base class. */
+
+  /**
+   * @brief Creates a iterator wrapping the given native @c Eina_Iterator handle.
+   *
+   * The native iterator gives out @c const @c char* and the iterator owns it.
+   */
+  explicit iterator(Eina_Iterator* iterator_ = 0)
+    : base_type(iterator_)
+    , _value(nullptr)
+  {
+    if(this->_iterator)
+      ++*this;
+  }
+
+  /**
+   * @brief Move the iterator to the next position.
+   * @return The iterator itself.
+   */
+  self_type& operator++()
+  {
+    void* data = ""
+    Eina_Bool r = ::eina_iterator_next(this->_iterator, &data);
+    if(!r)
+      {
+         ::eina_iterator_free(this->_iterator);
+         this->_iterator = 0;
+         data = ""
+      }
+    _value = static_cast<const char*>(data);
+    _view = _value ? string_view(_value) : string_view();
+    return *this;
+  }
+
+  /**
+   * @brief Move the iterator to the next position.
+   * @return The iterator itself.
+   */
+  self_type& operator++(int)
+  {
+    return ++*this;
+  }
+
+  /**
+   * @brief Get a reference to the string currently pointed by the iterator.
+   * @return A view on the current string, valid until the iterator moves on.
+   */
+  value_type& operator*() const
+  {
+    return _view;
+  }
+
+  /**
+   * @brief Return a pointer to the current string view.
+   */
+  pointer operator->() const
+  {
+    return &_view;
+  }
+
+  /**
+   * @internal
+   * Lend the iterator to native code that reads it without freeing it, for
+   * the generated conversions: not part of the API.
+   * @return A native @c Eina_Iterator that goes on from the string this
+   * iterator points to; @c NULL when there is nothing left.
+   */
+  Eina_Iterator* _lend_native_handle() const
+  {
+    return this->_lend_primed(const_cast<char*>(_value));
+  }
+};
+
 /**
  * @}
  */
diff --git a/src/tests/eina_cxx/eina_cxx_test_iterator.cc b/src/tests/eina_cxx/eina_cxx_test_iterator.cc
index b82c960607..c6ce8486de 100644
--- a/src/tests/eina_cxx/eina_cxx_test_iterator.cc
+++ b/src/tests/eina_cxx/eina_cxx_test_iterator.cc
@@ -21,6 +21,8 @@
 #include <Eo.hh>
 
 #include <algorithm>
+#include <string>
+#include <vector>
 
 #include "eina_cxx_suite.h"
 #include "eina_simple.eo.hh"
@@ -130,9 +132,32 @@ EFL_START_TEST(eina_cxx_iterator_assign)
 }
 EFL_END_TEST
 
+/* A native string iterator gives out const char*, which a string_view is not:
+ * reading them as string_view objects would read past each string. */
+EFL_START_TEST(eina_cxx_string_iterator)
+{
+  efl::eina::eina_init init;
+
+  const char* strings[] = { "a", "bb", "ccc" };
+  Eina_List* list = nullptr;
+  for (const char*& s : strings)
+    list = eina_list_append(list, s);
+
+  {
+    efl::eina::iterator<efl::eina::string_view> it(eina_list_iterator_new(list));
+    std::vector<std::string> got;
+    for (; it != efl::eina::iterator<efl::eina::string_view>(); ++it)
+      got.push_back(std::string(it->data(), it->size()));
+    ck_assert(got == (std::vector<std::string>{"a", "bb", "ccc"}));
+  }
+  eina_list_free(list);
+}
+EFL_END_TEST
+
 void
 eina_test_iterator(TCase *tc)
 {
+  tcase_add_test(tc, eina_cxx_string_iterator);
   tcase_add_test(tc, eina_cxx_iterator_assign);
   tcase_add_test(tc, eina_cxx_iterator_equal);
   tcase_add_test(tc, eina_cxx_eo_iterator_equal);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to