mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-10-06 23:31:19 +00:00
* Add Missing Includes Across src/libslic3r Every libslic3r source and header now directly includes the headers declaring what it uses, rather than relying on the precompiled header or transitive includes. Generated with clang-tidy misc-include-cleaner, with libslic3r headers spelled libslic3r/... so they resolve outside the library's private include paths. MultiMaterialSegmentation.hpp, Support/SupportParameters.hpp and Format/STEP.hpp are made self-contained by hand. * Make the libslic3r Headers Compile on Their Own Each now includes, or forward-declares, what it uses instead of relying on what its includers happened to include first. Left out: I18N.hpp, which errors on purpose when included from GUI code, and VoxelizeCSGMesh.hpp and SLA/bicubic.h, which nothing includes and which no longer compile at all. * Add the Includes Missing From the Hand-Fixed libslic3r Headers clang-tidy would not edit these headers while they failed to compile on their own, so the first pass skipped them. With the headers now self-contained, a second pass adds the rest. * Keep Windows Setup Ahead of the Added libslic3r Includes Print.cpp and Thread.cpp open with a _WIN32 block that has to come first; without the precompiled header, Print.cpp otherwise reaches windows.h through OCCT with NONLS defined and boost/regex fails. OpenVDBUtils.cpp and SLA/SupportTreeBuilder.cpp had includes inside #ifndef NOMINMAX, which libslic3r defines on Windows, so those were skipped there. .clang-tidy also ignores the MSVC STL and UCRT internals, Boost.Multiprecision's fwd.hpp and CPython's Windows include directory. * Re-Add libslic3r Includes After the Clipper2 2.0.1 Migration Rebasing onto main took main's version of the files the Clipper2 migration rewrote, so their added includes are restored here, along with includes for main's new code. Clipper2's individual headers are now ignored by clang-tidy: they only build the Z variant through clipper2_z.hpp, which defines USINGZ first, so including clipper.core.h and the like directly broke ClipperZUtils.cpp.
132 lines
4.3 KiB
C++
132 lines
4.3 KiB
C++
#ifndef ANYPTR_HPP
|
|
#define ANYPTR_HPP
|
|
|
|
#include <boost/variant/variant.hpp>
|
|
#include <memory>
|
|
#include <type_traits>
|
|
#include <boost/variant.hpp>
|
|
|
|
namespace Slic3r {
|
|
|
|
// A general purpose pointer holder that can hold any type of smart pointer
|
|
// or raw pointer which can own or not own any object they point to.
|
|
// In case a raw pointer is stored, it is not destructed so ownership is
|
|
// assumed to be foreign.
|
|
//
|
|
// The stored pointer is not checked for being null when dereferenced.
|
|
//
|
|
// This is a movable only object due to the fact that it can possibly hold
|
|
// a unique_ptr which a non-copy.
|
|
template<class T>
|
|
class AnyPtr {
|
|
enum { RawPtr, UPtr, ShPtr, WkPtr };
|
|
|
|
boost::variant<T*, std::unique_ptr<T>, std::shared_ptr<T>, std::weak_ptr<T>> ptr;
|
|
|
|
template<class Self> static T *get_ptr(Self &&s)
|
|
{
|
|
switch (s.ptr.which()) {
|
|
case RawPtr: return boost::get<T *>(s.ptr);
|
|
case UPtr: return boost::get<std::unique_ptr<T>>(s.ptr).get();
|
|
case ShPtr: return boost::get<std::shared_ptr<T>>(s.ptr).get();
|
|
case WkPtr: {
|
|
auto shptr = boost::get<std::weak_ptr<T>>(s.ptr).lock();
|
|
return shptr.get();
|
|
}
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
public:
|
|
template<class TT = T, class = std::enable_if_t<std::is_convertible_v<TT, T>>>
|
|
AnyPtr(TT *p = nullptr) : ptr{p}
|
|
{}
|
|
template<class TT, class = std::enable_if_t<std::is_convertible_v<TT, T>>>
|
|
AnyPtr(std::unique_ptr<TT> p) : ptr{std::unique_ptr<T>(std::move(p))}
|
|
{}
|
|
template<class TT, class = std::enable_if_t<std::is_convertible_v<TT, T>>>
|
|
AnyPtr(std::shared_ptr<TT> p) : ptr{std::shared_ptr<T>(std::move(p))}
|
|
{}
|
|
template<class TT, class = std::enable_if_t<std::is_convertible_v<TT, T>>>
|
|
AnyPtr(std::weak_ptr<TT> p) : ptr{std::weak_ptr<T>(std::move(p))}
|
|
{}
|
|
|
|
~AnyPtr() = default;
|
|
|
|
AnyPtr(AnyPtr &&other) noexcept : ptr{std::move(other.ptr)} {}
|
|
AnyPtr(const AnyPtr &other) = delete;
|
|
|
|
AnyPtr &operator=(AnyPtr &&other) noexcept { ptr = std::move(other.ptr); return *this; }
|
|
AnyPtr &operator=(const AnyPtr &other) = delete;
|
|
|
|
template<class TT, class = std::enable_if_t<std::is_convertible_v<TT, T>>>
|
|
AnyPtr &operator=(TT *p) { ptr = p; return *this; }
|
|
|
|
template<class TT, class = std::enable_if_t<std::is_convertible_v<TT, T>>>
|
|
AnyPtr &operator=(std::unique_ptr<TT> p) { ptr = std::move(p); return *this; }
|
|
|
|
template<class TT, class = std::enable_if_t<std::is_convertible_v<TT, T>>>
|
|
AnyPtr &operator=(std::shared_ptr<TT> p) { ptr = p; return *this; }
|
|
|
|
template<class TT, class = std::enable_if_t<std::is_convertible_v<TT, T>>>
|
|
AnyPtr &operator=(std::weak_ptr<TT> p) { ptr = std::move(p); return *this; }
|
|
|
|
const T &operator*() const { return *get_ptr(*this); }
|
|
T &operator*() { return *get_ptr(*this); }
|
|
|
|
T *operator->() { return get_ptr(*this); }
|
|
const T *operator->() const { return get_ptr(*this); }
|
|
|
|
T *get() { return get_ptr(*this); }
|
|
const T *get() const { return get_ptr(*this); }
|
|
|
|
operator bool() const
|
|
{
|
|
switch (ptr.which()) {
|
|
case RawPtr: return bool(boost::get<T *>(ptr));
|
|
case UPtr: return bool(boost::get<std::unique_ptr<T>>(ptr));
|
|
case ShPtr: return bool(boost::get<std::shared_ptr<T>>(ptr));
|
|
case WkPtr: {
|
|
auto shptr = boost::get<std::weak_ptr<T>>(ptr).lock();
|
|
return bool(shptr);
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
// If the stored pointer is a shared or weak pointer, returns a reference
|
|
// counted copy. Empty shared pointer is returned otherwise.
|
|
std::shared_ptr<T> get_shared_cpy() const
|
|
{
|
|
std::shared_ptr<T> ret;
|
|
|
|
switch (ptr.which()) {
|
|
case ShPtr: ret = boost::get<std::shared_ptr<T>>(ptr); break;
|
|
case WkPtr: ret = boost::get<std::weak_ptr<T>>(ptr).lock(); break;
|
|
default:
|
|
;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
// If the underlying pointer is unique, convert to shared pointer
|
|
void convert_unique_to_shared()
|
|
{
|
|
if (ptr.which() == UPtr)
|
|
ptr = std::shared_ptr<T>{std::move(boost::get<std::unique_ptr<T>>(ptr))};
|
|
}
|
|
|
|
// Returns true if the data is owned by this AnyPtr instance
|
|
bool is_owned() const noexcept
|
|
{
|
|
return ptr.which() == UPtr || ptr.which() == ShPtr;
|
|
}
|
|
};
|
|
|
|
} // namespace Slic3r
|
|
|
|
#endif // ANYPTR_HPP
|