1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-14 02:08:27 +08:00
Files
CMake/Source/cmStack.tcc
Matthew Woehlke f2bdc2176f cmStack: New, mutable stack class
We would like to record additional information in the find-package
stack, but we don't have the information at the point a stack entry is
created. The most sane way to handle this appears to be making the stack
mutable, at least under specific circumstances. To facilitate this, we
need a mutable stack type.

Refactor cmConstStack into cmStack, with the ability to either allow or
forbid mutation of its data. Re-add cmConstStack as a type alias.

This doesn't yet allow mutating cmFindPackageStack, but it's a necessary
step toward being able to do so.
2025-07-30 10:11:36 -04:00

86 lines
2.2 KiB
C++

/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include <cassert>
#include <memory>
#include <utility>
template <typename T>
struct cmStackEntry<T, cmStackType::Mutable>
{
cmStackEntry(std::shared_ptr<cmStackEntry const> parent, T value)
: Value(std::move(value))
, Parent(std::move(parent))
{
}
T mutable Value;
std::shared_ptr<cmStackEntry const> Parent;
};
template <typename T>
struct cmStackEntry<T, cmStackType::Const>
{
cmStackEntry(std::shared_ptr<cmStackEntry const> parent, T value)
: Value(std::move(value))
, Parent(std::move(parent))
{
}
T Value;
std::shared_ptr<cmStackEntry const> Parent;
};
template <typename T, typename Stack, cmStackType Mutable>
cmStack<T, Stack, Mutable>::cmStack() = default;
template <typename T, typename Stack, cmStackType Mutable>
Stack cmStack<T, Stack, Mutable>::Push(T value) const
{
return Stack(this->TopEntry, std::move(value));
}
template <typename T, typename Stack, cmStackType Mutable>
Stack cmStack<T, Stack, Mutable>::Pop() const
{
assert(this->TopEntry);
return Stack(this->TopEntry->Parent);
}
template <typename T, typename Stack, cmStackType Mutable>
T const& cmStack<T, Stack, Mutable>::Top() const
{
assert(this->TopEntry);
return this->TopEntry->Value;
}
template <typename T, typename Stack, cmStackType Mutable>
template <bool E>
typename std::enable_if<E, T>::type& cmStack<T, Stack, Mutable>::Top()
{
static_assert(Mutable == cmStackType::Mutable,
"T& cmStack::Top should only exist for mutable cmStack");
assert(this->TopEntry);
return this->TopEntry->Value;
}
template <typename T, typename Stack, cmStackType Mutable>
bool cmStack<T, Stack, Mutable>::Empty() const
{
return !this->TopEntry;
}
template <typename T, typename Stack, cmStackType Mutable>
cmStack<T, Stack, Mutable>::cmStack(std::shared_ptr<Entry const> parent,
T value)
: TopEntry(
std::make_shared<Entry const>(std::move(parent), std::move(value)))
{
}
template <typename T, typename Stack, cmStackType Mutable>
cmStack<T, Stack, Mutable>::cmStack(std::shared_ptr<Entry const> top)
: TopEntry(std::move(top))
{
}