File: worm.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 (79 lines) | stat: -rw-r--r-- 1,485 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
/*
    This program demonstrates a simple SCF object with the ability to
    create and return another object with same interface.
*/

#include "cssysdef.h"
#include "iworm.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

class csWorm : public iWorm
{
  int length;
  int number;
public:
  SCF_DECLARE_IBASE;

  csWorm (iBase *iParent);
  virtual ~csWorm ();
  virtual void Crawl ();
  virtual int Length ();
  virtual iWorm *Split (int iLen1, int iLen2);
  void SetLength (int iLength)
  { length = iLength; }
};

//--------------- implementation ----------------

CS_IMPLEMENT_PLUGIN

SCF_IMPLEMENT_IBASE (csWorm)
  SCF_IMPLEMENTS_INTERFACE (iWorm)
SCF_IMPLEMENT_IBASE_END

static int worm_count = 1;

csWorm::csWorm (iBase *iParent)
{
  SCF_CONSTRUCT_IBASE (iParent);
  length = 50 + (rand () % 50);
  number = worm_count++;
}

csWorm::~csWorm ()
{
  printf ("Worm %d dies ...\n", number);
}

void csWorm::Crawl ()
{
  printf ("Worm %d crawls ...\n", number);
}

int csWorm::Length ()
{
  return length;
}

iWorm *csWorm::Split (int iLen1, int iLen2)
{
  int newlength = length * iLen1 / (iLen1 + iLen2);

  if (newlength >= length)
    return NULL;

  csWorm *newworm = new csWorm (scfParent);
  newworm->SetLength (length - newlength);
  length = newlength;
  return newworm;
}

// ... and now export all classes

SCF_IMPLEMENT_FACTORY (csWorm)

SCF_EXPORT_CLASS_TABLE (worm)
  SCF_EXPORT_CLASS (csWorm, "test.worm", "A Worm that crawls")
SCF_EXPORT_CLASS_TABLE_END