aboutsummaryrefslogtreecommitdiff
path: root/src/vrepFfi-inl.h
blob: 554d954f7ceb425850bf217546bab08bc71e7ebd (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
/* vrepFfi-inl.h -- exposing functions to V-REP
 * Copyright (C) 2013  Galois, Inc.
 *
 * This library is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact Galois, complete the Web form at <http://corp.galois.com/contact/>
 * or write to Galois, Inc., 421 Southwest 6th Avenue, Suite 300, Portland,
 * Oregon, 97204-1622. */

#ifndef PPAML_VREP_AUTOMOBILE_PLUGIN_VREPFFI_INL_H_
#define PPAML_VREP_AUTOMOBILE_PLUGIN_VREPFFI_INL_H_

namespace vrep {

    namespace {

        // Converting a variadic template type to a Lua type list //

        std::forward_list<int> toLuaTypes() {
            return std::forward_list<int>();
        }

        template<typename Car>
        std::forward_list<int> toLuaTypes() {
            std::forward_list<int> luaTypes = toLuaTypes();
            luaTypes.push_front(LuaType<Car>::id);
            return luaTypes;
        }

        template<typename Car, typename Cadr, typename ...Cddr>
        std::forward_list<int> toLuaTypes() {
            std::forward_list<int> luaTypes = toLuaTypes<Cadr, Cddr...>();
            luaTypes.push_front(LuaType<Car>::id);
            return luaTypes;
        }

    }


    // Exposing C++ functions to Lua //

    template<typename ...Args>
    void exposeFunction(const std::string name, const std::string callTips,
                        simVoid (*const f)(SLuaCallBack *)) {
        // Compute the Lua signature.
        std::vector<int> luaTypes;
        luaTypes.push_back(0);
        for (int type : toLuaTypes<Args...>()) {
            luaTypes.push_back(type);
            // We just added an argument.
            luaTypes[0]++;
        }
        // Register the function with Lua.
        VREP(simRegisterCustomLuaFunction(name.c_str(), callTips.c_str(),
                                          luaTypes.data(), f));
    }


    // Extracting Lua arguments from the callback structure //

    template<typename T>
    T LuaCall::expectAtom() {
        ensureNextArgType(LuaType<T>::id);
        const T result = unsafeGetAtom<T>();
        argIdx++;
        return result;
    }

    template<typename T>
    std::vector<T> LuaCall::expectTable() {
        ensureNextArgType(LuaType<std::vector<T>>::id);
        const std::vector<T> result = unsafeGetTable<T>();
        argIdx++;
        return result;
    }

    template<typename T>
    std::vector<T> LuaCall::unsafeGetTable() {
        // Determine how long the table is.
        size_t tableLen;
        if (simCall->inputArgTypeAndSize[2 * argIdx] & sim_lua_arg_table) {
            tableLen = simCall->inputArgTypeAndSize[2 * argIdx + 1];
        } else {
            tableLen = 1;
        }
        // Build the result.
        std::vector<T> result(tableLen);
        for (size_t i = 0; i < tableLen; i++) {
            result[i] = unsafeGetAtom<T>();
        }
        return result;
    }

    template<>
    bool LuaCall::unsafeGetAtom() {
        return *cursorBool++;
    }

    template<>
    int LuaCall::unsafeGetAtom() {
        return *cursorInt++;
    }

    template<>
    float LuaCall::unsafeGetAtom() {
        return *cursorFloat++;
    }


    // Error handling //

    MarshalingError::MarshalingError(const std::string &whatArg)
        : Error(whatArg) {
    }

    MarshalingError::MarshalingError(const char *whatArg)
        : MarshalingError(std::string(whatArg)) {
    }

}

#endif