Audacity 3.2.0
ClientDataHelpers.h
Go to the documentation of this file.
1/*!********************************************************************
2
3Audacity: A Digital Audio Editor
4
5@file ClientDataHelpers.h
6@brief Some implementation details for ClientData
7
8Paul Licameli
9
10**********************************************************************/
11
12#ifndef __AUDACITY_CLIENT_DATA_HELPERS__
13#define __AUDACITY_CLIENT_DATA_HELPERS__
14
15#include <memory>
16#include <mutex>
17#include <type_traits>
18
19namespace ClientData {
20 // Helpers to define ClientData::Site class template
21
23
28};
29
31
37};
38
39// forward declarations
40struct Base;
41template<
42 template<typename> class Owner
43> struct Cloneable;
44
46
47template< typename Ptr > static inline
48 const Ptr &Dereferenceable( Ptr &p )
49 { return p; }
51template< typename Obj > static inline
52 std::shared_ptr<Obj> Dereferenceable( std::weak_ptr<Obj> &p )
53 { return p.lock(); }
54
56
60template< typename Object, LockingPolicy > struct Lockable{};
62template< typename Object > struct Lockable< Object, NoLocking >
63: Object {
65 struct Lock{};
66 Lock lock() const { return {}; }
67};
69template< typename Object > struct Lockable< Object, NonrecursiveLocking >
70: Object, std::mutex {
71 using Lock = std::unique_lock< std::mutex >;
72 Lock lock() const { return Lock{ *this }; }
73};
75template< typename Object > struct Lockable< Object, RecursiveLocking >
76: Object, std::recursive_mutex {
77 using Lock = std::unique_lock< std::recursive_mutex >;
78 Lock lock() const { return Lock{ *this }; }
79};
80
82
83template< typename Lockable > struct Locked
84 : private Lockable::Lock
85{
86 explicit Locked( Lockable &object )
87 : Lockable::Lock( object.lock() )
88 , mObject( object )
89 {}
91};
92
94template< typename Container, CopyingPolicy > struct Copyable{};
96template< typename Container > struct Copyable< Container, SkipCopying >
97: Container {
98 Copyable() = default;
99 Copyable( const Copyable & ) {}
100 Copyable &operator=( const Copyable & ) { return *this; }
101 Copyable( Copyable && ) = default;
102 Copyable &operator=( Copyable&& ) = default;
103};
105template< typename Container > struct Copyable< Container, ShallowCopying >
106: Container {
107 Copyable() = default;
109 Copyable( const Copyable &other )
110 { *this = other; }
112 Copyable &operator=( const Copyable &other )
113 {
114 if (this != &other) {
115 // Build then swap for strong exception guarantee
116 Copyable temp;
117 for ( auto &&ptr : other )
118 temp.push_back( ptr );
119 this->swap( temp );
120 }
121 return *this;
122 }
123 Copyable( Copyable && ) = default;
124 Copyable &operator=( Copyable&& ) = default;
125};
127template< typename Container > struct Copyable< Container, DeepCopying >
128: Container {
129 Copyable() = default;
131 Copyable( const Copyable &other )
132 { *this = other; }
134 Copyable &operator=( const Copyable &other )
135 {
136 if (this != &other) {
137 // Build then swap for strong exception guarantee
138 Copyable temp;
139 for ( auto &&p : other ) {
140 using Ptr = decltype( p->Clone() );
141 temp.push_back( p ? p->Clone() : Ptr{} );
142 }
143 this->swap( temp );
144 }
145 return *this;
146 }
147 Copyable( Copyable && ) = default;
148 Copyable &operator=( Copyable&& ) = default;
149};
150
151}
152
153#endif
Utility ClientData::Site to register hooks into a host class that attach client data.
Definition: ClientData.h:24
static const Ptr & Dereferenceable(Ptr &p)
Conversion allowing operator * on any Pointer parameter of ClientData::Site.
CopyingPolicy
Statically specify how the ClientData::Site implements its copy constructor and assignment.
@ ShallowCopying
copy pointers only; won't compile for std::unique_ptr
@ SkipCopying
ignore the source and leave empty
@ DeepCopying
point to new sub-objects; these must define a Clone() member; won't compile for std::weak_ptr
LockingPolicy
Statically specify whether there is mutual exclusion (separately for the table of factories,...
@ RecursiveLocking
using std::recursive_mutex
@ NonrecursiveLocking
using std::mutex
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:752
Copyable & operator=(Copyable &&)=default
Copyable & operator=(const Copyable &other)
Copyable(const Copyable &other)
Call through to operator =.
Copyable(const Copyable &other)
Call through to operator =.
Copyable & operator=(Copyable &&)=default
Copyable & operator=(Copyable &&)=default
Decorator template injects copy and move operators for container of pointers.
std::unique_lock< std::recursive_mutex > Lock
Decorator template injects type Lock and method lock() into interface of Object.
Decorated reference to a ClientData::Lockable, with a current lock on it.
Locked(Lockable &object)