| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here is a complete working example. It implements a shared class that has a base and an embedded interface.
These are the interface files for the `iDog' interface (the base interface of our class) and the `iName' interface (the interface embedded into our class).
File: `idog.h'
#include "csutil/scf.h"
// Version number of our interface.
SCF_VERSION(iDog, 0, 0, 1);
struct iDog : public iBase
{
virtual void Walk() = 0;
virtual void Shout(char const* Message) = 0;
};
|
File: `iname.h'
#include "csutil/scf.h"
// Version number of our interface.
SCF_VERSION(iName, 0, 0, 1);
struct iName : public iBase
{
virtual char const* GetName() = 0;
virtual void SetName(char const*) = 0;
};
|
Now here is the implementation of a class for both of the mentioned interfaces:
File: `dog.cpp'
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cssysdef.h"
#include "idog.h"
#include "iname.h"
class csDog : public iDog
{
char* Name;
// Embedded interface.
class csName : public iName
{
public:
SCF_DECLARE_EMBEDDED_IBASE(csDog);
virtual char const* GetName();
virtual void SetName(char const*);
} scfiName;
friend class csName;
public:
SCF_DECLARE_IBASE;
csDog(iBase* Parent);
virtual ~csDog();
virtual void Walk();
virtual void Shout(char const*);
};
//---------- Implementation ---------
SCF_IMPLEMENT_FACTORY(csDog)
SCF_IMPLEMENT_IBASE(csDog)
SCF_IMPLEMENTS_INTERFACE(iDog)
SCF_IMPLEMENTS_EMBEDDED_INTERFACE(iName)
SCF_IMPLEMENT_IBASE_END
SCF_IMPLEMENT_EMBEDDED_IBASE(csDog::csName)
SCF_IMPLEMENTS_INTERFACE(iName)
SCF_IMPLEMENT_EMBEDDED_IBASE_END
csDog::csDog(iBase* Parent)
{
SCF_CONSTRUCT_IBASE(Parent);
SCF_CONSTRUCT_EMBEDDED_IBASE(scfiName);
Name = strdup("<noname>");
}
csDog::~csDog()
{
free(Name);
SCF_DESTRUCT_EMBEDDED_IBASE(scfiName);
SCF_DESTRUCT_IBASE();
}
void csDog::Walk()
{
printf("%s: I'm walking\n", Name);
}
void csDog::Shout(char const* Message)
{
printf("I'm %s: shout, shout, %s\n", Name, Message);
}
// iName interface for dog.
char const* csDog::csName::GetName()
{
return scfParent->Name;
}
void csDog::csName::SetName(char const* NewName)
{
if (scfParent->Name != 0)
free(scfParent->Name);
scfParent->Name = strdup(NewName != 0 ? NewName : "");
}
|
Since this is a plugin module, it requires a meta-information resource. Here is the meta-information for the example plugin:
File: `dog.csplugin'
<?xml version="1.0"?>
<!-- dog.csplugin -->
<plugin>
<scf>
<classes>
<class>
<name>dog</name>
<implementation>csDog</implementation>
<description>A rather unusual dog</description>
</class>
</classes>
</scf>
</plugin>
|
The above files should be built to get a plugin module named `dog.so'
(Unix) or `dog.dll' (Windows). The plugin module will export the
csDog_Create() function which is implemented by the
SCF_IMPLEMENT_FACTORY() macro, and it will publish the meta-information
contained in `dog.csplugin'.
Finally, here is the source code for a simple client application that uses the `csDog' plugin.
File: `doggy.cpp'
#include <stdio.h>
#include "cssysdef.h"
#include "csutil/scf.h"
#include "csutil/cfgfile.h"
#include "idog.h"
#include "iname.h"
static void test_dog()
{
csRef<iDog> dog = SCF_CREATE_INSTANCE("csDog", iDog);
if (!dog)
fprintf(stderr, "No csDog shared class!\n");
else
{
csRef<iName> name = SCF_QUERY_INTERFACE(dog, iName);
if (!name)
fprintf(stderr,
"Dog does not support iName interface!\n");
else
{
name->SetName("Droopy");
dog->Walk();
dog->Shout("hello!");
printf("Dog's name is %s\n", name->GetName());
}
}
}
int main (int argc, char const* argv[])
{
scfInitialize(argc, argv);
test_dog();
iSCF::SCF->Finish();
}
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] |
This document was generated using texi2html 1.76.