12
12
13
13
namespace opentimelineio { namespace OPENTIMELINEIO_VERSION {
14
14
15
- /* *
16
- * An AnyDictionary has exactly the same API as
17
- * std::map<std::string, std::any>
18
- *
19
- * except that it records a "time-stamp" that bumps monotonically every time an
20
- * operation that would invalidate iterators is performed.
21
- * (This happens for operator=, clear, erase, insert, swap). The stamp also
22
- * lets external observers know when the map has been destroyed (which includes
23
- * the case of the map being relocated in memory).
24
- *
25
- * This allows us to hand out iterators that can be aware of mutation and moves
26
- * and take steps to safe-guard themselves from causing a crash. (Yes,
27
- * I'm talking to you, Python...)
28
- */
15
+ // / @brief This class provides a replacement for "std::map<std::string, std::any>".
16
+ // /
17
+ // / This class has exactly the same API as "std::map<std::string, std::any>",
18
+ // / except that it records a "time-stamp" that bumps monotonically every time an
19
+ // / operation that would invalidate iterators is performed (this happens for
20
+ // / operator =, clear, erase, insert, and swap). The stamp also lets external
21
+ // / observers know when the map has been destroyed (which includes the case of
22
+ // / the map being relocated in memory).
23
+ // /
24
+ // / This allows us to hand out iterators that can be aware of mutation and moves
25
+ // / and take steps to safe-guard themselves from causing a crash. (Yes, I'm
26
+ // / talking to you, Python...)
29
27
class AnyDictionary : private std ::map<std::string, std::any>
30
28
{
31
29
public:
32
30
using map::map;
33
31
32
+ // / @brief Create an empty dictionary.
34
33
AnyDictionary ()
35
34
: map{}
36
35
, _mutation_stamp{}
37
36
{}
38
37
39
- // to be safe, avoid brace-initialization so as to not trigger
40
- // list initialization behavior in older compilers:
38
+ // / @brief Create a copy of a dictionary.
39
+ // /
40
+ // / To be safe, avoid brace-initialization so as to not trigger
41
+ // / list initialization behavior in older compilers:
41
42
AnyDictionary (const AnyDictionary& other)
42
43
: map(other)
43
44
, _mutation_stamp{}
44
45
{}
45
46
47
+ // / @brief Destructor.
46
48
~AnyDictionary ()
47
49
{
48
50
if (_mutation_stamp)
@@ -52,13 +54,15 @@ class AnyDictionary : private std::map<std::string, std::any>
52
54
}
53
55
}
54
56
57
+ // / @brief Copy operator.
55
58
AnyDictionary& operator =(const AnyDictionary& other)
56
59
{
57
60
mutate ();
58
61
map::operator =(other);
59
62
return *this ;
60
63
}
61
64
65
+ // / @brief Move operator.
62
66
AnyDictionary& operator =(AnyDictionary&& other)
63
67
{
64
68
mutate ();
@@ -67,6 +71,7 @@ class AnyDictionary : private std::map<std::string, std::any>
67
71
return *this ;
68
72
}
69
73
74
+ // / @brief Copy operator.
70
75
AnyDictionary& operator =(std::initializer_list<value_type> ilist)
71
76
{
72
77
mutate ();
@@ -88,6 +93,7 @@ class AnyDictionary : private std::map<std::string, std::any>
88
93
using map::rbegin;
89
94
using map::rend;
90
95
96
+ // / @brief Clear the dictionary.
91
97
void clear () noexcept
92
98
{
93
99
mutate ();
@@ -97,36 +103,40 @@ class AnyDictionary : private std::map<std::string, std::any>
97
103
using map::emplace_hint;
98
104
using map::insert;
99
105
106
+ // / @brief Erase an item.
100
107
iterator erase (const_iterator pos)
101
108
{
102
109
mutate ();
103
110
return map::erase (pos);
104
111
}
105
112
113
+ // / @brief Erase a range of items.
106
114
iterator erase (const_iterator first, const_iterator last)
107
115
{
108
116
mutate ();
109
117
return map::erase (first, last);
110
118
}
111
119
120
+ // / @brief Erase an item with the given key.
112
121
size_type erase (const key_type& key)
113
122
{
114
123
mutate ();
115
124
return map::erase (key);
116
125
}
117
126
127
+ // / @brief Swap dictionaries.
118
128
void swap (AnyDictionary& other)
119
129
{
120
130
mutate ();
121
131
other.mutate ();
122
132
map::swap (other);
123
133
}
124
134
125
- // / @TODO: remove all of these @{
126
-
127
- // if key is in this, and the type of key matches the type of result, then
128
- // set result to the value of std::any_cast<type>(this[key]) and return true,
129
- // otherwise return false
135
+ // / @brief Return whether the given key has been set.
136
+ // /
137
+ // / If key is in this, and the type of key matches the type of result, then
138
+ // / set result to the value of std::any_cast<type>(this[key]) and return true,
139
+ // / otherwise return false.
130
140
template <typename containedType>
131
141
bool get_if_set (const std::string& key, containedType* result) const
132
142
{
@@ -150,13 +160,16 @@ class AnyDictionary : private std::map<std::string, std::any>
150
160
}
151
161
}
152
162
163
+ // / @brief Return whether the dictionary contains the given key.
153
164
inline bool has_key (const std::string& key) const
154
165
{
155
166
return (this ->find (key) != this ->end ());
156
167
}
157
168
158
- // if key is in this, place the value in result and return true, otherwise
159
- // store the value in result at key and return false
169
+ // / @brief Set the default for the given key.
170
+ // /
171
+ // / If key is in this, place the value in result and return true, otherwise
172
+ // / store the value in result at key and return false.
160
173
template <typename containedType>
161
174
bool set_default (const std::string& key, containedType* result)
162
175
{
@@ -210,8 +223,10 @@ class AnyDictionary : private std::map<std::string, std::any>
210
223
using map::size_type;
211
224
using map::value_type;
212
225
226
+ // / @brief This struct provides a mutation time stamp.
213
227
struct MutationStamp
214
228
{
229
+ // / @brief Create a new time stamp.
215
230
constexpr MutationStamp (AnyDictionary* d) noexcept
216
231
: stamp{ 1 }
217
232
, any_dictionary{ d }
@@ -223,6 +238,7 @@ class AnyDictionary : private std::map<std::string, std::any>
223
238
MutationStamp (MutationStamp const &) = delete ;
224
239
MutationStamp& operator =(MutationStamp const &) = delete ;
225
240
241
+ // / @brief Destructor.
226
242
~MutationStamp ()
227
243
{
228
244
if (any_dictionary)
@@ -249,6 +265,7 @@ class AnyDictionary : private std::map<std::string, std::any>
249
265
}
250
266
};
251
267
268
+ // / @brief Get or crate a mutation time stamp.
252
269
MutationStamp* get_or_create_mutation_stamp ()
253
270
{
254
271
if (!_mutation_stamp)
0 commit comments