[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

#### 4.3.1.9 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 <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    #include "cssysdef.h"
    #include "idog.h"
    #include "iname.h"

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

    public:
      SCF_DECLARE_IBASE;
      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 = strdup("<noname>");
    }

    csDog::~csDog()
    {
      free(Name);
    }

    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::GetName()
    {
      return Name;
    }

    void csDog::SetName(char const* NewName)
    {
      if (Name != 0)
        free(Name);
      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 = 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.