File: zoo.cpp

package info (click to toggle)
crystalspace 0.94-20020412-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 62,276 kB
  • ctags: 52,843
  • sloc: cpp: 274,783; ansic: 6,608; perl: 6,276; objc: 3,952; asm: 2,942; python: 2,354; php: 542; pascal: 530; sh: 430; makefile: 370; awk: 193
file content (163 lines) | stat: -rw-r--r-- 4,076 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
    This program demonstrates several aspects of SCF usage:

    - Dynamic class loading/unloading (Dog and Worm classes)
    - Always-static class linkage (Frog class)
    - Object-created-within-object (Worm::Split method)
    - Run-time class registration with SCF kernel (no need for scf.cfg)
    - Embedded interface (iName within iDog)
    - Multiple interfaces (iFrog also supports iName)
*/

#include <stdio.h>
#include "cssysdef.h"
#include "csutil/scf.h"
#include "csutil/cfgfile.h"

#include "iname.h"
#include "idog.h"
#include "iworm.h"
#include "ifrog.h"

CS_IMPLEMENT_APPLICATION

// for the case we're using static linking ...
#ifdef CS_STATIC_LINKED
  SCF_REGISTER_STATIC_LIBRARY (dog)
  SCF_REGISTER_STATIC_LIBRARY (worm)
#endif

// frog is always statically linked
SCF_REGISTER_STATIC_LIBRARY (Frog)

// This function will clone given object, do something with him
// and finally destroy
void Clone (iBase *iObject)
{
  printf ("--- cloning the object\n");
  iFactory *factory = SCF_QUERY_INTERFACE (iObject, iFactory);
  if (!factory)
  {
    fprintf (stderr, "Object does not support the iFactory interface!\n");
    return;
  }
  printf ("Class description: %s\n", factory->QueryDescription ());

  // Create a new instance of the class
  iBase *newobj = (iBase *)factory->CreateInstance ();

  // Release the factory interface of the parent - we don't need it anymore
  factory->DecRef ();

  if (!newobj)
  {
    fprintf (stderr, "Failed to create a object of the same type!\n");
    return;
  }

  // Check if the object supports the iName interface
  iName *name = SCF_QUERY_INTERFACE (newobj, iName);
  if (name)
  {
    if (!name->GetName())
      printf ("Object is unnamed; renaming to \"Clone\"\n");
    else
      printf ("Object's name is \"%s\"; renaming to \"Clone\"\n",
        name->GetName ());
    name->SetName ("Clone");
    name->DecRef ();
  }

  // Delete the clone
  newobj->DecRef ();
}

//Changing this causes a link2001 error for win32.
//int main(int,char **)
int main(int argc, char *argv[])
{
  (void)argc;
  (void)argv;
#if 0
  // This method requires you register dlls with scfreg (or manually) in scf.cfg
  csConfigFile config ("scf.cfg");
  scfInitialize (&config);
#else
  // Don't use a .cfg file, instead manually register classes
  scfInitialize (NULL);
  iSCF::SCF->RegisterClass ("test.dog", "dog");
  iSCF::SCF->RegisterClass ("test.worm", "worm");
#endif

  iDog *dog = SCF_CREATE_INSTANCE ("test.dog", iDog);
  if (!dog)
    fprintf (stderr, "No csDog shared class!\n");
  else
  {
    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->Barf ("hello!");
      printf ("Dog's name is %s\n", name->GetName ());
      name->DecRef ();
    }

    Clone (dog);

    dog->DecRef ();
  }

  printf ("----------------\n");

  iWorm *worm = SCF_CREATE_INSTANCE ("test.worm", iWorm);
  if (!worm)
    fprintf (stderr, "No csWorm shared class!\n");
  else
  {
    worm->Crawl ();
    printf ("Worm 1 length is %d\n", worm->Length ());
    printf ("Splitting worm into two ...\n");
    iWorm *worm2 = worm->Split (60, 40);
    if (!worm2)
      fprintf (stderr, "Failed to split the worm!\n");
    else
    {
      printf ("Worm1 length: %d  Worm2 length: %d\n", worm->Length (), worm2->Length ());
      worm2->Crawl ();
      worm2->DecRef ();
    }

    Clone (worm);

    worm->DecRef ();
  }

  printf ("----------------\n");
  iFrog *frog = SCF_CREATE_INSTANCE ("test.frog", iFrog);
  if (!frog)
    fprintf (stderr, "No csFrog shared class!\n");
  else
  {
    iName *name = SCF_QUERY_INTERFACE (frog, iName);
    if (!name)
      fprintf (stderr, "frog does not support iName interface!\n");
    else
    {
      name->SetName ("Froggy");
      frog->Jump ();
      frog->Croak ("Barf");
      printf ("Frog's name is %s\n", name->GetName ());
      name->DecRef ();
    }

    Clone (frog);

    frog->DecRef ();
  }
  iSCF::SCF->Finish ();
  return 0;
}