-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathtypeRegistry.cpp
More file actions
500 lines (444 loc) · 15.3 KB
/
Copy pathtypeRegistry.cpp
File metadata and controls
500 lines (444 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenTimelineIO project
#include "opentimelineio/typeRegistry.h"
#include "anyDictionary.h"
#include "opentimelineio/clip.h"
#include "opentimelineio/composable.h"
#include "opentimelineio/composition.h"
#include "opentimelineio/effect.h"
#include "opentimelineio/externalReference.h"
#include "opentimelineio/freezeFrame.h"
#include "opentimelineio/gap.h"
#include "opentimelineio/generatorReference.h"
#include "opentimelineio/imageSequenceReference.h"
#include "opentimelineio/item.h"
#include "opentimelineio/linearTimeWarp.h"
#include "opentimelineio/marker.h"
#include "opentimelineio/mediaReference.h"
#include "opentimelineio/missingReference.h"
#include "opentimelineio/serializableCollection.h"
#include "opentimelineio/serializableObject.h"
#include "opentimelineio/serializableObjectWithMetadata.h"
#include "opentimelineio/stack.h"
#include "opentimelineio/timeEffect.h"
#include "opentimelineio/timeline.h"
#include "opentimelineio/track.h"
#include "opentimelineio/transition.h"
#include "opentimelineio/unknownSchema.h"
#include "stringUtils.h"
#include <algorithm>
#include <assert.h>
#include <cctype>
#include <map>
#include <set>
#include <vector>
namespace opentimelineio { namespace OPENTIMELINEIO_VERSION_NS {
TypeRegistry&
TypeRegistry::TypeRegistry::instance()
{
static TypeRegistry r;
return r;
}
TypeRegistry::TypeRegistry()
{
register_type(
UnknownSchema::Schema::name,
UnknownSchema::Schema::version,
&typeid(UnknownSchema),
[]() {
fatal_error(
"UnknownSchema should not be created from type registry");
return nullptr;
},
"UnknownSchema");
register_type<Clip>();
register_type<Composable>();
register_type<Composition>();
register_type<Effect>();
register_type<ExternalReference>();
register_type<FreezeFrame>();
register_type<Gap>();
register_type_from_existing_type("Filler", 1, "Gap", nullptr);
register_type<GeneratorReference>();
register_type<ImageSequenceReference>();
register_type<Item>();
register_type<LinearTimeWarp>();
register_type<Marker>();
register_type<MediaReference>();
register_type<MissingReference>();
register_type<SerializableObject>();
register_type<SerializableObjectWithMetadata>();
register_type<SerializableCollection>();
register_type_from_existing_type(
"SerializeableCollection",
1,
"SerializableCollection",
nullptr);
register_type<Stack>();
register_type<TimeEffect>();
register_type<Timeline>();
register_type<Track>();
register_type_from_existing_type("Sequence", 1, "Track", nullptr);
register_type<Transition>();
/*
* Upgrade functions:
*/
register_upgrade_function(Marker::Schema::name, 2, [](AnyDictionary* d) {
(*d)["marked_range"] = (*d)["range"];
d->erase("range");
});
// 2 - 3
register_upgrade_function(Marker::Schema::name, 3, [](AnyDictionary* d) {
// get color name
std::string color_name_v2;
if (d->get_if_set("color", &color_name_v2))
{
static const std::map<std::string, Color> color_map = {
{ "PINK", Color::pink },
{ "RED", Color::red },
{ "ORANGE", Color::orange },
{ "YELLOW", Color::yellow },
{ "GREEN", Color::green },
{ "CYAN", Color::cyan },
{ "BLUE", Color::blue },
{ "MAGENTA", Color::magenta },
{ "PURPLE", Color::purple },
{ "BLACK", Color::black },
{ "WHITE", Color::white },
{ "TRANSPARENT", Color::transparent }
};
// make copy of color_name_v2, for uppercase change to be separate
std::string color_name_v2_upper = color_name_v2;
// force color name to uppercase for lookup
// since v2 color names were case-insensitive
std::transform(
color_name_v2_upper.begin(),
color_name_v2_upper.end(),
color_name_v2_upper.begin(),
[](unsigned char c) { return std::toupper(c); }
);
// if all-caps name matches a known color, convert to color with r,g,b,a, and name fields
auto it = color_map.find(color_name_v2_upper);
Color color_match = Color::white;
std::string color_match_name = "";
if (it != color_map.end()) { // match found
color_match = it->second;
color_match_name = color_match.name();
}
else { // no match, default to white but keep original name
color_match = Color::white;
color_match_name = color_name_v2;
}
(*d)["color"] = Color(
color_match.r(),
color_match.g(),
color_match.b(),
color_match.a(),
color_match_name
);
}
});
register_upgrade_function(Clip::Schema::name, 2, [](AnyDictionary* d) {
auto media_ref = (*d)["media_reference"];
// The default ctor of Clip used to set media_reference to
// MissingReference. To preserve the same behaviour, if we don't have a
// valid MediaReference, do it here too.
if (media_ref.type() != typeid(SerializableObject::Retainer<>))
{
media_ref = SerializableObject::Retainer<>(new MissingReference);
}
(*d)["media_references"] =
AnyDictionary{ { Clip::default_media_key, media_ref } };
(*d)["active_media_reference_key"] =
std::string(Clip::default_media_key);
d->erase("media_reference");
});
// 3->2
register_downgrade_function(Marker::Schema::name, 3, [](AnyDictionary* d) {
AnyDictionary color_dict;
if (d->get_if_set("color", &color_dict))
{
std::string color_name = "";
color_dict.get_if_set("name", &color_name);
// if the name matches case-insensitive to a known color,
// set the color an all-caps version of that name
if (!color_name.empty())
{
// Convert to uppercase for comparison and storage
std::string upper_name = color_name;
std::transform(
upper_name.begin(),
upper_name.end(),
upper_name.begin(),
[](unsigned char c) { return std::toupper(c); }
);
// Known color names - these are the valid color enum values
static const std::set<std::string> known_colors = {
"RED", "GREEN", "BLUE", "YELLOW", "CYAN", "MAGENTA",
"PINK", "ORANGE", "PURPLE", "BLACK", "WHITE", "TRANSPARENT"
};
if (known_colors.find(upper_name) != known_colors.end())
{
// remove color object and replace with color name string
(*d)["color"] = upper_name;
}
else { // otherwise, keep name as-is
(*d)["color"] = color_name;
}
}
}
});
// 2->1
register_downgrade_function(Clip::Schema::name, 2, [](AnyDictionary* d) {
AnyDictionary mrefs;
std::string active_rkey = "";
if (d->get_if_set("media_references", &mrefs))
{
if (d->get_if_set("active_media_reference_key", &active_rkey))
{
AnyDictionary active_ref;
if (mrefs.get_if_set(active_rkey, &active_ref))
{
(*d)["media_reference"] = active_ref;
}
}
}
d->erase("media_references");
d->erase("active_media_reference_key");
});
}
bool
TypeRegistry::register_type(
std::string const& schema_name,
int schema_version,
std::type_info const* type,
std::function<SerializableObject*()> create,
std::string const& class_name)
{
std::lock_guard<std::mutex> lock(_registry_mutex);
// auto existing_tr = _find_type_record(schema_name);
//
// // if the exact type record has already been added (happens in unit tests
// // and re-setting manifest stuff)
// if (existing_tr)
// {
// if (
// existing_tr->schema_name == schema_name
// && existing_tr->schema_version == schema_version
// && existing_tr->class_name == class_name
// && (
// existing_tr->create.target<SerializableObject*()>()
// == create.target<SerializableObject*()>()
// )
// ) {
// return true;
// }
// }
if (!_find_type_record(schema_name))
{
_TypeRecord* r =
new _TypeRecord{ schema_name, schema_version, class_name, create };
_type_records[schema_name] = r;
if (type)
{
_type_records_by_type_name[type->name()] = r;
}
return true;
}
return false;
}
bool
TypeRegistry::register_type_from_existing_type(
std::string const& schema_name,
int /* schema_version */,
std::string const& existing_schema_name,
ErrorStatus* error_status)
{
std::lock_guard<std::mutex> lock(_registry_mutex);
if (auto r = _find_type_record(existing_schema_name))
{
if (!_find_type_record(schema_name))
{
_type_records[schema_name] = new _TypeRecord{ r->schema_name,
r->schema_version,
r->class_name,
r->create };
return true;
}
if (error_status)
{
*error_status = ErrorStatus(
ErrorStatus::SCHEMA_ALREADY_REGISTERED,
schema_name);
}
return false;
}
if (error_status)
{
*error_status = ErrorStatus(
ErrorStatus::SCHEMA_NOT_REGISTERED,
string_printf(
"cannot define schema %s in terms of %s; %s has not been registered",
schema_name.c_str(),
existing_schema_name.c_str(),
existing_schema_name.c_str()));
}
return false;
}
bool
TypeRegistry::register_upgrade_function(
std::string const& schema_name,
int version_to_upgrade_to,
std::function<void(AnyDictionary*)> upgrade_function)
{
std::lock_guard<std::mutex> lock(_registry_mutex);
if (auto r = _find_type_record(schema_name))
{
auto result = r->upgrade_functions.insert(
{ version_to_upgrade_to, upgrade_function });
return result.second;
}
return false;
}
bool
TypeRegistry::register_downgrade_function(
std::string const& schema_name,
int version_to_downgrade_from,
std::function<void(AnyDictionary*)> downgrade_function)
{
std::lock_guard<std::mutex> lock(_registry_mutex);
if (auto r = _find_type_record(schema_name))
{
auto result = r->downgrade_functions.insert(
{ version_to_downgrade_from, downgrade_function });
return result.second;
}
return false;
}
SerializableObject*
TypeRegistry::_instance_from_schema(
std::string schema_name,
int schema_version,
AnyDictionary& dict,
bool internal_read,
ErrorStatus* error_status)
{
_TypeRecord const* type_record;
bool create_unknown = false;
{
std::lock_guard<std::mutex> lock(_registry_mutex);
type_record = _find_type_record(schema_name);
if (!type_record)
{
create_unknown = true;
type_record = _find_type_record(UnknownSchema::Schema::name);
assert(type_record);
}
}
SerializableObject* so;
if (create_unknown)
{
so = new UnknownSchema(schema_name, schema_version);
schema_name = type_record->schema_name;
schema_version = type_record->schema_version;
}
else
{
so = type_record->create_object();
}
if (schema_version > type_record->schema_version)
{
if (error_status)
{
*error_status = ErrorStatus(
ErrorStatus::SCHEMA_VERSION_UNSUPPORTED,
string_printf(
"Schema %s has highest version %d, but the requested "
"schema version %d is even greater.",
schema_name.c_str(),
type_record->schema_version,
schema_version));
}
return nullptr;
}
else if (schema_version < type_record->schema_version)
{
for (const auto& e: type_record->upgrade_functions)
{
if (schema_version <= e.first
&& e.first <= type_record->schema_version)
{
e.second(&dict);
}
}
}
if (internal_read)
{
return so;
}
auto error_function = [error_status](ErrorStatus const& status) {
if (error_status)
{
*error_status = status;
}
};
// g++ compiler bug if we pass error_function directly into Reader
std::function<void(ErrorStatus const&)> ef = error_function;
SerializableObject::Reader r(dict, ef, nullptr);
return so->read_from(r) ? so : nullptr;
}
TypeRegistry::_TypeRecord*
TypeRegistry::_lookup_type_record(std::string const& schema_name)
{
std::lock_guard<std::mutex> lock(_registry_mutex);
auto e = _type_records.find(schema_name);
return e != _type_records.end() ? e->second : nullptr;
}
TypeRegistry::_TypeRecord*
TypeRegistry::_lookup_type_record(std::type_info const& type)
{
std::lock_guard<std::mutex> lock(_registry_mutex);
auto e = _type_records_by_type_name.find(type.name());
return e != _type_records_by_type_name.end() ? e->second : nullptr;
}
SerializableObject*
TypeRegistry::_TypeRecord::create_object() const
{
SerializableObject* so = create();
so->_set_type_record(this);
return so;
}
bool
TypeRegistry::set_type_record(
SerializableObject* so,
std::string const& schema_name,
ErrorStatus* error_status)
{
auto r = _lookup_type_record(schema_name);
if (r)
{
so->_set_type_record(r);
return true;
}
if (error_status)
{
*error_status = ErrorStatus(
ErrorStatus::SCHEMA_NOT_REGISTERED,
string_printf(
"Cannot set type record on instance of type %s: schema %s unregistered",
type_name_for_error_message(so).c_str(),
schema_name.c_str()));
}
return false;
}
void
TypeRegistry::type_version_map(schema_version_map& result)
{
std::lock_guard<std::mutex> lock(_registry_mutex);
for (const auto& pair: _type_records)
{
const auto record_ptr = pair.second;
result[record_ptr->schema_name] = record_ptr->schema_version;
}
}
}} // namespace opentimelineio::OPENTIMELINEIO_VERSION_NS