Commit d5269108 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

nporuntime: Avoid friend class declaration by making RuntimeNPClass helpers static methods

parent 48033d2f
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
* *
* Authors: Damien Fouilleul <damien.fouilleul@laposte.net> * Authors: Damien Fouilleul <damien.fouilleul@laposte.net>
* JP Dinger <jpd@videolan.org> * JP Dinger <jpd@videolan.org>
* Hugo Beauzée-Luyssen <hugo@beauzee.fr>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
...@@ -37,13 +38,6 @@ ...@@ -37,13 +38,6 @@
#include <npruntime.h> #include <npruntime.h>
#include <stdlib.h> #include <stdlib.h>
static void RuntimeNPClassDeallocate(NPObject *npobj);
static void RuntimeNPClassInvalidate(NPObject *npobj);
static bool RuntimeNPClassInvokeDefault(NPObject *npobj,
const NPVariant *args,
uint32_t argCount,
NPVariant *result);
class RuntimeNPObject : public NPObject class RuntimeNPObject : public NPObject
{ {
public: public:
...@@ -54,7 +48,7 @@ public: ...@@ -54,7 +48,7 @@ public:
bool isValid() bool isValid()
{ {
return _instance != NULL; return _instance != NULL;
}; }
enum InvokeResult enum InvokeResult
{ {
...@@ -99,13 +93,6 @@ protected: ...@@ -99,13 +93,6 @@ protected:
}; };
virtual ~RuntimeNPObject() = default; virtual ~RuntimeNPObject() = default;
friend void RuntimeNPClassDeallocate(NPObject *npobj);
friend void RuntimeNPClassInvalidate(NPObject *npobj);
friend bool RuntimeNPClassInvokeDefault(NPObject *npobj,
const NPVariant *args,
uint32_t argCount,
NPVariant *result);
bool isPluginRunning() bool isPluginRunning()
{ {
return (_instance->pdata != NULL); return (_instance->pdata != NULL);
...@@ -116,9 +103,13 @@ protected: ...@@ -116,9 +103,13 @@ protected:
} }
NPP _instance; NPP _instance;
template <typename>
friend class RuntimeNPClass;
}; };
template<class T> class RuntimeNPClass : public NPClass template<class T>
class RuntimeNPClass : public NPClass
{ {
public: public:
static NPClass *getClass() static NPClass *getClass()
...@@ -131,145 +122,125 @@ protected: ...@@ -131,145 +122,125 @@ protected:
RuntimeNPClass(); RuntimeNPClass();
virtual ~RuntimeNPClass(); virtual ~RuntimeNPClass();
template <class RuntimeNPObject> friend NPObject *RuntimeNPClassAllocate(NPP instance, NPClass *aClass); static NPObject *Allocate(NPP instance, NPClass *aClass)
template <class RuntimeNPObject> friend bool RuntimeNPClassHasMethod(NPObject *npobj, NPIdentifier name); {
template <class RuntimeNPObject> friend bool RuntimeNPClassHasProperty(NPObject *npobj, NPIdentifier name); const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(aClass);
template <class RuntimeNPObject> friend bool RuntimeNPClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result); return new T(instance, vClass);
template <class RuntimeNPObject> friend bool RuntimeNPClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value); }
template <class RuntimeNPObject> friend bool RuntimeNPClassRemoveProperty(NPObject *npobj, NPIdentifier name);
template <class RuntimeNPObject> friend bool RuntimeNPClassInvoke(NPObject *npobj, NPIdentifier name,
const NPVariant *args, uint32_t argCount,
NPVariant *result);
RuntimeNPObject *create(NPP instance) const;
int indexOfMethod(NPIdentifier name) const;
int indexOfProperty(NPIdentifier name) const;
private:
NPIdentifier *propertyIdentifiers;
NPIdentifier *methodIdentifiers;
};
template<class T>
inline void RuntimeNPObject::InstantObj( NPObject *&obj )
{
if( !obj )
obj = NPN_CreateObject(_instance, RuntimeNPClass<T>::getClass());
}
template<class T>
static NPObject *RuntimeNPClassAllocate(NPP instance, NPClass *aClass)
{
const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(aClass);
return vClass->create(instance);
}
static void RuntimeNPClassDeallocate(NPObject *npobj) static void Deallocate(NPObject *npobj)
{ {
RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj); RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
vObj->_class = NULL; delete vObj;
delete vObj; }
}
static void RuntimeNPClassInvalidate(NPObject *npobj) static void Invalidate(NPObject *npobj)
{ {
RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj); RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
vObj->_instance = NULL; vObj->_instance = NULL;
} }
template<class T> static bool HasMethod(NPObject *npobj, NPIdentifier name)
static bool RuntimeNPClassHasMethod(NPObject *npobj, NPIdentifier name) {
{ const RuntimeNPClass* vClass = static_cast<RuntimeNPClass*>(npobj->_class);
const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class); return vClass->indexOfMethod(name) != -1;
return vClass->indexOfMethod(name) != -1; }
}
template<class T> static bool HasProperty(NPObject *npobj, NPIdentifier name)
static bool RuntimeNPClassHasProperty(NPObject *npobj, NPIdentifier name) {
{ const RuntimeNPClass* vClass = static_cast<RuntimeNPClass*>(npobj->_class);
const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class); return vClass->indexOfProperty(name) != -1;
return vClass->indexOfProperty(name) != -1; }
}
template<class T> static bool GetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
static bool RuntimeNPClassGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
{
RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
if( vObj->isValid() )
{ {
const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class); RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
int index = vClass->indexOfProperty(name); if( vObj->isValid() )
if( index != -1 )
{ {
return vObj->returnInvokeResult(vObj->getProperty(index, *result)); const RuntimeNPClass* vClass = static_cast<RuntimeNPClass*>(npobj->_class);
int index = vClass->indexOfProperty(name);
if( index != -1 )
{
return vObj->returnInvokeResult(vObj->getProperty(index, *result));
}
} }
return false;
} }
return false;
}
template<class T> static bool SetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value)
static bool RuntimeNPClassSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value)
{
RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
if( vObj->isValid() )
{ {
const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class); RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
int index = vClass->indexOfProperty(name); if( vObj->isValid() )
if( index != -1 )
{ {
return vObj->returnInvokeResult(vObj->setProperty(index, *value)); const RuntimeNPClass* vClass = static_cast<RuntimeNPClass*>(npobj->_class);
int index = vClass->indexOfProperty(name);
if( index != -1 )
{
return vObj->returnInvokeResult(vObj->setProperty(index, *value));
}
} }
return false;
} }
return false;
}
template<class T> static bool RemoveProperty(NPObject *npobj, NPIdentifier name)
static bool RuntimeNPClassRemoveProperty(NPObject *npobj, NPIdentifier name)
{
RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
if( vObj->isValid() )
{ {
const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class); RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
int index = vClass->indexOfProperty(name); if( vObj->isValid() )
if( index != -1 )
{ {
return vObj->returnInvokeResult(vObj->removeProperty(index)); const RuntimeNPClass* vClass = static_cast<RuntimeNPClass*>(npobj->_class);
int index = vClass->indexOfProperty(name);
if( index != -1 )
{
return vObj->returnInvokeResult(vObj->removeProperty(index));
}
} }
return false;
} }
return false;
}
template<class T> static bool ClassInvoke(NPObject *npobj, NPIdentifier name,
static bool RuntimeNPClassInvoke(NPObject *npobj, NPIdentifier name, const NPVariant *args, uint32_t argCount,
const NPVariant *args, uint32_t argCount, NPVariant *result)
NPVariant *result)
{
RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
if( vObj->isValid() )
{ {
const RuntimeNPClass<T> *vClass = static_cast<RuntimeNPClass<T> *>(npobj->_class); RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
int index = vClass->indexOfMethod(name); if( vObj->isValid() )
if( index != -1 )
{ {
return vObj->returnInvokeResult(vObj->invoke(index, args, argCount, *result)); const RuntimeNPClass* vClass = static_cast<RuntimeNPClass*>(npobj->_class);
int index = vClass->indexOfMethod(name);
if( index != -1 )
{
return vObj->returnInvokeResult(vObj->invoke(index, args, argCount, *result));
}
} }
return false;
} }
return false;
}
static bool RuntimeNPClassInvokeDefault(NPObject *npobj, static bool InvokeDefault(NPObject *npobj,
const NPVariant *args, const NPVariant *args,
uint32_t argCount, uint32_t argCount,
NPVariant *result) NPVariant *result)
{
RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
if( vObj->isValid() )
{ {
return vObj->returnInvokeResult(vObj->invokeDefault(args, argCount, *result)); RuntimeNPObject *vObj = static_cast<RuntimeNPObject *>(npobj);
if( vObj->isValid() )
{
return vObj->returnInvokeResult(vObj->invokeDefault(args, argCount, *result));
}
return false;
} }
return false;
int indexOfMethod(NPIdentifier name) const;
int indexOfProperty(NPIdentifier name) const;
private:
NPIdentifier *propertyIdentifiers;
NPIdentifier *methodIdentifiers;
};
template<class T>
inline void RuntimeNPObject::InstantObj( NPObject *&obj )
{
if( !obj )
obj = NPN_CreateObject(_instance, RuntimeNPClass<T>::getClass());
} }
template<class T> template<class T>
...@@ -295,16 +266,16 @@ RuntimeNPClass<T>::RuntimeNPClass() ...@@ -295,16 +266,16 @@ RuntimeNPClass<T>::RuntimeNPClass()
// fill in NPClass structure // fill in NPClass structure
structVersion = NP_CLASS_STRUCT_VERSION; structVersion = NP_CLASS_STRUCT_VERSION;
allocate = &RuntimeNPClassAllocate<T>; allocate = &Allocate;
deallocate = &RuntimeNPClassDeallocate; deallocate = &Deallocate;
invalidate = &RuntimeNPClassInvalidate; invalidate = &Invalidate;
hasMethod = &RuntimeNPClassHasMethod<T>; hasMethod = &HasMethod;
invoke = &RuntimeNPClassInvoke<T>; invoke = &ClassInvoke;
invokeDefault = &RuntimeNPClassInvokeDefault; invokeDefault = &InvokeDefault;
hasProperty = &RuntimeNPClassHasProperty<T>; hasProperty = &HasProperty;
getProperty = &RuntimeNPClassGetProperty<T>; getProperty = &GetProperty;
setProperty = &RuntimeNPClassSetProperty<T>; setProperty = &SetProperty;
removeProperty = &RuntimeNPClassRemoveProperty<T>; removeProperty = &RemoveProperty;
enumerate = 0; enumerate = 0;
construct = 0; construct = 0;
} }
...@@ -316,12 +287,6 @@ RuntimeNPClass<T>::~RuntimeNPClass() ...@@ -316,12 +287,6 @@ RuntimeNPClass<T>::~RuntimeNPClass()
delete[] methodIdentifiers; delete[] methodIdentifiers;
} }
template<class T>
RuntimeNPObject *RuntimeNPClass<T>::create(NPP instance) const
{
return new T(instance, this);
}
template<class T> template<class T>
int RuntimeNPClass<T>::indexOfMethod(NPIdentifier name) const int RuntimeNPClass<T>::indexOfMethod(NPIdentifier name) const
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment