cmConstStack: Factor out of cmListFileBacktrace

This presents value semantics for a stack of constant values.
Internally it shares ownership to avoid copies.  Previously
this was implemented by `cmListFileBacktrace` explicitly,
but the approach can be re-used for other kinds of stacks.
This commit is contained in:
Brad King
2022-03-30 16:35:23 -04:00
parent 9123193758
commit 11cc728e75
27 changed files with 146 additions and 87 deletions

View File

@@ -12,6 +12,7 @@
#include <cm/optional>
#include "cmConstStack.h"
#include "cmSystemTools.h"
/** \class cmListFileCache
@@ -157,35 +158,16 @@ bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs);
bool operator==(cmListFileContext const& lhs, cmListFileContext const& rhs);
bool operator!=(cmListFileContext const& lhs, cmListFileContext const& rhs);
// Represent a backtrace (call stack). Provide value semantics
// but use efficient reference-counting underneath to avoid copies.
// Represent a backtrace (call stack) with efficient value semantics.
class cmListFileBacktrace
: public cmConstStack<cmListFileContext, cmListFileBacktrace>
{
public:
// Default-constructed backtrace is empty.
cmListFileBacktrace() = default;
// Get a backtrace with the given call context added to the top.
cmListFileBacktrace Push(cmListFileContext const& lfc) const;
// Get a backtrace with the top level removed.
// May not be called until after a matching Push.
cmListFileBacktrace Pop() const;
// Get the context at the top of the backtrace.
// This may be called only if Empty() would return false.
cmListFileContext const& Top() const;
// Return true if this backtrace is empty.
bool Empty() const;
private:
struct Entry;
std::shared_ptr<Entry const> TopEntry;
cmListFileBacktrace(std::shared_ptr<Entry const> parent,
cmListFileContext const& lfc);
cmListFileBacktrace(std::shared_ptr<Entry const> top);
using cmConstStack::cmConstStack;
friend class cmConstStack<cmListFileContext, cmListFileBacktrace>;
};
#ifndef cmListFileCache_cxx
extern template class cmConstStack<cmListFileContext, cmListFileBacktrace>;
#endif
// Wrap type T as a value with a backtrace. For purposes of
// ordering and equality comparison, only the original value is