Sciter: F:/hsmile5/sdk/include/aux-asset.h Source File

Sciter  3.3.2.5
Sciter API
aux-asset.h
Go to the documentation of this file.
1 #pragma once
2 
3 #if defined(_WIN32)
4 
5  #define WIN32_LEAN_AND_MEAN
6  #include <windows.h>
7  //#include <assert.h>
8 #else
9  #include <atomic>
10 #endif
11 
12  namespace aux {
13 
14   namespace atomic {
15 
16  #if defined(_WINDOWS) || defined(WINDOWS)
17 
18  typedef volatile long counter_t;
19  inline long _inc(counter_t& v) { return InterlockedIncrement((LPLONG)&v); }
20  inline long _inc(counter_t& v,long by) { return InterlockedExchangeAdd((LPLONG)&v,by); }
21  inline long _dec(counter_t& v) { return InterlockedDecrement((LPLONG)&v); }
22  inline long _set(counter_t& v, long nv) { return InterlockedExchange((LPLONG)&v, nv); }
23  inline long _set_when_eq(counter_t& v, long to_set, long eq_to) { return InterlockedCompareExchange ((LPLONG)&v,to_set,eq_to); }
24 
25  #elif defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ <= 8) )
26 
27  typedef long counter_t;
28  inline long _inc(counter_t& v) { return __sync_add_and_fetch(&v,1); }
29  inline long _inc(counter_t& v,long by) { return __sync_add_and_fetch(&v,by); }
30  inline long _dec(counter_t& v) { return __sync_add_and_fetch(&v,-1); }
31  inline long _set(counter_t& v, long nv) { return __sync_lock_test_and_set(&v,nv); }
32  inline long _set_when_eq(counter_t& v, long to_set, long eq_to) { return __sync_val_compare_and_swap(&v,eq_to,to_set);
33  /*long t(v); if(t == eq_to) v = to_set; return t;*/ }
34  #else
35 
36   typedef std::atomic<long> counter_t;
37   inline long _inc(counter_t& v) { return ++v; }
38   inline long _inc(counter_t& v,long by) { return v += by; }
39   inline long _dec(counter_t& v) { return --v; }
40   inline long _set(counter_t& v, long nv) { return v.exchange(nv); }
41   inline long _set_when_eq(counter_t& v, long to_set, long eq_to) { long t(v); v.compare_exchange_strong(eq_to,to_set); return t; }
42 
43  #endif
44 
45   struct counter
46  {
47   counter_t cv;
48   counter():cv(0) {}
49   counter(long iv):cv(iv) {}
50   counter& operator=(long nv) { _set(cv,nv); return *this; }
51   operator long() const { return cv; }
52   long operator++() { return _inc(cv); }
53   long operator--() { return _dec(cv); }
54  };
55  } // atomic
56 
57 
58  // COM::IUnknown alike thing:
59   class iasset {
60  public:
61  // mandatory:
62  virtual long add_ref() = 0;
63  virtual long release() = 0;
64  // optional:
65  virtual bool get_interface(const char* name, iasset** out) = 0;
66  };
67 
68 
69  // intrusive add_ref/release counter
70   class asset: public virtual iasset
71  {
72  atomic::counter _ref_cntr;
73  public:
74   asset ():_ref_cntr(0) {}
75   asset (const asset&/*r*/):_ref_cntr(0) {}
76 
77   virtual ~asset ()
78  {
79  assert ( _ref_cntr == 0 );
80  }
81 
82   virtual long release()
83  {
84  assert(_ref_cntr > 0);
85  long t = --_ref_cntr;
86  if(t == 0)
87  finalize();
88  return t;
89  }
90   virtual long add_ref() { return ++_ref_cntr; }
91 
92  // "name" here is an arbitrary name that includes domain name in reversed order:
93  // "element.dom.sciter.com"
94  // "video-renderer.dom.sciter.com"
95   virtual bool get_interface(const char* name, iasset** out) { return false; }
96 
97   virtual void finalize()
98  {
99  delete this;
100  }
101  };
102 
103  //asset - yet another shared_ptr
104  // R here is something derived from the asset above
105  template <class R>
106   class asset_ptr
107  {
108  protected:
109   R* p;
110 
111  public:
112   typedef R asset_t;
113 
114   asset_ptr():p(0) {}
115   asset_ptr(R* lp):p(0) { if (lp) (p = lp)->add_ref(); }
116   asset_ptr(const asset_ptr<R>& cp):p(0) { if (cp.p) (p = cp.p)->add_ref(); }
117 
118   ~asset_ptr() { if (p) p->release();}
119   operator R*() const { return p; }
120   R* operator->() const { assert(p != 0); return p; }
121 
122   bool operator!() const { return p == 0; }
123   operator bool() const { return p != 0; }
124   bool operator!=(R* pR) const { return p != pR; }
125   bool operator==(R* pR) const { return p == pR; }
126 
127  // release the interface and set it to NULL
128   void release() { if (p) { R* pt = p; p = 0; pt->release(); }}
129 
130  // attach to an existing interface (does not AddRef)
131   void attach(R* p2) { release(); p = p2; }
132  // detach the interface (does not Release)
133   R* detach() { R* pt = p; p = 0; return pt; }
134 
135   static R* assign(R* &pp, R* lp)
136  {
137  if (lp != 0) lp->add_ref();
138  if (pp) pp->release();
139  pp = lp;
140  return lp;
141  }
142 
143   R* operator=(R* lp) { if(p != lp) return assign(p, lp); return p; }
144   R* operator=(const asset_ptr<R>& lp) { if(p != lp) return assign(p, lp.p); return p; }
145 
146   R** target() { release(); return &p; }
147 
148  };
149 
150 }
151 
152 #if defined(_WINDOWS)
153 
154 namespace com {
155 
156  //asset - yet another smart pointer
157  template <class T>
158  class ptr
159  {
160  protected:
161  T* p;
162 
163  public:
164  typedef T asset_t;
165 
166  ptr():p(0) {}
167  ptr(T* lp):p(0) { if (lp) (p = lp)->AddRef(); }
168  ptr(const ptr<T>& cp):p(0) { if (cp.p) (p = cp.p)->AddRef(); }
169 
170  ~ptr()
171  {
172  ULONG c = 0;
173  if (p)
174  c = p->Release();
175  }
176  operator T*() const { return p; }
177  T* operator->() const { assert(p != 0); return p; }
178 
179  // used as target T** pointer to pointer - in places receiving newly created objects (initially add-refed)
180  T** target() { release(); return &p; }
181 
182  bool operator!() const { return p == 0; }
183  operator bool() const { return p != 0; }
184  bool operator!=(T* pT) const { return p != pT; }
185  bool operator==(T* pT) const { return p == pT; }
186 
187  // release the interface and set it to NULL
188  void release() { if (p) { T* pt = p; p = 0; pt->Release(); }}
189 
190  // attach to an existing interface (does not AddRef)
191  void attach(T* p2) { release(); p = p2; }
192  // detach the interface (does not Release)
193  T* detach() { T* pt = p; p = 0; return pt; }
194 
195  static T* assign(T* &pp, T* lp)
196  {
197  if (lp != 0) lp->AddRef();
198  if (pp) pp->Release();
199  pp = lp;
200  return lp;
201  }
202 
203  T* operator=(T* lp) { if(p != lp) return assign(p, lp); return p; }
204  T* operator=(const ptr<T>& lp) { if(p != lp) return assign(p, lp.p); return p; }
205 
206  T* acquire() { if( p ) p->AddRef(); return p; }
207 
208  HRESULT CoCreateInstance (REFCLSID classUUID, DWORD dwClsContext = CLSCTX_INPROC_SERVER)
209  {
210  HRESULT hr = ::CoCreateInstance (classUUID, 0, dwClsContext, __uuidof (T), (void**) target());
211  assert (hr != CO_E_NOTINITIALIZED); // You haven't called CoInitialize for the current thread!
212  return hr;
213  }
214 
215  template <class OTHER_COM_CLASS>
216  HRESULT QueryInterface (REFCLSID classUUID, ptr<OTHER_COM_CLASS>& dest_object) const
217 {
218  if (p == 0)
219  return E_POINTER;
220  return p->QueryInterface (classUUID, (void**) dest_object.target());
221  }
222 
223  template <class OTHER_COM_CLASS>
224  HRESULT QueryInterface (ptr<OTHER_COM_CLASS>& dest_object) const
225 {
226  return this->QueryInterface (__uuidof (OTHER_COM_CLASS), dest_object);
227  }
228 
229  };
230 
231 }
232 
233 #endif
long _set(counter_t &v, long nv)
Definition: aux-asset.h:40
virtual long add_ref()=0
bool operator!() const
Definition: aux-asset.h:122
R * operator=(const asset_ptr< R > &lp)
Definition: aux-asset.h:144
static R * assign(R *&pp, R *lp)
Definition: aux-asset.h:135
virtual ~asset()
Definition: aux-asset.h:77
virtual long release()
Definition: aux-asset.h:82
counter & operator=(long nv)
Definition: aux-asset.h:50
counter_t cv
Definition: aux-asset.h:47
long operator++()
Definition: aux-asset.h:52
std::atomic< long > counter_t
Definition: aux-asset.h:36
asset_ptr(const asset_ptr< R > &cp)
Definition: aux-asset.h:116
virtual long add_ref()
Definition: aux-asset.h:90
void release()
Definition: aux-asset.h:128
bool operator==(R *pR) const
Definition: aux-asset.h:125
R * operator->() const
Definition: aux-asset.h:120
Definition: aux-asset.h:12
asset(const asset &)
Definition: aux-asset.h:75
virtual void finalize()
Definition: aux-asset.h:97
counter(long iv)
Definition: aux-asset.h:49
R * operator=(R *lp)
Definition: aux-asset.h:143
virtual bool get_interface(const char *name, iasset **out)
Definition: aux-asset.h:95
long operator--()
Definition: aux-asset.h:53
asset()
Definition: aux-asset.h:74
long _inc(counter_t &v)
Definition: aux-asset.h:37
asset_ptr(R *lp)
Definition: aux-asset.h:115
R * detach()
Definition: aux-asset.h:133
long _dec(counter_t &v)
Definition: aux-asset.h:39
R ** target()
Definition: aux-asset.h:146
bool operator!=(R *pR) const
Definition: aux-asset.h:124
long _set_when_eq(counter_t &v, long to_set, long eq_to)
Definition: aux-asset.h:41
void attach(R *p2)
Definition: aux-asset.h:131

AltStyle によって変換されたページ (->オリジナル) /