This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
/
sciter-om.h
218 lines (174 loc) · 6.27 KB
/
sciter-om.h
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
#pragma once
#ifndef __SCITER_OM_H__
#define __SCITER_OM_H__
struct som_passport_t;
typedef UINT64 som_atom_t;
struct som_asset_t;
struct som_asset_class_t {
long(*asset_add_ref)(som_asset_t* thing);
long(*asset_release)(som_asset_t* thing);
long(*asset_get_interface)(som_asset_t* thing, const char* name, void** out);
som_passport_t* (*asset_get_passport)(som_asset_t* thing);
};
struct som_asset_t {
som_asset_class_t* isa;
};
inline som_asset_class_t* som_asset_get_class(const som_asset_t* pass)
{
return pass ? pass->isa : nullptr;
}
som_atom_t SCAPI SciterAtomValue(const char* name);
#ifdef CPP11
#include <cstring>
#include <cassert>
#include <atomic>
namespace sciter {
class atom {
som_atom_t _atom;
public:
atom(const char* name) { _atom = SciterAtomValue(name); }
atom(const atom& other) { _atom = other._atom; }
operator som_atom_t() const { return _atom; }
};
namespace om {
template <class R> class hasset;
// implementation of som_asset_t ISA
// note: does not define asset_add_ref()/asset_release() as they shall be defined in specializations
template <class A>
class iasset : public som_asset_t
{
public:
iasset() { isa = get_asset_class(); }
// mandatory:
virtual long asset_add_ref() = 0;
virtual long asset_release() = 0;
// can be used to as to get interface as to check if the thing supports the feature
virtual long asset_get_interface(const char* name, void** out = nullptr) {
if (0 != strcmp(name, interface_name())) return false;
if (out) { this->asset_add_ref(); *out = this; }
return true;
}
virtual som_passport_t* asset_get_passport() const {
return nullptr;
}
static som_asset_class_t* get_asset_class() {
static som_asset_class_t cls = {
&thunk_asset_add_ref,
&thunk_asset_release,
&thunk_asset_get_interface,
&thunk_asset_get_passport
};
return &cls;
};
static long thunk_asset_add_ref(som_asset_t* thing) { return static_cast<A*>(thing)->asset_add_ref(); }
static long thunk_asset_release(som_asset_t* thing) { return static_cast<A*>(thing)->asset_release(); }
static long thunk_asset_get_interface(som_asset_t* thing, const char* name, void** out) { return static_cast<A*>(thing)->asset_get_interface(name, out); }
static som_passport_t* thunk_asset_get_passport(som_asset_t* thing) { return static_cast<A*>(thing)->asset_get_passport(); }
static const char* interface_name() { return "asset.sciter.com"; }
//template<class C> hasset<C> interface_of() { hasset<C> p; get_interface(C::interface_name(), p.target()); return p; }
};
inline long asset_add_ref(som_asset_t *ptr) {
assert(ptr);
assert(ptr->isa);
assert(ptr->isa->asset_add_ref);
return ptr->isa->asset_add_ref(ptr);
}
inline long asset_release(som_asset_t *ptr) {
assert(ptr);
assert(ptr->isa);
assert(ptr->isa->asset_release);
return ptr->isa->asset_release(ptr);
}
inline long asset_get_interface(som_asset_t *ptr, const char* name, void** out = nullptr) {
assert(ptr);
assert(ptr->isa);
assert(ptr->isa->asset_get_interface);
return ptr->isa->asset_get_interface(ptr, name, out);
}
inline som_passport_t* asset_get_passport(som_asset_t *ptr) {
assert(ptr);
assert(ptr->isa);
assert(ptr->isa->asset_get_passport);
return ptr->isa->asset_get_passport(ptr);
}
inline som_asset_class_t* asset_get_class(som_asset_t *ptr) {
assert(ptr);
return ptr->isa;
}
//hasset - yet another shared_ptr
// R here is an entity derived from som_asset_t
template <class R> class hasset
{
protected:
R* p;
public:
typedef R asset_t;
hasset() :p(nullptr) {}
hasset(R* lp) :p(nullptr) { if (lp) asset_add_ref(p = lp); }
hasset(const hasset<R>& cp) :p(nullptr) { if (cp.p) asset_add_ref(p = cp.p); }
~hasset() { if (p) asset_release(p); }
operator R*() const { return p; }
R* operator->() const { assert(p != 0); return p; }
bool operator!() const { return p == 0; }
explicit operator bool() const { return p != 0; }
bool operator!=(R* pR) const { return p != pR; }
bool operator==(R* pR) const { return p == pR; }
// release the interface and set it to NULL
void release() { if (p) { R* pt = p; p = 0; asset_release(pt); } }
// attach to an existing interface (does not AddRef)
void attach(R* p2) { asset_release(p); p = p2; }
// detach the interface (does not Release)
R* detach() { R* pt = p; p = 0; return pt; }
static R* assign(R* &pp, R* lp)
{
if (lp != 0) asset_add_ref(lp);
if (pp) asset_release(pp);
pp = lp;
return lp;
}
R* operator=(R* lp) { if (p != lp) return assign(p, lp); return p; }
R* operator=(const hasset<R>& lp) { if (p != lp) return assign(p, lp.p); return p; }
void** target() { release(); return (void**)&p; }
};
// reference counted asset, uses intrusive add_ref/release counter
template<class C>
class asset : public iasset<asset<C>>
{
std::atomic<long> _ref_cntr;
public:
asset() :_ref_cntr(0) {}
asset(const asset&/*r*/) = delete;
virtual ~asset()
{
assert(_ref_cntr == 0);
}
long asset_release() override
{
assert(_ref_cntr > 0);
long t = --_ref_cntr;
if (t == 0)
asset_finalize();
return t;
}
long asset_add_ref() override { return ++_ref_cntr; }
// "name" here is an arbitrary name that includes domain name in reversed order:
// "element.dom.sciter.com"
// "video-renderer.dom.sciter.com"
//virtual bool asset_get_interface(const char* name, iasset** out) override { return false; }
virtual void asset_finalize()
{
delete static_cast<C*>(this);
}
};
}
template <class AT>
inline AT* value::get_asset() const {
som_asset_t* pass = get_asset();
if (pass && (som_asset_get_class(pass) == AT::get_asset_class()))
return static_cast<AT*>(pass);
return nullptr;
}
}
#endif
#include "sciter-om-def.h"
#endif