FreeWRL / FreeX3D 4.3.0
ScriptablePluginObjectBase.h
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Netscape Public License
6 * Version 1.1 (the "License"); you may not use this file except in
7 * compliance with the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/NPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the NPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the NPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#pragma once
39#include "plugin.h"
40#include "npfunctions.h"
41
42//-------------------------------------------------------
43//----------- utility macros ----------------------------
44//-------------------------------------------------------
45
46// expands to method name
47#define NPMETHODID(_nm_) m_method##_nm_
48// expands to -> NPidentifier m_methodname;
49#define DECLARE_NPMETHODID(_nm_) static NPIdentifier NPMETHODID(_nm_);
50// expands to -> classScope::NPIdentifier name = 0
51#define DEFINE_NPMETHODID(_cls_,_nm_) NPIdentifier _cls_::NPMETHODID(_nm_) = 0;
52// expands to -> NPIdentifier m_methodname = NPN_GetStringIdentifiers("name");
53#define INIT_NPMETHODID(_nm_) NPMETHODID(_nm_) = NPN_GetStringIdentifier(#_nm_);
54// returns a boolean true if the tested string is equal to the string assigned to the NPIdentifier
55#define TEST_NPMETHODID(str,_nm_) (str == NPMETHODID(_nm_))
56
57// expands to property name
58#define NPPROPID(_nm_) m_property##_nm_
59// expands to -> NPIdentifier m_propertyname
60#define DECLARE_NPPROPID(_nm_) static NPIdentifier NPPROPID(_nm_);
61// expands to classScope::NPIdentifier name = 0;
62#define DEFINE_NPPROPID(_cls_,_nm_) NPIdentifier _cls_::NPPROPID(_nm_) = 0;
63// expands to NPIdentifier m_propertyname = NPN_GetStringIdentifier("name");
64#define INIT_NPPROPID(_nm_) NPPROPID(_nm_) = NPN_GetStringIdentifier(#_nm_);
65// returns a boolean true if the tested string is equal to the string assigned to the NPIdentifier
66#define TEST_NPPROPID(str,_nm_) (str == NPPROPID(_nm_))
67
69{
70public:
72 : mNpp(npp)
73 {
74 }
75
77 {
78 }
79
80 // Virtual NPObject hooks called through this base class. Override
81 // as you see fit.
82 virtual void Invalidate();
83 virtual bool HasMethod(NPIdentifier name);
84 virtual bool Invoke(NPIdentifier name, const NPVariant *args,
85 uint32_t argCount, NPVariant *result);
86 virtual bool InvokeDefault(const NPVariant *args, uint32_t argCount,
87 NPVariant *result);
88 virtual bool HasProperty(NPIdentifier name);
89 virtual bool GetProperty(NPIdentifier name, NPVariant *result);
90 virtual bool SetProperty(NPIdentifier name, const NPVariant *value);
91 virtual bool RemoveProperty(NPIdentifier name);
92 virtual bool Enumerate(NPIdentifier **identifier, uint32_t *count);
93 virtual bool Construct(const NPVariant *args, uint32_t argCount,
94 NPVariant *result);
95
96public:
97 static void _Deallocate(NPObject *npobj);
98 static void _Invalidate(NPObject *npobj);
99 static bool _HasMethod(NPObject *npobj, NPIdentifier name);
100 static bool _Invoke(NPObject *npobj, NPIdentifier name,
101 const NPVariant *args, uint32_t argCount,
102 NPVariant *result);
103 static bool _InvokeDefault(NPObject *npobj, const NPVariant *args,
104 uint32_t argCount, NPVariant *result);
105 static bool _HasProperty(NPObject * npobj, NPIdentifier name);
106 static bool _GetProperty(NPObject *npobj, NPIdentifier name,
107 NPVariant *result);
108 static bool _SetProperty(NPObject *npobj, NPIdentifier name,
109 const NPVariant *value);
110 static bool _RemoveProperty(NPObject *npobj, NPIdentifier name);
111 static bool _Enumerate(NPObject *npobj, NPIdentifier **identifier,
112 uint32_t *count);
113 static bool _Construct(NPObject *npobj, const NPVariant *args,
114 uint32_t argCount, NPVariant *result);
115
116protected:
117 NPP mNpp;
118};
119
120#define DECLARE_NPOBJECT_CLASS_WITH_BASE(_class, ctor) \
121static NPClass s##_class##_NPClass = { \
122 NP_CLASS_STRUCT_VERSION_CTOR, \
123 ctor, \
124 ScriptablePluginObjectBase::_Deallocate, \
125 ScriptablePluginObjectBase::_Invalidate, \
126 ScriptablePluginObjectBase::_HasMethod, \
127 ScriptablePluginObjectBase::_Invoke, \
128 ScriptablePluginObjectBase::_InvokeDefault, \
129 ScriptablePluginObjectBase::_HasProperty, \
130 ScriptablePluginObjectBase::_GetProperty, \
131 ScriptablePluginObjectBase::_SetProperty, \
132 ScriptablePluginObjectBase::_RemoveProperty, \
133 ScriptablePluginObjectBase::_Enumerate, \
134 ScriptablePluginObjectBase::_Construct \
135}
136
137#define GET_NPOBJECT_CLASS(_class) &s##_class##_NPClass
138
Definition npapi.h:149