29 ClearCacheValue

ClearCacheValue() is used to reclaim memory for dependent variables which have allocated memory. It can make sense to reclaim this memory when the dependent is hard-dirty.

For example dependent variables which are pointers, strings, vectors, deques and maps are cleared.

Note that calling std::string::clear() merely sets the size to zero. The capacity() won't change (nor will reserve()ing less memory than currently reserved change the capacity).

Similarly clear() on a std::vector is not guaranteed to change its capacity. Instead swap can be used:


std::vector<T>().swap(x);

There is no concept of reserved space for a std::map. The members of the map are internally stored in a tree structure. There is no way to build the tree until you know the keys and values that are to be stored.

By contrast there is a reserve() method on std::unordered_map, so we use swap to clear any reserved space.

Implementation

template <typename T>
void ClearCacheValue(T&)
{
}

template <typename U, typename V>
void ClearCacheValue(std::pair<U,V>& p)
{
    ClearCacheValue(p.first);
    ClearCacheValue(p.second);
}

template <typename T, ssize_t N>
void ClearCacheValue(std::array<T,N>& a)
{
    for (auto& e : a)
    {
        ClearCacheValue(e);
    }
}

template <typename T, ssize_t N>
void ClearCacheValue(const Array<T,N>& a)
{
    for (auto& e : a)
    {
        ClearCacheValue(e);
    }
}

template <typename T>
void ClearCacheValue(const DynArray<T>& a)
{
    a.clear();
}

template <typename T>
void ClearCacheValue(std::basic_string<T>& s)
{
    std::basic_string<T>().swap(s);
}

template <typename T,typename A>
void ClearCacheValue(std::vector<T,A>& s)
{
    std::vector<T,A>().swap(s);
}

template <typename T,typename A>
void ClearCacheValue(std::deque<T,A>& s)
{
    std::deque<T,A>().swap(s);
}

template <typename T,typename A>
void ClearCacheValue(std::list<T,A>& s)
{
    std::list<T,A>().swap(s);
}

template <typename K, typename P, typename A>
void ClearCacheValue(std::set<K,P,A>& s)
{
    s.clear();
}

template <typename K, typename P, typename A>
void ClearCacheValue(std::multiset<K,P,A>& s)
{
    s.clear();
}

template <typename K,typename T,typename P,typename A>
void ClearCacheValue(std::map<K,T,P,A>& s)
{
    s.clear();
}

template <typename K,typename T,typename P,typename A>
void ClearCacheValue(std::multimap<K,T,P,A>& s)
{
    s.clear();
}

template <typename K,typename T,typename H,typename P, typename A>
void ClearCacheValue(std::unordered_map<K,T,H,P,A>& s)
{
    std::unordered_map<K,T,H,P,A>().swap(s);
}

template <typename T>
void ClearCacheValue(basic_xstring<T>& s)
{
    basic_xstring<T>().swap(s);
}

template <typename T>
void ClearCacheValue(xvector<T>& s)
{
    xvector<T>().swap(s);
}

template <typename T, ssize_t pageSize>
void ClearCacheValue(xdeque<T,pageSize>& s)
{
    xdeque<T,pageSize>().swap(s);
}

template <typename T>
void ClearCacheValue(std::shared_ptr<T>& p)
{
    p.reset();
}

template <typename T>
void ClearCacheValue(std::unique_ptr<T>& p)
{
    p.reset(nullptr);
}

template <typename T>
void ClearCacheValue(std::optional<T>& p)
{
    p.reset();
}

template <typename T>
void ClearCacheValue(ptr<T>& v)
{
    v = null;
}