Program Listing for File playback/resource-info/detail/utilities.h

Return to documentation for file (include\playback\resource-info\detail\utilities.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 <algorithm>
#include <cstddef>
#include <string>
#include <vector>

namespace gpa {
namespace playback {
namespace detail {

template<typename T>
inline void ValidateVectorSizeForIndex(size_t index, std::vector<T>& vector)
{
    if (vector.size() <= index) {
        vector.resize(index + 1);
    }
}

template<typename T, typename ComparisonFunctionType>
inline void SortVector(std::vector<T>& vector, ComparisonFunctionType comparisonFunction)
{
    std::sort(vector.begin(), vector.end(), comparisonFunction);
}

template<typename T>
inline void SortVector(std::vector<T>& vector)
{
    SortVector(vector, [](T const& lhs, T const& rhs) { return lhs < rhs; });
}

template<typename T, typename ComparisonFunctionType>
inline void SortVectorAndRemoveDuplicates(std::vector<T>& vector, ComparisonFunctionType comparisonFunction)
{
    SortVector(vector, comparisonFunction);
    vector.erase(std::unique(vector.begin(), vector.end()), vector.end());
}

template<typename T>
inline void SortVectorAndRemoveDuplicates(std::vector<T>& vector)
{
    SortVectorAndRemoveDuplicates(vector, [](T const& lhs, T const& rhs) { return lhs < rhs; });
}

template<typename InfoType, typename DerivedType>
inline void AddInfoPtrToVector(InfoType const* pInfo, std::vector<DerivedType const*>& vector)
{
    auto pDerivedInfo = pInfo ? pInfo->template As<DerivedType>() : nullptr;
    if (pDerivedInfo) {
        vector.push_back(pDerivedInfo);
    }
}

template<typename CountType, typename DataType>
inline void RegisterStorage(std::vector<DataType>& storage, CountType& count, DataType const*& pData)
{
    // NOTE : To register storage for an arbitrary array, we copy the given
    //  array into the given std::vector<> then replace the array's address
    //  with the address of the given std::vector<>'s underlying storage.
    if (count && pData) {
        storage.resize((size_t)count);
        memcpy(storage.data(), pData, (size_t)count * sizeof(DataType));
        pData = storage.data();
    } else {
        count = 0;
        pData = nullptr;
    }
}

inline void RegisterStorage(std::string& storage, char const*& pStr)
{
    // NOTE : To register storage for a C string, we copy the given C string
    //  into the given std::string then replace the C string's address
    //  with the address of the given std::string's underlying storage.
    if (pStr) {
        storage = pStr;
        pStr = storage.c_str();
    }
}

template<typename PtrType>
struct PtrComparer final
{
    inline bool operator()(PtrType const& lhs, PtrType const& rhs) const
    {
        if (!lhs && !rhs) {
            return false;
        }
        if (!lhs && rhs) {
            return true;
        }
        if (lhs && !rhs) {
            return false;
        }
        return *lhs < *rhs;
    }
};

}  // namespace detail
}  // namespace playback
}  // namespace gpa