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
17
19 std::string tag, TypeErasedObjectAccessor accessor )
20{
21 // Store string in a separate container from the map, so the map
22 // can be keyed by string_view.
23 // Beware small-string optimization! Be sure strings don't relocate for
24 // growth of the container. Use a list, not a vector.
25 auto &newtag = mTags.emplace_front(move(tag));
26 mTagTable[ newtag ] = move( accessor );
27}
28
30 const std::string_view &tag, void *p )
31{
32 const auto &table = mTagTable;
33 if (auto iter = table.find( tag ); iter != table.end())
34 if (auto &fn = iter->second)
35 return fn( p );
36 return nullptr;
37}
38
40{
41 mAccessors.emplace_back( move( accessor ) );
42}
43
45 std::string tag, TypeErasedMutator mutator )
46{
47 // Similar to the other overload of Register
48 auto &newtag = mMutatorTags.emplace_front(move(tag));
49 mMutatorTable[ newtag ] = { mAccessors.size() - 1, move( mutator ) };
50}
51
52bool XMLMethodRegistryBase::CallAttributeHandler( const std::string_view &tag,
53 void *p, const XMLAttributeValueView &value )
54{
55 const auto &table = mMutatorTable;
56 if (auto iter = table.find(tag); iter != table.end())
57 // Tag is known
58 if (auto &pair = iter->second;
59 pair.second && pair.first < mAccessors.size() )
60 // Mutator is not null and accessor exists
61 if (auto &accessor = mAccessors[pair.first])
62 // Accessor is not null; compose accessor and mutator
63 return pair.second( accessor( p ), value ), true;
64 return false;
65}
66
68{
69 mAttributeWriterTable.emplace_back( move( writer ) );
70}
71
73 const void *p, XMLWriter &writer )
74{
75 const auto &table = mAttributeWriterTable;
76 for ( auto &fn : table )
77 if (fn)
78 fn( p, writer );
79}
80
82{
83 mObjectWriterTable.emplace_back( move( writer ) );
84}
85
87 const void *p, XMLWriter &writer )
88{
89 const auto &table = mObjectWriterTable;
90 for ( auto &fn : table )
91 if (fn)
92 fn( p, writer );
93}
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:25