Skip to content
Snippets Groups Projects
Commit 3560884a authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: allow direct testing of HashTable iterator as a bool

- can be used as a more natural test on the iterator.
  For example, with

     HashTable<..> table;
     auto iter = table.find(...);

  Following are now all equivalent:

    1.  if (iter != table.end()) ...
    2.  if (iter.found()) ...
    3.  if (iter) ...
parent 08335beb
No related tags found
No related merge requests found
...@@ -40,7 +40,7 @@ using namespace Foam; ...@@ -40,7 +40,7 @@ using namespace Foam;
template<class Iter> template<class Iter>
void printIf(const Iter& iter) void printIf(const Iter& iter)
{ {
if (iter.found()) if (iter)
{ {
Info<< *iter; Info<< *iter;
} }
......
...@@ -685,8 +685,13 @@ protected: ...@@ -685,8 +685,13 @@ protected:
//- The key associated with the iterator //- The key associated with the iterator
inline const Key& key() const; inline const Key& key() const;
// Member Operators // Member Operators
//- True if iterator points to an entry
// This can be used directly instead of comparing to end()
explicit inline operator bool() const noexcept;
//- Compare hash-entry element pointers. //- Compare hash-entry element pointers.
// Independent of const/non-const access // Independent of const/non-const access
inline bool operator==(const Iterator<true>& iter) const; inline bool operator==(const Iterator<true>& iter) const;
......
...@@ -124,6 +124,15 @@ inline const Key& Foam::HashTable<T, Key, Hash>::Iterator<Const>::key() const ...@@ -124,6 +124,15 @@ inline const Key& Foam::HashTable<T, Key, Hash>::Iterator<Const>::key() const
} }
template<class T, class Key, class Hash>
template<bool Const>
inline Foam::HashTable<T, Key, Hash>::Iterator<Const>::operator
bool() const noexcept
{
return entry_;
}
template<class T, class Key, class Hash> template<class T, class Key, class Hash>
template<bool Const> template<bool Const>
inline bool Foam::HashTable<T, Key, Hash>::Iterator<Const>::operator== inline bool Foam::HashTable<T, Key, Hash>::Iterator<Const>::operator==
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment