Added iterators to Setting sequences.
authorMatt Renaud <mrenaud92@gmail.com>
Wed, 5 Mar 2014 23:38:24 +0000 (18:38 -0500)
committerMatt Renaud <mrenaud92@gmail.com>
Wed, 5 Mar 2014 23:38:24 +0000 (18:38 -0500)
To address hyperrealm/libconfig#3, I have added a SettingIterator class
and begin() and end() methods on Setting so that lists and arrays of
Setting can be iterated through using C++ style iterators.

There is an iterator as well as const_iterator defined for the class.

lib/libconfig.h++
lib/libconfigcpp.c++

index 23dddb8..4d9f3ab 100644 (file)
@@ -51,6 +51,8 @@ namespace libconfig {
 class LIBCONFIGXX_API ConfigException : public std::exception { };
 
 class Setting; // fwd decl
+class SettingIterator;
+class SettingConstIterator;
 
 class LIBCONFIGXX_API SettingException : public ConfigException
 {
@@ -189,6 +191,9 @@ class LIBCONFIGXX_API Setting
     FormatHex = 1
   };
 
+  typedef SettingIterator iterator;
+  typedef SettingConstIterator const_iterator;
+
   private:
 
   config_setting_t *_setting;
@@ -343,8 +348,113 @@ class LIBCONFIGXX_API Setting
 
   unsigned int getSourceLine() const throw();
   const char *getSourceFile() const throw();
+
+  iterator begin();
+  iterator end();
+
+  const_iterator begin() const;
+  const_iterator end() const;
 };
 
