summaryrefslogtreecommitdiff
path: root/tests/cpp/include/Classes_and_objects.h
blob: 0977da985c6715bfdf2badd55b2576db27034b8e (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
 *  
 *
 *  Classes_and_objects.h
 *
 */


/**
 *  Set up namespace
 */
namespace TestBaseClass {

    

    class MyCustomClass : public Php::Base, public Php::Countable
    {
    private:
        int _x = 3;
        
    public:
        MyCustomClass()
        {
            std::cerr << "MyCustomClass::MyCustomClass()" << std::endl;
        }

        MyCustomClass(int value) : _x(value)
        {
            std::cerr << "MyCustomClass::MyCustomClass(" << value << ")" << std::endl;
        }

        MyCustomClass(const MyCustomClass &that)
        {
            //std::cerr << "MyCustomClass::MyCustomClass copy constructor" << std::endl;
        }
        
        virtual ~MyCustomClass()
        {
            std::cerr << "MyCustomClass::~MyCustomClass" << std::endl;
        }

        virtual long int count() override
        {
            return 33;
        }

        Php::Value myMethod(Php::Parameters &params)
        {
            // check number of parameters
            //if (params.size() != 1) throw Php::Exception("Invalid number of parameters supplied");
            
            Php::out << "myMethod is called for object " << _x << std::endl;

            return 5;

        }
    };


    /**
     *  Test custom comparison operator
     */
    class Comparable : public Php::Base
    {
    private:
        /**
         *  Internal value of the class
         *  @var    int
         */
        static int count;
        int _nom;
        int _value;

    public:
        /**
         *  C++ constructor
         */
        Comparable() 
        {
            // start with random value
            //_value = rand();
            _nom   = ++count;
            _value = _nom%2+1;
        }
        
        /**
         *  C++ destructor
         */
        virtual ~Comparable() {}

        /**
         *  Cast the object to a string
         *  @return std::string
         */
        std::string __toString()
        {
            return "Obj#" + std::to_string(_nom) + "(" + std::to_string(_value) + ")";
        }
        
        /**
         *  Compare with a different object
         *  @param  that
         *  @return int
         */
        int __compare(const Comparable &that) const
        {
            return _value - that._value;
        }
    };
    int Comparable::count = 0;


    /**
     *  Begin test static functions
     */


        /**
         *  Regular function
         *
         *  Because a regular function does not have a 'this' pointer,
         *  it has the same signature as static methods
         *
         *  @param  params      Parameters passed to the function
         */
        void testStaticRegFunc(Php::Parameters &params)
        {
            Php::out << "testStatic regular function"<< std::endl;
        }

        /**
         *  A very simple class that will not be exported to PHP
         */
        class testStaticPrivClass
        {
        public:
            /**
             *  C++ constructor and destructor
             */
            testStaticPrivClass() {}
            virtual ~testStaticPrivClass() {}

            /** 
             *  Static method
             *
             *  A static method also has no 'this' pointer and has
             *  therefore a signature identical to regular functions
             *
             *  @param  params      Parameters passed to the method
             */
            static void staticMethod(Php::Parameters &params)
            {
                Php::out << "testStaticPrivClass::staticMethod()"<< std::endl;
            }
        };

        /**
         *  A very simple class that will be exported to PHP
         */
        class  testStaticPubClass : public Php::Base
        {
        public:
            /**
             *  C++ constructor and destructor
             */
            testStaticPubClass() {}
            virtual ~testStaticPubClass() {}

            /** 
             *  Another static method
             *
             *  This static has exactly the same signature as the
             *  regular function and static method that were mentioned
             *  before
             *
             *  @param  params      Parameters passed to the method
             */
            static void staticMethod(Php::Parameters &params)
            {
                Php::out << "testStaticPubClass::staticMethod()"<< std::endl;
            }
        };


    /**
     *  End test static functions
     */






/**
 *  End of namespace
 */
}