summaryrefslogtreecommitdiff
path: root/Examples/simple/simple.cpp
blob: 3a93bdc5b01c668c1cecc05d3ece4d39a017e9a8 (plain)
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
/**
 *  Simple.h
 *
 *  A very simple extension that does almost nothing
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2013 Copernica BV
 */
#include <string>
#include <iostream>
#include <phpcpp.h>

/**
 *  Namespace to use
 */
using namespace std;


Php::Value bubblesort(Php::Parameters &params)
{
    cout << "start bubblesort" << endl;
    
    // the array that was passed
    Php::Value array(params[0]);
    
    cout << "go return" << endl;
    
    return array;
    
    // size of the array
    int size = array.size();
    
    cout << "convert to native" << endl;
    
    int *x = new int[size];
    for (int i=0; i<size; i++) x[i] = array[i];
    
    cout << "converted" << endl;
    
    
    // outer loop
    for (int i=0; i<size; i++)
    {
        // left value
        int left = x[i];
//        cout << "left: " << left << endl;
        
        // inner loop
        for (int j=i+1; j<size; j++)
        {
            // right value
            int right = x[j];
            
            if (left < right) continue;
            
            // swap values
            x[i] = right;
            x[j] = left;
            left = right;
        }
    }
    
    cout << "algorithm end" << endl;
    
    Php::Value r;
    
    for (int i=0; i<size; i++) r[i] = x[i];
    
    
    delete[] x;
    
    // done
    return r;
}



/**
 *  Our own "my_plus" function that will be available in PHP
 *  @param  environment
 *  @param  params
 *  @return Value
 */
static Php::Value my_plus(Php::Parameters &params)
{
    Php::Value r(0);
    
    for (unsigned int i=0; i<params.size(); i++) r += params[i];
    
    return r;
    
    
//    int p1 = params[0];
//    int p2 = params[1];
//    return p1 + p2;
//    
//    cout << "global g1: " << env["g1"].stringValue() << endl;
//    cout << "global g2: " << env["g2"].stringValue() << endl;
//    
//    Php::Global g(env["g3"]);
//    
//    g = "zo kan het ook";
//    
//    string output = env.call("strtoupper","test in lowercase");
//    
//    cout << "output: " << output << endl;
//    
//    return p1 + p2;
}

/**
 *  Custom class that will be available in PHP
 */
class MyCustomClass : public Php::Base
{
private:
    int _x;
    
public:
    MyCustomClass()
    {
        _x = 3;
        cout << "MyCustomClass::MyCustomClass" << endl;
        cout << this << endl;
        cout << _x << endl;
    }
    
    virtual ~MyCustomClass()
    {
        cout << "MyCustomClass::~MyCustomClass" << endl;
    }

    virtual void __construct()
    {
        cout << "MyCustomClass::__construct" << endl;
    }

    virtual void __destruct()
    {
        cout << "MyCustomClass::__destruct" << endl;
    }
    
    void myMethod(Php::Parameters &params)
    {
        cout << "myMethod GETS CALLED!!!!" << endl;
        cout << this << endl;
        cout << _x << endl;
//        cout << "A: " << _properties["a"] << endl;
//        cout << "Z: " << _properties["z"] << endl;
    }
};

// symbols are exported according to the "C" language
extern "C"
{
    // export the "get_module" function that will be called by the Zend engine
    PHPCPP_EXPORT void *get_module() 
    { 
        // create extension
        static Php::Extension extension("simple","1.0");

        // define the functions
        extension.add("my_plus", my_plus, {
            Php::ByVal("a", Php::numericType),
            Php::ByVal("b", Php::numericType),
            Php::ByVal("c", "MyClass"),
            Php::ByRef("d", Php::stringType)
        });
        
        extension.add("bubblesort", bubblesort);
        
        Php::Method<MyCustomClass> x(&MyCustomClass::myMethod);
        
        // define classes
        extension.add("my_class", Php::Class<MyCustomClass>({
            Php::Public("myMethod", Php::Method<MyCustomClass>(&MyCustomClass::myMethod)),
            Php::Public("__construct", Php::Method<MyCustomClass>(&MyCustomClass::__construct))
        }));
        
        // return the module entry
        return extension.module();
    }
}