summaryrefslogtreecommitdiff
path: root/include/callstatic.h
blob: 3979ef336c56fd5f82538466f2cc012a5c9ad521 (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
/**
 *  CallStatic.h
 *
 *  Class that performs a lot of C++11 magic to find out if the __callStatic()
 *  method was implemented by the user, and if it was, calls it
 *
 */
 
namespace Php {

/**
 *  SFINAE test to check if the __callStatic method is defined
 * 
 *  This type trait checks if the __callStatic method is defined in class T
 * 
 *  @see http://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-template-to-check-for-a-functions-existence
 */
template <typename T>
class HasCallStatic
{
    typedef char one;
    typedef long two;

    template <typename C> static one test( decltype(&C::__callStatic) ) ;
    template <typename C> static two test(...);

public:
    static const bool value = sizeof(test<T>(0)) == sizeof(char);
};

/**
 *  Function that only exists if the class T has a __callStatic method
 *  @param  name        Name of the function
 *  @param  params      Parameters passed to the function
 *  @return Value
 */
template<typename T>
typename std::enable_if<HasCallStatic<T>::value, Value >::type
callStatic(const char *name, Parameters &params)
{
    // call the __callStatic() function
    return T::__callStatic(name, params);
}

/**
 *  Function that only exists if the class T does not have a __callStatic method
 *  @param  name        Name of the function
 *  @param  params      Parameters passed to the function
 *  @return Value
 */
template<typename T>
typename std::enable_if<!HasCallStatic<T>::value >::type
callStatic(const char *name, Parameters &params)
{
    std::cout << "has NO call static" << std::endl;
    
    return nullptr;
}

/**
 *  End namespace
 */
}