summaryrefslogtreecommitdiff
path: root/include/function.h
blob: fec83e9d5d9716946c5a92c8842b2ac3b940ec1a (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
/**
 *  Function.h
 *
 *  Object represents a callable function that is defined with the CPP API.
 *  After you've instantiated the extension, you can add function objects to
 *  it.
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2013 Copernica BV
 */
 
/**
 *  Set up namespace
 */
namespace PhpCpp {

/**
 *  Forward definitions
 */
class Callable;

/** 
 *  Class definition
 */
class Function
{
public:
    /**
     *  Constructor
     *  @param  name        Name of the function
     *  @param  arguments   The arguments that can be passed to the function
     */
    Function(const std::string &name, const std::initializer_list<Argument> &arguments);

    /**
     *  Constructor
     *  @param  name        Name of the function
     */
    Function(const char *name) : Function(name, {}) {}
    
    /**
     *  Copy constructor
     *  @param  function    The other function
     */
    Function(const Function &function)
    {
        // copy other object
        _refcount = function._refcount;
        _callable = function._callable;
        
        // increate number of references
        (*_refcount)++;
    }
    
    /**
     *  Destructor
     */
    virtual ~Function()
    {
        // cleanup the object
        cleanup();
    }
    
    /**
     *  Assignment operator
     *  @param  function    The other function
     *  @return Function
     */
    Function &operator=(const Function &function)
    {
        // skip self assignment
        if (&function == this) return *this;
        
        // cleanup the object
        cleanup();
        
        // copy other object
        _refcount = function._refcount;
        _callable = function._callable;
        
        // increate number of references
        (*_refcount)++;
        
        // done
        return *this;
    }

    /**
     *  Method that gets called every time the function is executed
     *  @param  request     The request during which the call was made
     *  @param  arguments   The actual arguments that were passed
     *  @return Variable    Return value
     */
    virtual Value invoke(const Request *request, const std::initializer_list<Value> &arguments);
    
    /**
     *  Get access to the internal object
     *  @return Callable
     *  @internal
     */
    Callable *internal() const
    {
        return _callable;
    }

protected:
    /**
     *  Pointer to the callable object
     *  @var smart_ptr
     */
    Callable *_callable;
    
    /**
     *  Counter with the number of references
     *  @var integer
     */
    int *_refcount;


    /**
     *  Remove one reference
     */
    void cleanup();
};

/**
 *  End of namespace
 */
}