[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ] [ Search: ]

4.3.1.10 Example

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"

struct iDog : public virtual iBase
{
  SCF_INTERFACE(iDog, 0, 0, 1);
  virtual void Walk() = 0;
  virtual void Shout(char const* Message) = 0;
};

File: `iname.h'

 
#include "csutil/scf.h"

struct iName : public virtual iBase
{
  SCF_INTERFACE(iName, 0, 0, 1);
  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 "crystalspace.h"
#include "idog.h"
#include "iname.h"

class csDog : public scfImplementation2<csDog,iDog,iName>
{
private:
  csString Name;

public:
  csDog(iBase* Parent);
  virtual ~csDog();

  // From iDog.
  virtual void Walk();
  virtual void Shout(char const*);

  // From iName.
  virtual char const* GetName();
  virtual void SetName(char const*);
};

//---------- Implementation ---------

SCF_IMPLEMENT_FACTORY(csDog)

csDog::csDog(iBase* Parent) : scfImplementationType (this, Parent),
  Name ("<noname>")
{
}

csDog::~csDog()
{
}

void csDog::Walk()
{
  printf("%s: I'm walking\n", Name.GetData());
}

void csDog::Shout(char const* Message)
{
  printf("I'm %s: shout, shout, %s\n", Name.GetData(), Message);
}

// iName interface for dog.

char const* csDog::GetName()
{
  return Name;
}

void csDog::SetName(char const* NewName)
{
  Name = 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 = scfCreateInstance<iDog> ("csDog");
  if (!dog)
    fprintf(stderr, "No csDog shared class!\n");
  else
  {
    csRef<iName> name = scfQueryInterface<iName> (dog);
    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.




No user comments yet!


Add a comment