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,40 @@ 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 @{
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.
130140 template <typename containedType>
131141 bool get_if_set (const std::string& key, containedType* result) const
132142 {
@@ -150,13 +160,16 @@ class AnyDictionary : private std::map<std::string, std::any>
150160 }
151161 }
152162
163+ // / @brief Return whether the dictionary contains the given key.
153164 inline bool has_key (const std::string& key) const
154165 {
155166 return (this ->find (key) != this ->end ());
156167 }
157168
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.
160173 template <typename containedType>
161174 bool set_default (const std::string& key, containedType* result)
162175 {
@@ -210,8 +223,10 @@ class AnyDictionary : private std::map<std::string, std::any>
210223 using map::size_type;
211224 using map::value_type;
212225
226+ // / @brief This struct provides a mutation time stamp.
213227 struct MutationStamp
214228 {
229+ // / @brief Create a new time stamp.
215230 constexpr MutationStamp (AnyDictionary* d) noexcept
216231 : stamp{ 1 }
217232 , any_dictionary{ d }
@@ -223,6 +238,7 @@ class AnyDictionary : private std::map<std::string, std::any>
223238 MutationStamp (MutationStamp const &) = delete ;
224239 MutationStamp& operator =(MutationStamp const &) = delete ;
225240
241+ // / @brief Destructor.
226242 ~MutationStamp ()
227243 {
228244 if (any_dictionary)
@@ -249,6 +265,7 @@ class AnyDictionary : private std::map<std::string, std::any>
249265 }
250266 };
251267
268+ // / @brief Get or crate a mutation time stamp.
252269 MutationStamp* get_or_create_mutation_stamp ()
253270 {
254271 if (!_mutation_stamp)
0 commit comments