1212
1313namespace opentimelineio { namespace OPENTIMELINEIO_VERSION {
1414
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...)
2927class AnyDictionary : private std ::map<std::string, std::any>
3028{
3129public:
3230 using map::map;
3331
32+ // / @brief Create an empty dictionary.
3433 AnyDictionary ()
3534 : map{}
3635 , _mutation_stamp{}
3736 {}
3837
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:
4142 AnyDictionary (const AnyDictionary& other)
4243 : map(other)
4344 , _mutation_stamp{}
4445 {}
4546
47+ // / @brief Destructor.
4648 ~AnyDictionary ()
4749 {
4850 if (_mutation_stamp)
@@ -52,13 +54,15 @@ class AnyDictionary : private std::map<std::string, std::any>
5254 }
5355 }
5456
57+ // / @brief Copy operator.
5558 AnyDictionary& operator =(const AnyDictionary& other)
5659 {
5760 mutate ();
5861 map::operator =(other);
5962 return *this ;
6063 }
6164
65+ // / @brief Move operator.
6266 AnyDictionary& operator =(AnyDictionary&& other)
6367 {
6468 mutate ();
@@ -67,6 +71,7 @@ class AnyDictionary : private std::map<std::string, std::any>
6771 return *this ;
6872 }
6973
74+ // / @brief Copy operator.
7075 AnyDictionary& operator =(std::initializer_list<value_type> ilist)
7176 {
7277 mutate ();
@@ -88,6 +93,7 @@ class AnyDictionary : private std::map<std::string, std::any>
8893 using map::rbegin;
8994 using map::rend;
9095
96+ // / @brief Clear the dictionary.
9197 void clear () noexcept
9298 {
9399 mutate ();
@@ -97,36 +103,44 @@ class AnyDictionary : private std::map<std::string, std::any>
97103 using map::emplace_hint;
98104 using map::insert;
99105
106+ // / @brief Erase an item.
100107 iterator erase (const_iterator pos)
101108 {
102109 mutate ();
103110 return map::erase (pos);
104111 }
105112
113+ // / @brief Erase a range of items.
106114 iterator erase (const_iterator first, const_iterator last)
107115 {
108116 mutate ();
109117 return map::erase (first, last);
110118 }
111119
120+ // / @brief Erase an item with the given key.
112121 size_type erase (const key_type& key)
113122 {
114123 mutate ();
115124 return map::erase (key);
116125 }
117126
127+ // / @brief Swap dictionaries.
118128 void swap (AnyDictionary& other)
119129 {
120130 mutate ();
121131 other.mutate ();
122132 map::swap (other);
123133 }
124134
125- // / @TODO: remove all of these @{
135+ // / @todo Remove all of these.
136+ // /
137+ // /@{
126138
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
139+ // / @brief Return whether the given key has been set.
140+ // /
141+ // / If key is in this, and the type of key matches the type of result, then
142+ // / set result to the value of std::any_cast<type>(this[key]) and return true,
143+ // / otherwise return false.
130144 template <typename containedType>
131145 bool get_if_set (const std::string& key, containedType* result) const
132146 {
@@ -150,13 +164,16 @@ class AnyDictionary : private std::map<std::string, std::any>
150164 }
151165 }
152166
167+ // / @brief Return whether the dictionary contains the given key.
153168 inline bool has_key (const std::string& key) const
154169 {
155170 return (this ->find (key) != this ->end ());
156171 }
157172
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
173+ // / @brief Set the default for the given key.
174+ // /
175+ // / If key is in this, place the value in result and return true, otherwise
176+ // / store the value in result at key and return false.
160177 template <typename containedType>
161178 bool set_default (const std::string& key, containedType* result)
162179 {
@@ -181,6 +198,8 @@ class AnyDictionary : private std::map<std::string, std::any>
181198 }
182199 }
183200
201+ // /@}
202+
184203 using map::empty;
185204 using map::max_size;
186205 using map::size;
@@ -210,8 +229,10 @@ class AnyDictionary : private std::map<std::string, std::any>
210229 using map::size_type;
211230 using map::value_type;
212231
232+ // / @brief This struct provides a mutation time stamp.
213233 struct MutationStamp
214234 {
235+ // / @brief Create a new time stamp.
215236 constexpr MutationStamp (AnyDictionary* d) noexcept
216237 : stamp{ 1 }
217238 , any_dictionary{ d }
@@ -223,6 +244,7 @@ class AnyDictionary : private std::map<std::string, std::any>
223244 MutationStamp (MutationStamp const &) = delete ;
224245 MutationStamp& operator =(MutationStamp const &) = delete ;
225246
247+ // / @brief Destructor.
226248 ~MutationStamp ()
227249 {
228250 if (any_dictionary)
@@ -249,6 +271,7 @@ class AnyDictionary : private std::map<std::string, std::any>
249271 }
250272 };
251273
274+ // / @brief Get or crate a mutation time stamp.
252275 MutationStamp* get_or_create_mutation_stamp ()
253276 {
254277 if (!_mutation_stamp)
0 commit comments