File: cs_140.html

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 (247 lines) | stat: -rw-r--r-- 7,660 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- Created by texi2html 1.64 -->
<!-- 
Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author)
            Karl Berry  <karl@freefriends.org>
            Olaf Bachmann <obachman@mathematik.uni-kl.de>
            and many others.
Maintained by: Olaf Bachmann <obachman@mathematik.uni-kl.de>
Send bugs and suggestions to <texi2html@mathematik.uni-kl.de>
 
-->
<HTML>
<HEAD>
<TITLE>Crystal Space: SCF Example</TITLE>

<META NAME="description" CONTENT="Crystal Space: SCF Example">
<META NAME="keywords" CONTENT="Crystal Space: SCF Example">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META NAME="Generator" CONTENT="texi2html 1.64">

</HEAD>

<BODY LANG="" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000">

<A NAME="SEC305"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_139.html#SEC304"> &lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_141.html#SEC306"> &gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_131.html#SEC294"> &lt;&lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_134.html#SEC297"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_142.html#SEC310"> &gt;&gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="index.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_285.html#SEC711">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<HR SIZE=1>
<H3> 6.4.6 Example </H3>
<!--docid::SEC305::-->
<P>

Here is a completely working example. It implements a shared class that has a
base and an embedded interface.
</P><P>

Here are the interface files for the `<SAMP>iDog</SAMP>' interface (the base
interface of our class) and the `<SAMP>iName</SAMP>' interface (the interface
embedded into our class).
</P><P>

<EM>File</EM>: `<TT>idog.h</TT>'
</P><P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>#include "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* iWhat) = 0;
};
</pre></td></tr></table></P><P>

<EM>File</EM>: `<TT>iname.h</TT>'
</P><P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>#include "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* iName) = 0;
};
</pre></td></tr></table></P><P>

Now here is the implementation of a class for both of the mentioned
interfaces:
</P><P>

<EM>File</EM>: `<TT>dog.cpp</TT>'
</P><P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>#include "idog.h"
#include "iname.h"
#include &#60;stdio.h&#62;
#include &#60;string.h&#62;
#include &#60;stdlib.h&#62;

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* iName);
  } scfiName;
  friend class csName;

public:
  SCF_DECLARE_IBASE;
  csDog(iBase* iParent);
  virtual void Walk();
  virtual void Shout(char const* iWhat);
};

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

SCF_IMPLEMENT_IBASE(csDog)
  SCF_IMPLEMENTS_INTERFACE(iDog)
  SCF_IMPLEMENTS_EMBEDDED_INTERFACE(iName)
SCF_IMPLEMENT_IBASE_END

csDog::csDog(iBase* iParent)
{
  SCF_CONSTRUCT_IBASE(iParent);
  SCF_CONSTRUCT_EMBEDDED_IBASE(scfiName);
  Name = NULL;
}

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

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

// iName interface for dog.

SCF_IMPLEMENT_EMBEDDED_IBASE(csDog::csName)
  SCF_IMPLEMENTS_INTERFACE(iName)
SCF_IMPLEMENT_IBASE_END

char const* csDog::csName::GetName()
{
  return scfParent-&#62;Name;
}

void csDog::csName::SetName(char const* iName)
{
  if (scfParent-&#62;Name)
    free(scfParent-&#62;Name);
  scfParent-&#62;Name = strdup(iName);
}

// Now export all classes.

SCF_IMPLEMENT_FACTORY(csDog)

SCF_EXPORT_CLASS_TABLE(Dog)
  SCF_EXPORT_CLASS(csDog, "A Dog that shouts")
SCF_EXPORT_CLASS_TABLE_END
</pre></td></tr></table></P><P>

The above three files should be compiled together to get a shared library.
The shared library should export the <CODE>Create_csDog()</CODE> function
(implemented with the <CODE>SCF_IMPLEMENT_FACTORY()</CODE> macro).  On most platforms
this is achieved automatically, however on OS/2 you will need to write a
`<TT>.def</TT>' file for this.  Finally, here is the source code for a client
application that uses the `<SAMP>csDog</SAMP>' class:
</P><P>

<EM>File</EM>: `<TT>doggy.cpp</TT>'
</P><P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>#include &#60;stdio.h&#62;
#include "scf.h"
#include "idog.h"
#include "iname.h"
#include "inifile.h"

int main ()
{
  csIniFile config("scf.cfg");
  scfInitialize(&#38;config);

  iDog *dog = SCF_CREATE_INSTANCE(csDog, 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-&#62;SetName("Droopy");
      dog-&#62;Walk();
      dog-&#62;Shout("hello!");
      printf("Dog's name is %s\n", name-&#62;GetName());
      name-&#62;DecRef();
    }
    dog-&#62;DecRef();
  }

  iSCF::SCF-&#62;Finish();
}
</pre></td></tr></table></P><P>

Now the last thing: SCF uses a file called `<TT>scf.cfg</TT>' for storing
the <VAR>class name</VAR> to <VAR>shared library name</VAR> mapping. The file's format
is simple: `<SAMP><VAR>ClassName</VAR> = <VAR>SharedLibraryName</VAR></SAMP>'. So, for the
above example to work you have to add a line that reads `<SAMP>csDog = dog</SAMP>' to
the <CODE>[SCF.Registry]</CODE> section of the configuration file `<TT>scf.cfg</TT>'.
</P><P>

Alternatively, you can register classes at run time:
</P><P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>iSCF::SCF-&#62;RegisterClass("csDog", "dog");
</pre></td></tr></table></P><P>

<A NAME="SCF Advanced"></A>
<HR SIZE=1>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_139.html#SEC304"> &lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_141.html#SEC306"> &gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_131.html#SEC294"> &lt;&lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_134.html#SEC297"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_142.html#SEC310"> &gt;&gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="index.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_285.html#SEC711">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="cs_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<BR>  
<FONT SIZE="-1">
This document was generated

using <A HREF="http://www.mathematik.uni-kl.de/~obachman/Texi2html
"><I>texi2html</I></A>

</BODY>
</HTML>