MultiObject.h#

This file contains the Multi<T> class template, which is the usual container for objects that must be mirrored in the API level: Framebuffers etc.

Includes#

  • vector

Included By#

Namespaces#

Classes#

Source Code#

#pragma once
#include <vector>

namespace pvr {
template<typename T_, uint8_t MAX_ITEMS = 4>
class Multi
{
public:
    typedef T_ ElementType;
    typedef ElementType ContainerType[MAX_ITEMS];
private:
    ContainerType container;
    uint32_t numItems;

public:
    ElementType* data() { return numItems > 0 ? container : nullptr; }

    ElementType& operator[](uint32_t idx)
    {
        if (idx >= numItems) { numItems = idx + 1; }
        return container[idx];
    }
    const ElementType& operator[](uint32_t idx) const
    {
        assertion(idx < MAX_ITEMS);
        return container[idx];
    }

    ContainerType& getContainer() { return container; }

    ElementType& back() { return container[numItems - 1]; }

    const ElementType& back() const { return container[numItems - 1]; }

    size_t size() const { return numItems; }

    void resize(uint32_t newsize)
    {
        for (uint32_t i = newsize + 1; i < numItems; ++i)
        {
            container[i] = ElementType(); // clean up unused elements
        }
        numItems = static_cast<uint32_t>(newsize);
    }

    void clear() { resize(0); }

    Multi() : numItems(0) {}

    explicit Multi(const ElementType& element) : numItems(MAX_ITEMS)
    {
        for (size_t i = 0; i < numItems; ++i) { container[i] = element; }
    }

    Multi(const ElementType* elements, const uint32_t count) : numItems(count)
    {
        assertion(count <= MAX_ITEMS, "Multi<T>: Index out of range");
        for (size_t i = 0; i < count; ++i) { container[i] = elements[i]; }
    }

    void add(const ElementType& element)
    {
        assertion(numItems < MAX_ITEMS, "Multi<T>: Index out of range");
        container[numItems++] = element;
    }
    void add(const ElementType* element, const uint32_t count)
    {
        assertion(numItems + count <= MAX_ITEMS, "Multi<T>: Index out of range");
        for (uint32_t i = 0; i < count; ++i) { container[numItems++] = element[i]; }
    }
};
} // namespace pvr