Skip to content

Commit c1db2d2

Browse files
committed
xiu 修改
1 parent a04e052 commit c1db2d2

19 files changed

+4124
-3514
lines changed

include/sciter-om-def.h

Lines changed: 205 additions & 61 deletions
Large diffs are not rendered by default.

include/sciter-om.h

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,57 @@
11
#pragma once
22

3-
#include <string.h>
4-
#include <assert.h>
3+
#ifndef __SCITER_OM_H__
4+
#define __SCITER_OM_H__
5+
56

67
struct som_passport_t;
78

89
typedef UINT64 som_atom_t;
910

10-
11-
struct som_asset_class_t;
12-
1311
typedef struct som_asset_t {
1412
struct som_asset_class_t* isa;
1513
} som_asset_t;
1614

17-
struct som_asset_class_t {
15+
typedef struct som_asset_class_t {
1816
long(*asset_add_ref)(som_asset_t* thing);
1917
long(*asset_release)(som_asset_t* thing);
2018
long(*asset_get_interface)(som_asset_t* thing, const char* name, void** out);
2119
struct som_passport_t* (*asset_get_passport)(som_asset_t* thing);
22-
};
20+
} som_asset_class_t;
21+
22+
inline struct som_asset_class_t* som_asset_get_class(const struct som_asset_t* pass)
23+
{
24+
#ifdef __cplusplus
25+
return pass ? pass->isa : nullptr;
26+
#else
27+
return pass ? pass->isa : NULL;
28+
#endif
29+
}
2330

31+
som_atom_t SCAPI SciterAtomValue(const char* name);
2432

2533
#ifdef CPP11
2634

35+
#include <cstring>
36+
#include <cassert>
2737
#include <atomic>
2838

2939
namespace sciter {
3040

41+
class atom {
42+
som_atom_t _atom;
43+
public:
44+
atom(const char* name) { _atom = SciterAtomValue(name); }
45+
atom(const atom& other) { _atom = other._atom; }
46+
operator som_atom_t() const { return _atom; }
47+
};
48+
3149
namespace om {
3250

3351
template <class R> class hasset;
3452

53+
// implementation of som_asset_t ISA
54+
// note: does not define asset_add_ref()/asset_release() as they shall be defined in specializations
3555
template <class A>
3656
class iasset : public som_asset_t
3757
{
@@ -47,8 +67,8 @@ namespace sciter {
4767
if (out) { this->asset_add_ref(); *out = this; }
4868
return true;
4969
}
50-
virtual som_passport_t* asset_get_passport() const {
51-
return nullptr;
70+
virtual som_passport_t* asset_get_passport() const {
71+
return nullptr;
5272
}
5373

5474
static som_asset_class_t* get_asset_class() {
@@ -69,8 +89,7 @@ namespace sciter {
6989
static const char* interface_name() { return "asset.sciter.com"; }
7090
//template<class C> hasset<C> interface_of() { hasset<C> p; get_interface(C::interface_name(), p.target()); return p; }
7191
};
72-
73-
92+
7493
inline long asset_add_ref(som_asset_t *ptr) {
7594
assert(ptr);
7695
assert(ptr->isa);
@@ -89,16 +108,21 @@ namespace sciter {
89108
assert(ptr->isa->asset_get_interface);
90109
return ptr->isa->asset_get_interface(ptr, name, out);
91110
}
92-
111+
93112
inline som_passport_t* asset_get_passport(som_asset_t *ptr) {
94113
assert(ptr);
95114
assert(ptr->isa);
96115
assert(ptr->isa->asset_get_passport);
97116
return ptr->isa->asset_get_passport(ptr);
98117
}
99118

119+
inline som_asset_class_t* asset_get_class(som_asset_t *ptr) {
120+
assert(ptr);
121+
return ptr->isa;
122+
}
123+
100124
//hasset - yet another shared_ptr
101-
// R here is something derived from the iasset (om::iasset) above
125+
// R here is an entity derived from som_asset_t
102126
template <class R> class hasset
103127
{
104128
protected:
@@ -108,10 +132,10 @@ namespace sciter {
108132
typedef R asset_t;
109133

110134
hasset() :p(nullptr) {}
111-
hasset(R* lp) :p(nullptr) { if (lp) (p = lp)->asset_add_ref(); }
112-
hasset(const hasset<R>& cp) :p(nullptr) { if (cp.p) (p = cp.p)->asset_add_ref(); }
135+
hasset(R* lp) :p(nullptr) { if (lp) asset_add_ref(p = lp); }
136+
hasset(const hasset<R>& cp) :p(nullptr) { if (cp.p) asset_add_ref(p = cp.p); }
113137

114-
~hasset() { if (p) p->asset_release(); }
138+
~hasset() { if (p) asset_release(p); }
115139
operator R*() const { return p; }
116140
R* operator->() const { assert(p != 0); return p; }
117141

@@ -121,7 +145,7 @@ namespace sciter {
121145
bool operator==(R* pR) const { return p == pR; }
122146

123147
// release the interface and set it to NULL
124-
void release() { if (p) { R* pt = p; p = 0; pt->asset_release(); } }
148+
void release() { if (p) { R* pt = p; p = 0; asset_release(pt); } }
125149

126150
// attach to an existing interface (does not AddRef)
127151
void attach(R* p2) { asset_release(p); p = p2; }
@@ -130,8 +154,8 @@ namespace sciter {
130154

131155
static R* assign(R* &pp, R* lp)
132156
{
133-
if (lp != 0) lp->asset_add_ref();
134-
if (pp) pp->asset_release();
157+
if (lp != 0) asset_add_ref(lp);
158+
if (pp) asset_release(pp);
135159
pp = lp;
136160
return lp;
137161
}
@@ -142,11 +166,9 @@ namespace sciter {
142166
void** target() { release(); return (void**)&p; }
143167

144168
};
145-
146-
147-
// intrusive add_ref/release counter
148-
149-
template<class C>
169+
170+
// reference counted asset, uses intrusive add_ref/release counter
171+
template<class C>
150172
class asset : public iasset<asset<C>>
151173
{
152174
std::atomic<long> _ref_cntr;
@@ -179,11 +201,20 @@ namespace sciter {
179201
delete static_cast<C*>(this);
180202
}
181203
};
204+
}
182205

183-
206+
template <class AT>
207+
inline AT* value::get_asset() const {
208+
som_asset_t* pass = get_asset();
209+
if (pass && (som_asset_get_class(pass) == AT::get_asset_class()))
210+
return static_cast<AT*>(pass);
211+
return nullptr;
184212
}
213+
185214
}
186215

187216
#endif
188217

189218
#include "sciter-om-def.h"
219+
220+
#endif

0 commit comments

Comments
 (0)