Audacity  3.0.3
XMLMethodRegistry.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  XMLMethodRegistry.h
6 
7  Paul Licameli
8 
9 **********************************************************************/
10 
11 #ifndef __AUDACITY_XML_METHOD_REGISTRY__
12 #define __AUDACITY_XML_METHOD_REGISTRY__
13 
14 #include <wx/string.h>
15 #include <functional>
16 #include <type_traits>
17 #include <unordered_map>
18 #include <utility>
19 #include <vector>
20 
21 class XMLTagHandler;
22 class XMLWriter;
23 
25 
27 class XML_API XMLMethodRegistryBase {
28 public:
29 
31 template< typename Substructure >
32 using Mutator = std::function< void(Substructure&, const wchar_t *) >;
33 
35 template< typename Substructure >
36 using Mutators = std::vector< std::pair< wxString, Mutator<Substructure> > >;
37 
40 protected:
41  using TypeErasedObjectAccessor = std::function< XMLTagHandler *( void* ) >;
42  using TagTable =
43  std::unordered_map< wxString, TypeErasedObjectAccessor >;
45 
46  void Register( const wxString &tag, TypeErasedObjectAccessor accessor );
47  XMLTagHandler *CallObjectAccessor( const wxString &tag, void *p );
48 
49  using TypeErasedAccessor = std::function< void*( void* ) >;
50  using TypeErasedAccessors = std::vector< TypeErasedAccessor >;
52 
53  void PushAccessor( TypeErasedAccessor accessor );
54 
55  using TypeErasedMutator = std::function< void( void*, const wchar_t* ) >;
57  using MutatorTable =
58  std::unordered_map< wxString, std::pair< size_t, TypeErasedMutator > >;
60 
61  void Register( const wxString &tag, TypeErasedMutator mutator );
62 
63  bool CallAttributeHandler( const wxString &tag,
64  void *p, const wchar_t *value );
65 
66  using TypeErasedWriter = std::function< void(const void *, XMLWriter &) >;
67  using WriterTable = std::vector< TypeErasedWriter >;
69 
70  void Register( TypeErasedWriter writer );
71  void CallWriters( const void *p, XMLWriter &writer );
72 };
73 
77 template< typename Host >
79 public:
80 
81  // Typically statically constructed
83  template <
89  typename ObjectAccessor
91  >
92  ObjectReaderEntry( const wxString &tag, ObjectAccessor fn )
93  {
94  // Remember the function, type-erased
95  Get().Register( tag, [ fn = std::move(fn) ] (void *p) {
96  // CallObjectAccessor will guarantee p is not null
97  return fn( *static_cast<Host *>(p) );
98  } );
99  }
100 };
101 
103  const wxString &tag, Host &host )
104 {
105  return XMLMethodRegistryBase::CallObjectAccessor( tag, &host );
106 }
107 
117  template<
118  typename Accessor,
120  typename Substructure //<! Type deduction of the return of Accessor
121  = std::remove_reference_t< decltype(
122  std::declval<Accessor>()( std::declval<Host &>() )
123  ) >
124  >
126  {
127  // Remember the functions, type-erased
128  auto &registry = Get();
129  registry.PushAccessor(
130  [ fn = std::move(fn) ] ( void *p ) {
131  // CallAttributeHandler will guarantee p is not null
132  return &fn( *static_cast<Host *>(p) ); }
133  );
134  for (auto &pair : pairs)
135  registry.Register( pair.first,
136  [ fn = move(pair.second) ]( auto p, auto value ){
137  fn( *static_cast<Substructure*>(p), value ); }
138  );
139  }
140 };
141 
142 // @return whether any function was found and called for the tag
144  const wxString &tag, Host &host, const wchar_t *value )
145 {
146  return XMLMethodRegistryBase::CallAttributeHandler( tag, &host, value );
147 }
148 
150 struct WriterEntry {
151  template <
159  typename Writer
161  >
162  explicit WriterEntry( Writer fn )
163  {
164  // Remember the function, type-erased
165  Get().Register(
166  [ fn = std::move(fn) ] ( const void *p, XMLWriter &writer ) {
167  // CallObjectAccessor will guarantee p is not null
168  return fn( *static_cast<const Host *>(p), writer );
169  } );
170  }
171 };
172 
173 void CallWriters( const Host &host, XMLWriter &writer )
174 {
175  XMLMethodRegistryBase::CallWriters( &host, writer );
176 }
177 
180 
181 };
182 
185 #define DECLARE_XML_METHOD_REGISTRY(DECLSPEC, Name) \
186  template<> auto DECLSPEC Name::Get() -> Name &;
187 
189 #define DEFINE_XML_METHOD_REGISTRY(Name) \
190  template<> auto Name::Get() -> Name & \
191  { \
192  static Name registry; \
193  return registry; \
194  }
195 
196 #endif
XMLWriter
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:23
XMLMethodRegistryBase::Mutators
std::vector< std::pair< wxString, Mutator< Substructure > > > Mutators
A helper type alias for a list of mutators, associated with tag strings.
Definition: XMLMethodRegistry.h:36
fn
static const auto fn
Definition: WaveformView.cpp:1108
XMLMethodRegistry::CallAttributeHandler
bool CallAttributeHandler(const wxString &tag, Host &host, const wchar_t *value)
Definition: XMLMethodRegistry.h:143
XMLMethodRegistry::ObjectReaderEntry
Definition: XMLMethodRegistry.h:82
XMLMethodRegistryBase::mWriterTable
WriterTable mWriterTable
Definition: XMLMethodRegistry.h:68
XMLMethodRegistryBase::~XMLMethodRegistryBase
~XMLMethodRegistryBase()
XMLMethodRegistry::WriterEntry
Typically statically constructed.
Definition: XMLMethodRegistry.h:150
XMLMethodRegistryBase::TypeErasedWriter
std::function< void(const void *, XMLWriter &) > TypeErasedWriter
Definition: XMLMethodRegistry.h:66
XMLMethodRegistryBase::TagTable
std::unordered_map< wxString, TypeErasedObjectAccessor > TagTable
Definition: XMLMethodRegistry.h:43
XMLMethodRegistryBase::mAccessors
TypeErasedAccessors mAccessors
Definition: XMLMethodRegistry.h:51
XMLMethodRegistryBase
Implementation helper for ProjectFileIORegistry.
Definition: XMLMethodRegistry.h:27
XMLMethodRegistryBase::TypeErasedAccessor
std::function< void *(void *) > TypeErasedAccessor
Definition: XMLMethodRegistry.h:49
XMLMethodRegistryBase::mTagTable
TagTable mTagTable
Definition: XMLMethodRegistry.h:44
XMLMethodRegistry::WriterEntry::WriterEntry
WriterEntry(Writer fn)
Definition: XMLMethodRegistry.h:162
XMLMethodRegistryBase::CallObjectAccessor
XMLTagHandler * CallObjectAccessor(const wxString &tag, void *p)
Definition: XMLMethodRegistry.cpp:25
XMLMethodRegistry
Definition: XMLMethodRegistry.h:78
XMLMethodRegistry::CallObjectAccessor
XMLTagHandler * CallObjectAccessor(const wxString &tag, Host &host)
Definition: XMLMethodRegistry.h:102
XMLMethodRegistryBase::TypeErasedObjectAccessor
std::function< XMLTagHandler *(void *) > TypeErasedObjectAccessor
Definition: XMLMethodRegistry.h:41
XMLTagHandler
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:62
XMLMethodRegistry::ObjectReaderEntry::ObjectReaderEntry
ObjectReaderEntry(const wxString &tag, ObjectAccessor fn)
Definition: XMLMethodRegistry.h:92
XMLMethodRegistryBase::MutatorTable
std::unordered_map< wxString, std::pair< size_t, TypeErasedMutator > > MutatorTable
From attribute name, to index in accessor table with a mutator.
Definition: XMLMethodRegistry.h:58
XMLMethodRegistry::Get
static XMLMethodRegistry & Get()
Get the unique instance.
XMLMethodRegistry::AttributeReaderEntries::AttributeReaderEntries
AttributeReaderEntries(Accessor fn, Mutators< Substructure > pairs)
Definition: XMLMethodRegistry.h:125
XMLMethodRegistryBase::mMutatorTable
MutatorTable mMutatorTable
Definition: XMLMethodRegistry.h:59
XMLMethodRegistryBase::TypeErasedAccessors
std::vector< TypeErasedAccessor > TypeErasedAccessors
Definition: XMLMethodRegistry.h:50
XMLMethodRegistryBase::Register
void Register(const wxString &tag, TypeErasedObjectAccessor accessor)
Definition: XMLMethodRegistry.cpp:19
XMLMethodRegistryBase::CallWriters
void CallWriters(const void *p, XMLWriter &writer)
Definition: XMLMethodRegistry.cpp:66
XMLMethodRegistryBase::WriterTable
std::vector< TypeErasedWriter > WriterTable
Definition: XMLMethodRegistry.h:67
XMLMethodRegistry::CallWriters
void CallWriters(const Host &host, XMLWriter &writer)
Definition: XMLMethodRegistry.h:173
XMLMethodRegistryBase::XMLMethodRegistryBase
XMLMethodRegistryBase()
XMLMethodRegistry::AttributeReaderEntries
Definition: XMLMethodRegistry.h:116
XMLMethodRegistryBase::CallAttributeHandler
bool CallAttributeHandler(const wxString &tag, void *p, const wchar_t *value)
Definition: XMLMethodRegistry.cpp:46
XMLMethodRegistryBase::Mutator
std::function< void(Substructure &, const wchar_t *) > Mutator
A helper type alias for a function taking a structure and a string value.
Definition: XMLMethodRegistry.h:32
XMLMethodRegistryBase::TypeErasedMutator
std::function< void(void *, const wchar_t *) > TypeErasedMutator
Definition: XMLMethodRegistry.h:55