Skip to content

Commit

Permalink
Merge pull request #2343 from Paul-Licameli/modernization
Browse files Browse the repository at this point in the history
Use more C++14 and C++17 library types
  • Loading branch information
Paul-Licameli committed Jan 11, 2022
2 parents ab7a1a9 + c814e58 commit 672f192
Show file tree
Hide file tree
Showing 50 changed files with 208 additions and 282 deletions.
8 changes: 3 additions & 5 deletions libraries/lib-audio-devices/AudioIOBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ Paul Licameli split from AudioIO.h
#ifndef __AUDACITY_AUDIO_IO_BASE__
#define __AUDACITY_AUDIO_IO_BASE__




#include <cfloat>
#include <chrono>
#include <functional>
#include <optional>
#include <vector>
Expand Down Expand Up @@ -67,8 +65,8 @@ struct AudioIOStartStreamOptions

// An unfortunate thing needed just to make scrubbing work on Linux when
// we can't use a separate polling thread.
// The return value is a number of milliseconds to sleep before calling again
std::function< unsigned long() > playbackStreamPrimer;
// The return value is duration to sleep before calling again
std::function< std::chrono::milliseconds() > playbackStreamPrimer;

using PolicyFactory = std::function<
std::unique_ptr<PlaybackPolicy>(const AudioIOStartStreamOptions&) >;
Expand Down
4 changes: 2 additions & 2 deletions libraries/lib-audio-devices/DeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,10 @@ void DeviceManager::Rescan()
}


float DeviceManager::GetTimeSinceRescan() {
std::chrono::duration<float> DeviceManager::GetTimeSinceRescan() {
auto now = std::chrono::steady_clock::now();
auto dur = std::chrono::duration_cast<std::chrono::duration<float>>(now - mRescanTime);
return dur.count();
return dur;
}


Expand Down
2 changes: 1 addition & 1 deletion libraries/lib-audio-devices/DeviceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class AUDIO_DEVICES_API DeviceManager final
void Rescan();

// Time since devices scanned in seconds.
float GetTimeSinceRescan();
std::chrono::duration<float> GetTimeSinceRescan();

DeviceSourceMap* GetDefaultOutputDevice(int hostIndex);
DeviceSourceMap* GetDefaultInputDevice(int hostIndex);
Expand Down
2 changes: 1 addition & 1 deletion libraries/lib-files/wxFileNameWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class wxFileNameWrapper : public wxFileName
// Awful hack number 1 makes gcc 5 choke
enum : size_t { Size = sizeof(*this) };
// Do it bitwise.
// std::aligned_storage<Size>::type buffer;
// std::aligned_storage_t<Size> buffer;
char buffer[Size];
memcpy(&buffer, this, Size);
memcpy(this, &that, Size);
Expand Down
2 changes: 1 addition & 1 deletion libraries/lib-math/SampleCount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

size_t sampleCount::as_size_t() const {
wxASSERT(value >= 0);
wxASSERT(static_cast<std::make_unsigned<type>::type>(value) <= std::numeric_limits<size_t>::max());
wxASSERT(static_cast<std::make_unsigned_t<type>>(value) <= std::numeric_limits<size_t>::max());
return value;
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/lib-registries/AttachedVirtualFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class AttachedVirtualFunction

// Check that inheritance is correct
static_assert(
std::is_base_of< typename Overridden::Object, Object >::value,
std::is_base_of_v< typename Overridden::Object, Object >,
"overridden class must be a base of the overriding class"
);

Expand Down
8 changes: 4 additions & 4 deletions libraries/lib-registries/ClientData.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ class Site
//! @copydoc Get
/*! const overload returns const references only. */
template< typename Subclass = const ClientData >
auto Get( const RegisteredFactory &key ) const -> typename
std::enable_if< std::is_const< Subclass >::value, Subclass & >::type
auto Get( const RegisteredFactory &key ) const ->
std::enable_if_t< std::is_const< Subclass >::value, Subclass & >
{
auto data = GetData();
return DoGet< Subclass >( data, key );
Expand All @@ -339,8 +339,8 @@ class Site
//! @copydoc Find
/*! const overload returns pointers to const only. */
template< typename Subclass = const ClientData >
auto Find( const RegisteredFactory &key ) const -> typename
std::enable_if< std::is_const< Subclass >::value, Subclass * >::type
auto Find( const RegisteredFactory &key ) const ->
std::enable_if_t< std::is_const< Subclass >::value, Subclass * >
{
auto data = GetData();
return DoFind< Subclass >( data, key );
Expand Down
4 changes: 2 additions & 2 deletions libraries/lib-string-utils/ToChars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ template <typename FloatType> boundaries compute_boundaries(FloatType value)
constexpr std::uint64_t kHiddenBit = std::uint64_t { 1 }
<< (kPrecision - 1); // = 2^(p-1)

using bits_type = typename std::conditional<
kPrecision == 24, std::uint32_t, std::uint64_t>::type;
using bits_type = std::conditional_t<
kPrecision == 24, std::uint32_t, std::uint64_t>;

const std::uint64_t bits = reinterpret_bits<bits_type>(value);
const std::uint64_t E = bits >> (kPrecision - 1);
Expand Down
4 changes: 2 additions & 2 deletions libraries/lib-strings/Identifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ template< typename Tag1, typename Tag2, bool b1, bool b2 >
inline bool operator == (
const TaggedIdentifier< Tag1, b1 > &x, const TaggedIdentifier< Tag2, b2 > &y )
{
static_assert( std::is_same< Tag1, Tag2 >::value && b1 == b2,
static_assert( std::is_same_v< Tag1, Tag2 > && b1 == b2,
"TaggedIdentifiers with different tags or sensitivity are not comparable" );
// This test should be eliminated at compile time:
if ( b1 )
Expand All @@ -175,7 +175,7 @@ template< typename Tag1, typename Tag2, bool b1, bool b2 >
inline bool operator < (
const TaggedIdentifier< Tag1, b1 > &x, const TaggedIdentifier< Tag2, b2 > &y )
{
static_assert( std::is_same< Tag1, Tag2 >::value && b1 == b2,
static_assert( std::is_same_v< Tag1, Tag2 > && b1 == b2,
"TaggedIdentifiers with different tags or sensitivity are not comparable" );
// This test should be eliminated at compile time:
if ( b1 )
Expand Down
56 changes: 24 additions & 32 deletions libraries/lib-track/Track.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ class TRACK_API Track /* not final */
}

template<typename Subclass = const Track>
inline auto SharedPointer() const -> typename
std::enable_if<
std::is_const<Subclass>::value, std::shared_ptr<Subclass>
>::type
inline auto SharedPointer() const ->
std::enable_if_t<
std::is_const_v<Subclass>, std::shared_ptr<Subclass>
>
{
// shared_from_this is injected into class scope by base class
// std::enable_shared_from_this<Track>
Expand Down Expand Up @@ -643,7 +643,7 @@ class TRACK_API Track /* not final */

//! Whether upcast of ArgumentType* to first BaseClass* works
static constexpr bool Compatible =
std::is_base_of<BaseClass, ArgumentType>::value;
std::is_base_of_v<BaseClass, ArgumentType>;
//! undefined function used in decltype only to compute a type, using other overloads
template< typename Function, typename ...Functions >
static auto test()
Expand Down Expand Up @@ -928,8 +928,7 @@ template<typename T>
This overload for const pointers can cast only to other const pointer types. */
template<typename T>
inline std::enable_if_t<
std::is_pointer_v<T> &&
std::is_const_v< std::remove_pointer_t< T > >,
std::is_pointer_v<T> && std::is_const_v< std::remove_pointer_t< T > >,
T
>
track_cast(const Track *track)
Expand Down Expand Up @@ -959,15 +958,8 @@ template <
{
public:
//! Type of predicate taking pointer to const TrackType
/*! @todo C++14: simplify away ::type */
using FunctionType = std::function< bool(
typename std::add_pointer<
typename std::add_const<
typename std::remove_pointer<
TrackType
>::type
>::type
>::type
std::add_pointer_t< std::add_const_t< std::remove_pointer_t<TrackType> > >
) >;

//! Constructor, usually not called directly except by methods of TrackList
Expand Down Expand Up @@ -999,12 +991,12 @@ template <
satisfies the type constraint, or to the end */
template < typename TrackType2 >
auto Filter() const
-> typename std::enable_if<
std::is_base_of< TrackType, TrackType2 >::value &&
(!std::is_const<TrackType>::value ||
std::is_const<TrackType2>::value),
-> std::enable_if_t<
std::is_base_of_v< TrackType, TrackType2 > &&
(!std::is_const_v<TrackType> ||
std::is_const_v<TrackType2>),
TrackIter< TrackType2 >
>::type
>
{
return { this->mBegin, this->mIter, this->mEnd, this->mPred };
}
Expand Down Expand Up @@ -1373,9 +1365,9 @@ class TRACK_API TrackList final
/*! const overload will only produce iterators over const TrackType */
template < typename TrackType = const Track >
auto Find(const Track *pTrack) const
-> typename std::enable_if< std::is_const<TrackType>::value,
-> std::enable_if_t< std::is_const_v<TrackType>,
TrackIter< TrackType >
>::type
>
{
if (!pTrack || pTrack->GetOwner().get() != this)
return EndIterator<TrackType>();
Expand Down Expand Up @@ -1406,9 +1398,9 @@ class TRACK_API TrackList final

template < typename TrackType = const Track >
auto Any() const
-> typename std::enable_if< std::is_const<TrackType>::value,
-> std::enable_if_t< std::is_const_v<TrackType>,
TrackIterRange< TrackType >
>::type
>
{
return Tracks< TrackType >();
}
Expand All @@ -1423,9 +1415,9 @@ class TRACK_API TrackList final

template < typename TrackType = const Track >
auto Selected() const
-> typename std::enable_if< std::is_const<TrackType>::value,
-> std::enable_if_t< std::is_const_v<TrackType>,
TrackIterRange< TrackType >
>::type
>
{
return Tracks< TrackType >( &Track::IsSelected );
}
Expand All @@ -1440,9 +1432,9 @@ class TRACK_API TrackList final

template < typename TrackType = const Track >
auto Leaders() const
-> typename std::enable_if< std::is_const<TrackType>::value,
-> std::enable_if_t< std::is_const_v<TrackType>,
TrackIterRange< TrackType >
>::type
>
{
return Tracks< TrackType >( &Track::IsLeader );
}
Expand All @@ -1457,9 +1449,9 @@ class TRACK_API TrackList final

template < typename TrackType = const Track >
auto SelectedLeaders() const
-> typename std::enable_if< std::is_const<TrackType>::value,
-> std::enable_if_t< std::is_const_v<TrackType>,
TrackIterRange< TrackType >
>::type
>
{
return Tracks< TrackType >( &Track::IsSelectedLeader );
}
Expand Down Expand Up @@ -1601,9 +1593,9 @@ class TRACK_API TrackList final
typename TrackIterRange< TrackType >::iterator::FunctionType
>
auto Tracks( const Pred &pred = {} ) const
-> typename std::enable_if< std::is_const<TrackType>::value,
-> std::enable_if_t< std::is_const_v<TrackType>,
TrackIterRange< TrackType >
>::type
>
{
auto b = const_cast<TrackList*>(this)->getBegin();
auto e = const_cast<TrackList*>(this)->getEnd();
Expand Down
128 changes: 0 additions & 128 deletions libraries/lib-utility/MemoryX.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,134 +128,6 @@ class ArraysOf : public ArrayOf<ArrayOf<X>>
}
};

/**
\class Optional
\brief Like a smart pointer, allows for object to not exist (nullptr)
\brief emulating some of std::optional of C++17
template class Optional<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 emplace(), then use like a smart pointer,
with *, ->, reset(), or in if()
*/

template<typename X>
class Optional {
public:

using value_type = X;

// Construct as NULL
Optional() {}

// Supply the copy and move, so you might use this as a class member too
Optional(const Optional &that)
{
if (that)
emplace(*that);
}

Optional& operator= (const Optional &that)
{
if (this != &that) {
if (that)
emplace(*that);
else
reset();
}
return *this;
}

Optional(Optional &&that)
{
if (that)
emplace(::std::move(*that));
}

Optional& operator= (Optional &&that)
{
if (this != &that) {
if (that)
emplace(::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>
X& emplace(Args&&... args)
{
// Lose any old value
reset();
// emplace NEW value
pp = safenew(address()) X(std::forward<Args>(args)...);
return **this;
}

// Destroy any object that was built in it
~Optional()
{
reset();
}

// Pointer-like operators

/// Dereference, with the usual bad consequences if NULL
X &operator* () const
{
return *pp;
}

X *operator-> () const
{
return pp;
}

void reset()
{
if (pp)
pp->~X(), pp = nullptr;
}

// So you can say if(ptr)
explicit operator bool() const
{
return pp != nullptr;
}

bool has_value() const
{
return pp != nullptr;
}

private:
X* address()
{
return reinterpret_cast<X*>(&storage);
}

// Data
#if 0
typename ::std::aligned_storage<
sizeof(X)
// , alignof(X) // Not here yet in all compilers
>::type storage{};
#else
union {
double d;
char storage[sizeof(X)];
};
#endif
X* pp{ nullptr };
};

/**
A deleter for pointers obtained with malloc
*/
Expand Down

0 comments on commit 672f192

Please sign in to comment.