+
+class LIBCONFIGXX_API SettingIterator
+{
+  private:
+
+  Setting *_setting;
+
+  int _count;
+  int _idx;
+
+  public:
+
+  SettingIterator(Setting &setting, bool endIterator = false);
+  SettingIterator(const SettingIterator &rhs);
+  SettingIterator& operator=(const SettingIterator &rhs);
+
+  // Equality comparison.
+  inline bool operator ==(SettingIterator const& rhs) const
+  { return _idx == rhs._idx; }
+
+  inline bool operator !=(SettingIterator const& rhs) const
+  { return ! (*this == rhs); }
+
+  bool operator <(SettingIterator const& rhs) const;
+
+  // Dereference operators.
+  inline Setting & operator *()  { return (*_setting)[_idx]; }
+  inline Setting * operator ->() { return &(*_setting)[_idx]; }
+
+  inline const Setting & operator *()  const { return (*_setting)[_idx]; }
+  inline const Setting * operator ->() const { return &(*_setting)[_idx]; }
+
+  // Increment and decrement operators.
+  SettingIterator& operator ++();
+  SettingIterator  operator ++(int);
+
+  SettingIterator& operator --();
+  SettingIterator  operator --(int);
+
+  // Arithmetic operators.
+  SettingIterator operator +(int offset) const;
+  SettingIterator& operator +=(int offset);
+
+  SettingIterator operator -(int offset) const;
+  SettingIterator& operator -=(int offset);
+
+  int operator -(const SettingIterator &rhs) const;
+};
+
+SettingIterator operator +(int offset, const SettingIterator &si);
+
+class LIBCONFIGXX_API SettingConstIterator
+{
+  private:
+
+  const Setting *_setting;
+
+  int _count;
+  int _idx;
+
+  public:
+
+  SettingConstIterator(const Setting &setting, bool endIterator = false);
+  SettingConstIterator(const SettingConstIterator &rhs);
+  SettingConstIterator& operator=(const SettingConstIterator &rhs);
+
+  // Equality comparison.
+  bool operator ==(SettingConstIterator const& rhs) const
+  { return _idx == rhs._idx; }
+
+  inline bool operator !=(SettingConstIterator const& rhs) const
+  { return ! (*this == rhs); }
+
+  // Dereference operators.
+  inline Setting const& operator *()  { return (*_setting)[_idx]; }
+  inline Setting const* operator ->() { return &(*_setting)[_idx]; }
+
+  inline const Setting& operator *()  const { return (*_setting)[_idx]; }
+  inline const Setting* operator ->() const { return &(*_setting)[_idx]; }
+
+  // Increment and decrement operators.
+  SettingConstIterator& operator ++();
+  SettingConstIterator  operator ++(int);
+
+  SettingConstIterator& operator --();
+  SettingConstIterator  operator --(int);
+
+  // Arithmetic operators.
+  SettingConstIterator operator +(int offset) const;
+  SettingConstIterator& operator +=(int offset);
+
+  SettingConstIterator operator -(int offset) const;
+  SettingConstIterator& operator -=(int offset);
+
+  int operator -(const SettingConstIterator &rhs) const;
+};
+
+SettingConstIterator operator +(int offset, const SettingConstIterator &si);
+
 class LIBCONFIGXX_API Config
 {
   private:
index 33b0dcc..96206f1 100644 (file)
@@ -1156,6 +1156,246 @@ Setting & Setting::wrapSetting(config_setting_t *s)
 
 // ---------------------------------------------------------------------------
 
+Setting::iterator Setting::begin()
+{ return iterator(*this); }
+
+// ---------------------------------------------------------------------------
+
+Setting::iterator Setting::end()
+{ return iterator(*this, true); }
+
+// ---------------------------------------------------------------------------
+
+Setting::const_iterator Setting::begin() const
+{ return const_iterator(*this); }
+
+// ---------------------------------------------------------------------------
+
+Setting::const_iterator Setting::end() const
+{ return const_iterator(*this, true); }
+
+// ---------------------------------------------------------------------------
+
+SettingIterator::SettingIterator(Setting& setting, bool endIterator)
+  : _setting(&setting), _count(setting.getLength())
+{
+  if (endIterator)
+    _idx = _count;
+  else
+    _idx = 0;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingIterator::SettingIterator(const SettingIterator &rhs)
+  : _setting(rhs._setting), _count(rhs._count), _idx(rhs._idx)
+{
+}
+
+// ---------------------------------------------------------------------------
+
+SettingIterator& SettingIterator::operator=(const SettingIterator &rhs)
+{
+  _setting = rhs._setting;
+  _count = rhs._count;
+  _idx = rhs._idx;
+  return *this;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingIterator& SettingIterator::operator ++()
+{
+  ++_idx;
+  return *this;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingIterator SettingIterator::operator ++(int)
+{
+  SettingIterator tmp(*this);
+  ++_idx;
+  return tmp;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingIterator& SettingIterator::operator --()
+{
+  --_idx;
+  return *this;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingIterator SettingIterator::operator --(int)
+{
+  SettingIterator tmp(*this);
+  --_idx;
+  return tmp;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingIterator SettingIterator::operator +(int offset) const
+{
+  SettingIterator cpy(*this);
+  cpy += offset;
+  return cpy;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingIterator& SettingIterator::operator +=(int offset)
+{
+  _idx += offset;
+  return *this;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingIterator operator +(int offset, SettingIterator& si)
+{
+  SettingIterator cpy(si);
+  cpy += offset;
+  return cpy;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingIterator SettingIterator::operator -(int offset) const
+{
+  SettingIterator cpy(*this);
+  cpy._idx -= offset;
+  return cpy;;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingIterator& SettingIterator::operator -=(int offset)
+{
+  _idx -= offset;
+  return *this;
+}
+
+// ---------------------------------------------------------------------------
+
+int SettingIterator::operator -(SettingIterator const& rhs) const
+{
+  return _idx - rhs._idx;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingConstIterator::SettingConstIterator(const Setting &setting, bool endIterator)
+  : _setting(&setting), _count(setting.getLength())
+{
+  if (endIterator)
+    _idx = _count;
+  else
+    _idx = 0;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingConstIterator::SettingConstIterator(const SettingConstIterator &rhs)
+  : _setting(rhs._setting), _count(rhs._count), _idx(rhs._idx)
+{
+}
+
+// ---------------------------------------------------------------------------
+
+SettingConstIterator& SettingConstIterator::operator=(const SettingConstIterator &rhs)
+{
+  _setting = rhs._setting;
+  _count = rhs._count;
+  _idx = rhs._idx;
+  return *this;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingConstIterator& SettingConstIterator::operator ++()
+{ ++_idx; return *this; }
+
+// ---------------------------------------------------------------------------
+
+SettingConstIterator SettingConstIterator::operator ++(int)
+{
+  SettingConstIterator tmp(*this);
+  ++_idx;
+  return tmp;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingConstIterator& SettingConstIterator::operator --()
+{
+  --_idx;
+  return *this;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingConstIterator SettingConstIterator::operator --(int)
+{
+  SettingConstIterator tmp(*this);
+  --_idx;
+  return tmp;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingConstIterator SettingConstIterator::operator +(int offset) const
+{
+  SettingConstIterator cpy(*this);
+  cpy += offset;
+  return cpy;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingConstIterator& SettingConstIterator::operator +=(int offset)
+{
+  _idx += offset;
+  return *this;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingConstIterator operator +(int offset, SettingConstIterator& si)
+{
+  SettingConstIterator cpy(si);
+  cpy += offset;
+  return cpy;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingConstIterator SettingConstIterator::operator -(int offset) const
+{
+  SettingConstIterator cpy(*this);
+  cpy -= offset;
+  return cpy;
+}
+
+// ---------------------------------------------------------------------------
+
+SettingConstIterator& SettingConstIterator::operator -=(int offset)
+{
+  _idx -= offset;
+  return *this;
+}
+
+// ---------------------------------------------------------------------------
+
+int SettingConstIterator::operator -(SettingConstIterator const& rhs) const
+{
+  return _idx - rhs._idx;
+}
+
+
 } // namespace libconfig
 
 // eof