import source from 1.3.40
[external/swig.git] / Examples / php / reference / example.cxx
1 /* File : example.cxx */
2
3 /* Deal with Microsoft's attempt at deprecating C standard runtime functions */
4 #if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
5 # define _CRT_SECURE_NO_DEPRECATE
6 #endif
7
8 #include "example.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 Vector operator+(const Vector &a, const Vector &b) {
13   Vector r;
14   r.x = a.x + b.x;
15   r.y = a.y + b.y;
16   r.z = a.z + b.z;
17   return r;
18 }
19
20 char *Vector::as_string() {
21   static char temp[512];
22   sprintf(temp,"Vector %p (%g,%g,%g)", this, x,y,z);
23   return temp;
24 }
25
26 VectorArray::VectorArray(int size) {
27   items = new Vector[size];
28   maxsize = size;
29   printf("VectorArray new: self=%p\n",this);
30 }
31
32 VectorArray::~VectorArray() {
33   printf("VectorArray delete: self=%p\n",this);
34   delete [] items;
35 }
36
37 Vector &VectorArray::operator[](int index) {
38   printf("VectorArray: read[%d] self=%p\n",index,this);
39   if ((index < 0) || (index >= maxsize)) {
40     printf("Panic! Array index out of bounds.\n");
41     exit(1);
42   }
43   return items[index];
44 }
45
46 int VectorArray::size() {
47   printf("VectorArray: size %d self=%p\n",maxsize,this);
48   return maxsize;
49 }