Program Listing for File enumerator.h

Return to documentation for file (include\playback\resource-info\enumerator.h)

/******************************************************************************

© Intel Corporation.

This software and the related documents are Intel copyrighted materials,
and your use of them is governed by the express license under which they
were provided to you ("License"). Unless the License provides otherwise,
you may not use, modify, copy, publish, distribute, disclose or transmit
this software or the related documents without Intel's prior written
permission.

 This software and the related documents are provided as is, with no express
or implied warranties, other than those that are expressly stated in the
License.

******************************************************************************/

#pragma once

#include "utility/assert.h"

namespace gpa {
namespace playback {

template<typename T>
class Enumerator
{
public:
    class Iterator final
    {
    public:
        Iterator() = default;

        inline Iterator(T const** ptr)
            : mPtr{ptr}
        {
        }

        Iterator(Iterator const& other) = default;

        Iterator& operator=(Iterator const& other) = default;

        inline Iterator& operator++()
        {
            ++mPtr;
            return *this;
        }

        inline Iterator operator++(int)
        {
            Iterator temp = *this;
            operator++();
            return temp;
        }

        inline bool operator==(Iterator const& other)
        {
            return mPtr == other.mPtr;
        }

        inline bool operator!=(Iterator const& other)
        {
            return !(*this == other);
        }

        inline T const* operator->() const
        {
            return mPtr ? *mPtr : nullptr;
        }

        inline T const& operator*() const
        {
            auto pObj = operator->();
            if (!pObj) {
                static T sObj;
                return sObj;
            }
            return *pObj;
        }

    private:
        T const** mPtr{nullptr};
    };

    Enumerator() = default;

    inline Enumerator(
        T const** pBegin,
        T const** pEnd)
        : mBegin(pBegin)
        , mEnd(pEnd)
    {
    }

    inline Iterator begin() const
    {
        return mBegin;
    }

    inline Iterator end() const
    {
        return mEnd;
    }

private:
    Iterator mBegin{nullptr};
    Iterator mEnd{nullptr};
};

}  // namespace playback
}  // namespace gpa