Audacity 3.2.0
XMLMethodRegistry.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 XMLMethodRegistry.cpp
6
7 Paul Licameli
8
9**********************************************************************/
10
11#include "XMLMethodRegistry.h"
12
13#include "Identifier.h"
14#include <wx/string.h>
15
18
20 std::string tag, TypeErasedObjectAccessor accessor )
21{
22 // Store string in a separate container from the map, so the map
23 // can be keyed by string_view.
24 // Beware small-string optimization! Be sure strings don't relocate for
25 // growth of the container. Use a list, not a vector.
26 auto &newtag = mTags.emplace_front(move(tag));
27 mTagTable[ newtag ] = move( accessor );
28}
29
31 const std::string_view &tag, void *p )
32{
33 const auto &table = mTagTable;
34 if (auto iter = table.find( tag ); iter != table.end())
35 if (auto &fn = iter->second)
36 return fn( p );
37 return nullptr;
38}
39
41{
42 mAccessors.emplace_back( move( accessor ) );
43}
44
46 std::string tag, TypeErasedMutator mutator )
47{
48 // Similar to the other overload of Register
49 auto &newtag = mMutatorTags.emplace_front(move(tag));
50 mMutatorTable[ newtag ] = { mAccessors.size() - 1, move( mutator ) };
51}
52
53bool XMLMethodRegistryBase::CallAttributeHandler( const std::string_view &tag,
54 void *p, const XMLAttributeValueView &value )
55{
56 const auto &table = mMutatorTable;
57 if (auto iter = table.find(tag); iter != table.end())
58 // Tag is known
59 if (auto &pair = iter->second;
60 pair.second && pair.first < mAccessors.size() )
61 // Mutator is not null and accessor exists
62 if (auto &accessor = mAccessors[pair.first])
63 // Accessor is not null; compose accessor and mutator
64 return pair.second( accessor( p ), value ), true;
65 return false;
66}
67
69{
70 mAttributeWriterTable.emplace_back( move( writer ) );
71}
72
74 const void *p, XMLWriter &writer )
75{
76 const auto &table = mAttributeWriterTable;
77 for ( auto &fn : table )
78 if (fn)
79 fn( p, writer );
80}
81
83{
84 mObjectWriterTable.emplace_back( move( writer ) );
85}
86
88 const void *p, XMLWriter &writer )
89{
90 const auto &table = mObjectWriterTable;
91 for ( auto &fn : table )
92 if (fn)
93 fn( p, writer );
94}
static const auto fn
A view into an attribute value. The class does not take the ownership of the data.
WriterTable mObjectWriterTable
TypeErasedAccessors mAccessors
void CallAttributeWriters(const void *p, XMLWriter &writer)
std::function< void(const void *, XMLWriter &) > TypeErasedWriter
std::forward_list< std::string > mMutatorTags
void RegisterObjectWriter(TypeErasedWriter writer)
void RegisterAttributeWriter(TypeErasedWriter writer)
std::function< XMLTagHandler *(void *) > TypeErasedObjectAccessor
std::function< void *(void *) > TypeErasedAccessor
bool CallAttributeHandler(const std::string_view &tag, void *p, const XMLAttributeValueView &value)
void PushAccessor(TypeErasedAccessor accessor)
void CallObjectWriters(const void *p, XMLWriter &writer)
std::forward_list< std::string > mTags
std::function< void(void *, const XMLAttributeValueView &) > TypeErasedMutator
WriterTable mAttributeWriterTable
XMLTagHandler * CallObjectAccessor(const std::string_view &tag, void *p)
void Register(std::string tag, TypeErasedObjectAccessor accessor)
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:42
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:26