-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCxxVectorProperty.h
More file actions
147 lines (121 loc) · 3.76 KB
/
CxxVectorProperty.h
File metadata and controls
147 lines (121 loc) · 3.76 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
//
// CxxVectorProperty.h
// otio-swift
//
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenTimelineIO project
#import <Foundation/Foundation.h>
#import "CxxRetainer.h"
#if defined(__cplusplus)
#import <opentimelineio/serializableObject.h>
namespace otio = opentimelineio::OPENTIMELINEIO_VERSION_NS;
class CxxSOVectorBase {
public:
CxxSOVectorBase();
CxxSOVectorBase& operator=(CxxSOVectorBase const&) = delete;
CxxSOVectorBase(CxxSOVectorBase const&) = delete;
virtual ~CxxSOVectorBase();
virtual otio::SerializableObject* _Nullable fetch(int index) = 0;
virtual int size() = 0;
virtual void clear() = 0;
virtual void store(int index, otio::SerializableObject* _Nonnull) = 0;
virtual void moveIndex(int fromIndex, int toIndex) = 0;
virtual void removeAtEnd() = 0;
virtual void append(otio::SerializableObject* _Nonnull) = 0;
virtual void shrinkOrGrow(int n, bool grow) = 0;
virtual void setContents(CxxSOVectorBase* _Nonnull src, bool destroyingSrc) = 0;
};
template <typename T>
class CxxSOVector : public CxxSOVectorBase {
public:
CxxSOVector()
: _v{* new std::vector<otio::SerializableObject::Retainer<T>>}, _owner{true}
{
}
CxxSOVector(std::vector<otio::SerializableObject::Retainer<T>>& v)
: _v{v}, _owner{false} {
}
virtual otio::SerializableObject* _Nullable fetch(int index) {
return index < _v.size() ? _v[index] : nullptr;
}
virtual int size() {
return int(_v.size());
}
virtual ~CxxSOVector() {
if (_owner) {
delete &_v;
}
}
virtual void clear() {
_v.clear();
}
virtual void store(int index, otio::SerializableObject* _Nonnull so) {
if (index >= 0 && index < _v.size()) {
_v[index] = std::move(otio::SerializableObject::Retainer<T>((T*)so));
}
else {
_v.emplace_back(otio::SerializableObject::Retainer<T>((T*)so));
}
}
virtual void moveIndex(int fromIndex, int toIndex) {
std::swap(_v[fromIndex], _v[toIndex]);
}
virtual void append(otio::SerializableObject* _Nonnull so) {
_v.emplace_back(otio::SerializableObject::Retainer<T>((T*)so));
}
virtual void removeAtEnd() {
_v.pop_back();
}
virtual void shrinkOrGrow(int n, bool grow) {
if (grow) {
for (int i = 0; i < n; i++) {
_v.push_back(otio::SerializableObject::Retainer<T>());
}
}
else {
if (n >= _v.size()) {
_v.clear();
return;
}
for (int i = 0; i < n; i++) {
_v.pop_back();
}
}
}
virtual void setContents(CxxSOVectorBase* _Nonnull src, bool destroyingSrc) {
CxxSOVector* typedSrc = (CxxSOVector*) src;
if (destroyingSrc) {
std::swap(_v, typedSrc->_v);
}
else {
_v = typedSrc->_v;
}
}
private:
std::vector<otio::SerializableObject::Retainer<T>>& _v;
bool _owner;
};
#endif
NS_ASSUME_NONNULL_BEGIN
@interface CxxVectorProperty : NSObject
- (instancetype) init;
- (int) count;
- (void* _Nullable) cxxSerializableObjectAtIndex:(int) index;
- (void) clear;
- (void) store:(int) index
value:(void*) cxxSerializableObject;
- (void) moveIndex:(int) fromIndex
toIndex:(int) toIndex;
- (void) addAtEnd:(void*) cxxSerializableObject;
- (void) shrinkOrGrow:(int) n
grow:(bool) grow;
- (void) copyContents:(CxxVectorProperty*) src;
- (void) moveContents:(CxxVectorProperty*) src;
@property(weak) CxxRetainer* cxxRetainer;
@end
#if defined(__cplusplus)
@interface CxxVectorProperty ()
@property CxxSOVectorBase* _Nullable cxxVectorBase;
@end
#endif
NS_ASSUME_NONNULL_END