2
* Copyright © 2008 Christian Persch
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Library General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public
15
* License along with this library; if not, write to the
16
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
* Boston, MA 02111-1307, USA.
20
#ifndef __TOTEM_NPOBJECT_WRAPPER_H__
21
#define __TOTEM_NPOBJECT_WRAPPER_H__
24
#include <npruntime.h>
28
class totemNPObjectWrapper {
32
totemNPObjectWrapper () : mObject (0) { }
33
totemNPObjectWrapper (NPObject *aObject) : mObject (aObject) { } /* adopts */
34
totemNPObjectWrapper (const totemNPObjectWrapper& aOther) { Assign (aOther.mObject); }
36
~totemNPObjectWrapper () { Assign (0); }
38
bool IsNull () const { return mObject == 0; }
40
totemNPObjectWrapper& operator= (NPObject *aObject) { Assign (aObject); return *this; }
42
operator void*() const { return reinterpret_cast<void*>(mObject); }
43
operator NPObject*() const { return mObject; }
44
NPObject* operator->() const { assert (!IsNull ()); return mObject; }
48
explicit GetterRetains (totemNPObjectWrapper& aTarget) : mTarget (aTarget) { VOID_TO_NPVARIANT (mVariant); }
50
if (!NPVARIANT_IS_VOID (mVariant)) {
51
if (NPVARIANT_IS_OBJECT (mVariant)) {
52
mTarget = NPVARIANT_TO_OBJECT (mVariant);
54
NPN_ReleaseVariantValue (&mVariant);
58
operator void**() { return reinterpret_cast<void**> (mTarget.StartAssignment ()); } // FIXMEchpe this looks wrong...
59
operator NPObject**() { return mTarget.StartAssignment (); }
60
operator NPVariant*() { return &mVariant; }
62
/* NPN_GetValue uses void* which is broken */
63
operator void*() { return reinterpret_cast<void*> (mTarget.StartAssignment ()); }
66
totemNPObjectWrapper& mTarget;
70
class AlreadyRetained {
72
explicit AlreadyRetained (NPObject *aObject) : mObject (aObject) { }
73
~AlreadyRetained () { }
75
NPObject *Get () const { return mObject; }
80
totemNPObjectWrapper& operator= (const AlreadyRetained& aRetainer) { Adopt (aRetainer.Get()); return *this; }
84
totemNPObjectWrapper& operator= (const totemNPObjectWrapper&); // not implemented
86
void Assign (NPObject *aObject) {
88
NPN_ReleaseObject (mObject);
93
NPN_RetainObject (mObject);
97
void Adopt (NPObject *aObject) {
99
NPN_ReleaseObject (mObject);
105
NPObject** StartAssignment () { Assign (0); return &mObject; }
110
inline totemNPObjectWrapper::GetterRetains
111
getter_Retains (totemNPObjectWrapper &aTarget)
113
return totemNPObjectWrapper::GetterRetains (aTarget);
116
inline totemNPObjectWrapper::AlreadyRetained
117
do_CreateInstance (totemNPClass_base* aClass, NPP aNPP)
121
return totemNPObjectWrapper::AlreadyRetained (aClass->CreateInstance (aNPP));
124
#endif /* __TOTEM_NPOBJECT_WRAPPER_H__ */