Audacity  3.0.3
ClientDataHelpers.h
Go to the documentation of this file.
1 /*!********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 @file ClientDataHelpers.h
6 @brief Some implementation details for ClientData
7 
8 Paul 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 
19 namespace ClientData {
20  // Helpers to define ClientData::Site class template
21 
23 
28 };
29 
31 
37 };
38 
39 // forward declarations
40 struct Base;
41 template<
42  template<typename> class Owner
43 > struct Cloneable;
44 
46 
47 template< typename Ptr > static inline
48  const Ptr &Dereferenceable( Ptr &p )
49  { return p; }
51 template< typename Obj > static inline
52  std::shared_ptr<Obj> Dereferenceable( std::weak_ptr<Obj> &p )
53  { return p.lock(); }
54 
56 
60 template< typename Object, LockingPolicy > struct Lockable{};
62 template< typename Object > struct Lockable< Object, NoLocking >
63 : Object {
65  struct Lock{};
66  Lock lock() const { return {}; }
67 };
69 template< 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 };
75 template< 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 
83 template< 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 
94 template< typename Container, CopyingPolicy > struct Copyable{};
96 template< 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 };
105 template< 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 };
127 template< 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
ClientData::Copyable< Container, ShallowCopying >::operator=
Copyable & operator=(Copyable &&)=default
ClientData::Locked
Decorated reference to a ClientData::Lockable, with a current lock on it.
Definition: ClientDataHelpers.h:85
ClientData::Lockable< Object, RecursiveLocking >::lock
Lock lock() const
Definition: ClientDataHelpers.h:78
ClientData::Copyable< Container, ShallowCopying >::Copyable
Copyable(const Copyable &other)
Call through to operator =.
Definition: ClientDataHelpers.h:109
ClientData::Copyable< Container, DeepCopying >::operator=
Copyable & operator=(Copyable &&)=default
ClientData::ShallowCopying
@ ShallowCopying
copy pointers only; won't compile for std::unique_ptr
Definition: ClientDataHelpers.h:35
ClientData::Locked::mObject
Lockable & mObject
Definition: ClientDataHelpers.h:90
ClientData::Lockable< Object, RecursiveLocking >::Lock
std::unique_lock< std::recursive_mutex > Lock
Definition: ClientDataHelpers.h:77
ClientData::Copyable< Container, SkipCopying >::operator=
Copyable & operator=(Copyable &&)=default
ClientData::Copyable< Container, SkipCopying >::operator=
Copyable & operator=(const Copyable &)
Definition: ClientDataHelpers.h:100
ClientData::Copyable< Container, DeepCopying >::Copyable
Copyable()=default
ClientData::Copyable< Container, DeepCopying >::Copyable
Copyable(const Copyable &other)
Call through to operator =.
Definition: ClientDataHelpers.h:131
ClientData::SkipCopying
@ SkipCopying
ignore the source and leave empty
Definition: ClientDataHelpers.h:34
ClientData::LockingPolicy
LockingPolicy
Statically specify whether there is mutual exclusion (separately for the table of factories,...
Definition: ClientDataHelpers.h:24
ClientData::Lockable
Decorator template injects type Lock and method lock() into interface of Object.
Definition: ClientDataHelpers.h:60
ClientData::Lockable< Object, NonrecursiveLocking >::lock
Lock lock() const
Definition: ClientDataHelpers.h:72
ClientData::NoLocking
@ NoLocking
Definition: ClientDataHelpers.h:25
ClientData::Locked::Locked
Locked(Lockable &object)
Definition: ClientDataHelpers.h:86
ClientData::Copyable< Container, ShallowCopying >::Copyable
Copyable(Copyable &&)=default
ClientData::NonrecursiveLocking
@ NonrecursiveLocking
using std::mutex
Definition: ClientDataHelpers.h:26
ClientData::Copyable< Container, SkipCopying >::Copyable
Copyable(Copyable &&)=default
ClientData::RecursiveLocking
@ RecursiveLocking
using std::recursive_mutex
Definition: ClientDataHelpers.h:27
ClientData::Copyable< Container, DeepCopying >::operator=
Copyable & operator=(const Copyable &other)
Definition: ClientDataHelpers.h:134
anonymous_namespace{NoteTrack.cpp}::swap
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:735
ClientData::Copyable
Decorator template injects copy and move operators for container of pointers.
Definition: ClientDataHelpers.h:94
ClientData::Copyable< Container, SkipCopying >::Copyable
Copyable()=default
ClientData::CopyingPolicy
CopyingPolicy
Statically specify how the ClientData::Site implements its copy constructor and assignment.
Definition: ClientDataHelpers.h:33
ClientData::Copyable< Container, DeepCopying >::Copyable
Copyable(Copyable &&)=default
ClientData::Copyable< Container, ShallowCopying >::operator=
Copyable & operator=(const Copyable &other)
Definition: ClientDataHelpers.h:112
ClientData::Copyable< Container, SkipCopying >::Copyable
Copyable(const Copyable &)
Definition: ClientDataHelpers.h:99
ClientData::Lockable< Object, NonrecursiveLocking >::Lock
std::unique_lock< std::mutex > Lock
Definition: ClientDataHelpers.h:71
ClientData
Utility ClientData::Site to register hooks into a host class that attach client data.
Definition: ClientData.h:24
ClientData::DeepCopying
@ DeepCopying
point to new sub-objects; these must define a Clone() member; won't compile for std::weak_ptr
Definition: ClientDataHelpers.h:36
ClientData::Lockable< Object, NoLocking >::lock
Lock lock() const
Definition: ClientDataHelpers.h:66
ClientData::Copyable< Container, ShallowCopying >::Copyable
Copyable()=default
ClientData::Dereferenceable
static const Ptr & Dereferenceable(Ptr &p)
Conversion allowing operator * on any Pointer parameter of ClientData::Site.
Definition: ClientDataHelpers.h:48