Raised This Month: $32 Target: $400
 8% 

Creating Interfaces


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
showdax
Senior Member
Join Date: Dec 2004
Old 09-30-2005 , 07:19   Creating Interfaces
Reply With Quote #1

I'm trying to create an interface in one plugin that can be used in other plugins. I'm not entirely sure how I expose the interface through sourcemm, and I'm having some issues just compiling my plugin with the new abstract class.

Code:
In file included from CServerPluginAccess.cpp:15:
CPlayerAccess.h: At global scope:
CPlayerAccess.h:73: error: invalid covariant return type for `virtual CUtlVector<CPlayerAccess*, CUtlMemory<CPlayerAccess*> >* CPlayerAccessManager::getPlayers() const'
IPlayerAccess.h:47: error:   overriding `virtual CUtlVector<IPlayerAccess*, CUtlMemory<IPlayerAccess*> >* IPlayerAccessManager::getPlayers() const'
CServerPluginAccess.cpp: In member function `virtual bool CServerPluginAccess::Load(PluginId, ISmmAPI*, factories*, char*, size_t)':
CServerPluginAccess.cpp:244: error: cannot allocate an object of type `CPlayerAccessManager'
CServerPluginAccess.cpp:244: error:   because the following virtual functions are abstract:
IPlayerAccess.h:48: error:  virtual void IPlayerAccessManager::getPlayersByIsValidated(CUtlVector<IPlayerAccess*, CUtlMemory<IPlayerAccess*> >*, bool) const
IPlayerAccess.h:49: error:  virtual void IPlayerAccessManager::getPlayersByAccess(CUtlVector<IPlayerAccess*, CUtlMemory<IPlayerAccess*> >*, byte) const
IPlayerAccess.h:50: error:  virtual void IPlayerAccessManager::getPlayersByLowestAccess(CUtlVector<IPlayerAccess*, CUtlMemory<IPlayerAccess*> >*) const
Here are the relevant definitions from the header files:

CPlayerAccess.h:
Code:
CUtlVector<CPlayerAccess *> *getPlayers() const;
void getPlayersByIsValidated(CUtlVector<CPlayerAccess *> *results, bool isValidated) const;
void getPlayersByAccess(CUtlVector<CPlayerAccess *> *results, byte access) const;
void getPlayersByLowestAccess(CUtlVector<CPlayerAccess *> *results) const;
IPlayerAccess.h:
Code:
virtual CUtlVector<IPlayerAccess *> *getPlayers() const = 0;
virtual void getPlayersByIsValidated(CUtlVector<IPlayerAccess *> *results, bool isValidated) const = 0;
virtual void getPlayersByAccess(CUtlVector<IPlayerAccess *> *results, byte access) const = 0;
virtual void getPlayersByLowestAccess(CUtlVector<IPlayerAccess *> *results) const = 0;
I haven't messed with creating my own interfaces before, so this is all pretty new to me.
showdax is offline
Send a message via MSN to showdax
BAILOPAN
Join Date: Jan 2004
Old 09-30-2005 , 12:29  
Reply With Quote #2

First off you might need to swap const and =0 but I'm not sure.

I think the problem is that you are mixing and matching prototypes. The interface says you'll be returning things of IPlayerAccess, but your implementation is returning containers of CPlayerAccess.
__________________
egg
BAILOPAN is offline
showdax
Senior Member
Join Date: Dec 2004
Old 09-30-2005 , 15:05  
Reply With Quote #3

I'm using covariant return types, and apparently I can't do that with parameters, but there's no way for me to get around the template parameter. CPlayerAccess inherits from IPlayerAccess, which is why I'm doing that.

Also, even when I do get this compiled, what do I do to expose the interface? And how do other plugins find the interface?
showdax is offline
Send a message via MSN to showdax
showdax
Senior Member
Join Date: Dec 2004
Old 09-30-2005 , 17:24  
Reply With Quote #4

Okay well I got it compiled. For now I just made the functions in the implementation use vectors with IPlayerAccess objects, and have things static_cast them to CPlayerAccess when necessary. Now I just need to know how to expose the interface.

Edit: I'm using the SDK's interface exporting macros now, but I don't know how to get the CreateInterfaceFn for the plugin.
showdax is offline
Send a message via MSN to showdax
showdax
Senior Member
Join Date: Dec 2004
Old 10-01-2005 , 06:28  
Reply With Quote #5

I've got it working now, but if anyone's curious about what I'm using, here's some code:

In the interface's header:
Code:
#define INTERFACEVERSION_MYINTERFACE "MyInterface001"
In the implementation's cpp file:
Code:
EXPOSE_SINGLE_INTERFACE_GLOBALVAR(CMyImplementation, IMyInterface, INTERFACEVERSION_MYINTERFACE, g_MySingleton);
In the plugin's cpp file (g_MySingleton is externed from the implementation's object file):
Code:
void *CreateInterfaceHook(const char *pName, int *pReturnCode)
{
	if (Q_strcmp(pName, INTERFACEVERSION_MYINTERFACE) == 0)
	{
		if (pReturnCode != NULL)
			* pReturnCode = IFACE_OK;

		FACTORY_RETURN(MRES_SUPERCEDE, (void *) g_MySingleton);
	}

	FACTORY_RETURN(MRES_IGNORED, NULL);
}
And then in my plugin's Load() function:
Code:
list->server = CreateInterfaceHook;
Then I tested it in AllPluginsLoaded():
Code:
IMyInterface *g_MyInterface = (IMyInterface *)((g_Ismm->serverFactory())(INTERFACEVERSION_MYINTERFACE, NULL))
if (g_MyInterface == NULL)
{
	META_CONPRINTF("WARNING! Could not find interface %s\n", INTERFACE_MINTERFACE);
}
showdax is offline
Send a message via MSN to showdax
sslice
Senior Member
Join Date: Feb 2005
Location: Texas, USA
Old 11-27-2005 , 14:43  
Reply With Quote #6

How would you use SourceMM to expose an interface rather than the SDKs method?
__________________
sslice is offline
BAILOPAN
Join Date: Jan 2004
Old 11-28-2005 , 10:47  
Reply With Quote #7

You would override an input with your desired output in OnMetamodQuery in IMetamodListener.

Plugins can retrieve other plugin interfaces through g_SMAPI->MetamodQuery

(I think this is right, I don't have the code on me). I'll be updating the example plugin with the next release (which is sooner than you think, it's just a template library update and minor crash fixes).
__________________
egg
BAILOPAN is offline
sslice
Senior Member
Join Date: Feb 2005
Location: Texas, USA
Old 11-28-2005 , 16:13  
Reply With Quote #8

Ahhh.. thanks, BAILOPAN.
__________________
sslice is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:28.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode