File: comcomp.cpp

package info (click to toggle)
camlidl 1.02-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,244 kB
  • ctags: 1,161
  • sloc: ml: 4,547; ansic: 951; cpp: 877; makefile: 324; sh: 56
file content (182 lines) | stat: -rw-r--r-- 4,514 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
// A simple COM component, as per the book

#include <stdio.h>
#include <stddef.h>

#define interface class
typedef struct { unsigned char data[16]; } IID;
typedef int HRESULT;
typedef unsigned long ULONG;
#define IsEqualIID(a,b) (memcmp(a, b, sizeof(IID)) == 0)
#define InterlockedIncrement(p) (++(*(p)))
#define InterlockedDecrement(p) (--(*(p)))
#define S_OK 0
#define E_NOINTERFACE (-1)

IID IID_IUnknown = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80 } };
IID IID_IX = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x81 } };
IID IID_IY = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x82 } };

interface IUnknown {
 public:
  virtual HRESULT QueryInterface(const IID& iid, void ** res) = 0;
  virtual ULONG AddRef() = 0;
  virtual ULONG Release() = 0;
};

interface IX : public IUnknown {
 public:
  virtual void F(int x) = 0;
};

interface IY : public IUnknown {
 public:
  virtual int G(int x) = 0;
  virtual int H() = 0;
  virtual int K(char ** str) = 0;
};

static int CA_ident = 0;

class CA : public IX, public IY {

private:
  int refcount;
  int ident;
public:

  virtual HRESULT QueryInterface(const IID& iid, void ** res) {
    if (IsEqualIID(&iid, &IID_IUnknown)) {
      printf("%d: QueryInterface: return IUnknown pointer.\n", ident);
      *res = (interface IX *) this;
    } else if (IsEqualIID(&iid, &IID_IX)) {
      printf("%d: QueryInterface: return IX pointer.\n", ident);
      *res = (interface IX *) this;
    } else if (IsEqualIID(&iid, &IID_IY)) {
      printf("%d: QueryInterface: return IY pointer.\n", ident);
      *res = (interface IY *) this;
    } else {
      printf("%d: QueryInterface: interface not supported.\n", ident);
      *res = NULL;
      return E_NOINTERFACE;
    }
    ((IUnknown *)(*res))->AddRef();
    return S_OK;
  }

  virtual ULONG AddRef() {
    ULONG res = InterlockedIncrement(&refcount);
    printf("%d: AddRef: new refcount is %lu\n", ident, res);
    return res;
  }

  virtual ULONG Release() {
    ULONG res = InterlockedDecrement(&refcount);
    printf("%d: Release: new refcount is %lu\n", ident, res);
    if (res == 0) {
      printf("%d: destroying component.\n", ident);
      delete this;
    }
    return res;
  }

  virtual void F(int x) {
    printf("%d: F(%d) called.\n", ident, x);
  }

  virtual int G(int x) {
    int res = 3 * x + 1;
    printf("%d: G(%d) called, returning %d.\n", ident, x, res);
    return res;
  }

  virtual int H() {
    printf("%d: H() called, returning 0.\n", ident);
    return 0;
  }

  virtual int K(char ** str) {
    printf("%d: K() called, returning 0 and `foobar'.\n", ident);
    *str = "foobar";
    return 0;
  }

  // constructor:
  CA() { refcount = 0; ident = ++CA_ident; }

  // destructor:
  ~CA() {
    printf("%d: destroy self.\n", ident);
  }
};

extern "C"
interface IUnknown * create_instance()
{
  interface IUnknown * res = (interface IX *) new CA();
  res->AddRef();
  return res;
}

extern "C"
void test_component(interface IUnknown * c)
{
  interface IX * cix;
  interface IY * ciy;
  int res;
  char * stringres;

  // Test IX interface
  if (c->QueryInterface(IID_IX, (void **) &cix) == S_OK) {
    printf("test: got IX interface.\n");
    printf("test: calling F(12) on it.\n");
    cix->F(12);
    printf("test: releasing the IX interface.\n");
    res = cix->Release();
    printf("test: return of Release() is %d.\n", res);
  }
  // Test IY interface
  if (c->QueryInterface(IID_IY, (void **) &ciy) == S_OK) {
    printf("test: got IY interface.\n");
    printf("test: calling G(3) on it.\n");
    res = ciy->G(3);
    printf("test: return value is %d.\n", res);
    printf("test: calling H() on it.\n");
    res = ciy->H();
    printf("test: return value is %d.\n", res);
    printf("test: calling K() on it.\n");
    res = ciy->K(&stringres);
    printf("test: hresult is %d, return string is `%s'.\n", res, stringres);
    printf("test: releasing the IY interface.\n");
    res = ciy->Release();
    printf("test: return of Release() is %d.\n", res);
  }
}

extern "C"
void test_ix(interface IX * c)
{
  test_component(c);
  //  printf("test: releasing the interface, return of Release() is %d.\n",
  //         c->Release());
}

extern "C"
void test_iy(interface IY * c)
{
  test_component(c);
  //  printf("test: releasing the interface, return of Release() is %d.\n",
  //         c->Release());
}


#if 0
extern "C"
int main(int argc, char ** argv)
{
  interface IUnknown * i = create_instance();
  test_component(i);
  i->Release();
  return 0;
}
#endif