1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-10-24 23:33:50 +02:00

Fix mac build. No standard headers in Audacity.h.

This commit is contained in:
Paul Licameli
2016-02-14 13:23:08 -05:00
parent 7aee5d09d8
commit 0c9deb398c
31 changed files with 230 additions and 194 deletions

View File

@@ -170,9 +170,6 @@ void QuitAudacity();
// Marks strings for extraction only...must use wxGetTranslation() to translate.
#define XO(s) wxT(s)
#include <memory>
#include <utility>
// This renames a good use of this C++ keyword that we don't need to review when hunting for leaks.
#define PROHIBITED = delete
@@ -182,178 +179,4 @@ void QuitAudacity();
// pointer like std::unique_ptr or std::shared_ptr.
#define safenew new
#if !defined(__WXMSW__)
/* replicate the very useful C++14 make_unique for those build environments
that don't implement it yet.
typical useage:
auto p = std::make_unique<Myclass>(ctorArg1, ctorArg2, ... ctorArgN);
p->DoSomething();
auto q = std::make_unique<Myclass[]>(count);
q[0].DoSomethingElse();
The first hides naked NEW and DELETE from the source code.
The second hides NEW[] and DELETE[]. Both of course ensure destruction if
you don't use something like std::move(p) or q.release(). Both expressions require
that you identify the type only once, which is brief and less error prone.
(Whereas this omission of [] might invite a runtime error:
std::unique_ptr<Myclass> q { new Myclass[count] }; )
Some C++11 tricks needed here are (1) variadic argument lists and
(2) making the compile-time dispatch work correctly. You can't have
a partially specialized template function, but you get the effect of that
by other metaprogramming means.
*/
namespace std {
// For overloading resolution
template <typename X> struct __make_unique_result {
using scalar_case = unique_ptr<X>;
};
// Partial specialization of the struct for array case
template <typename X> struct __make_unique_result<X[]> {
using array_case = unique_ptr<X[]>;
using element = X;
};
// Now the scalar version of unique_ptr
template<typename X, typename... Args> inline
typename __make_unique_result<X>::scalar_case
make_unique(Args&&... args)
{
return typename __make_unique_result<X>::scalar_case
{ safenew X( forward<Args>(args)... ) };
}
// Now the array version of unique_ptr
// The compile-time dispatch trick is that the non-existence
// of the scalar_case type makes the above overload
// unavailable when the template parameter is explicit
template<typename X> inline
typename __make_unique_result<X>::array_case
make_unique(size_t count)
{
return typename __make_unique_result<X>::array_case
{ safenew typename __make_unique_result<X>::element[count] };
}
}
#endif
/*
* template class Maybe<X>
* Can be used for monomorphic objects that are stack-allocable, but only conditionally constructed.
* You might also use it as a member.
* Initialize with create(), then use like a smart pointer,
* with *, ->, get(), reset(), or in if()
*/
template<typename X>
class Maybe {
public:
// Construct as NULL
Maybe() {}
// Supply the copy and move, so you might use this as a class member too
Maybe(const Maybe &that)
{
if (that.get())
create(*that);
}
Maybe& operator= (const Maybe &that)
{
if (this != &that) {
if (that.get())
create(*that);
else
reset();
}
return *this;
}
Maybe(Maybe &&that)
{
if (that.get())
create(::std::move(*that));
}
Maybe& operator= (Maybe &&that)
{
if (this != &that) {
if (that.get())
create(::std::move(*that));
else
reset();
}
return *this;
}
// Make an object in the buffer, passing constructor arguments,
// but destroying any previous object first
// Note that if constructor throws, we remain in a consistent
// NULL state -- giving exception safety but only weakly
// (previous value was lost if present)
template<typename... Args>
void create(Args... args)
{
// Lose any old value
reset();
// Create new value
pp = safenew(address()) X( std::forward<Args>(args)... );
}
// Destroy any object that was built in it
~Maybe()
{
reset();
}
// Pointer-like operators
// Dereference, with the usual bad consequences if NULL
X &operator* () const
{
return *pp;
}
X *operator-> () const
{
return pp;
}
X* get() const
{
return pp;
}
void reset()
{
if (pp)
pp->~X(), pp = nullptr;
}
// So you can say if(ptr)
explicit operator bool() const
{
return pp != nullptr;
}
private:
X* address()
{
return reinterpret_cast<X*>(&storage);
}
// Data
typename ::std::aligned_storage<
sizeof(X)
// , alignof(X) // Not here yet in all compilers
>::type storage{};
X* pp{ nullptr };
};
#endif // __AUDACITY_H__