Removed Makefiles.
[luabind.git] / src / function.cpp
blob44e2b150d4bcaf4d1afd7892dcd22c5dd37e911f
1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
23 #include <luabind/lua_include.hpp>
25 #include <luabind/config.hpp>
26 #include <luabind/luabind.hpp>
28 namespace luabind { namespace detail { namespace free_functions {
30 void function_rep::add_overload(overload_rep const& o)
32 std::vector<overload_rep>::iterator i = std::find(
33 m_overloads.begin(), m_overloads.end(), o);
35 // if the overload already exists, overwrite the existing function
36 if (i != m_overloads.end())
38 *i = o;
40 else
42 m_overloads.push_back(o);
46 int function_dispatcher(lua_State* L)
48 function_rep* rep = static_cast<function_rep*>(
49 lua_touserdata(L, lua_upvalueindex(1))
52 bool ambiguous = false;
53 int min_match = std::numeric_limits<int>::max();
54 int match_index = -1;
55 bool ret;
57 #ifdef LUABIND_NO_ERROR_CHECKING
58 if (rep->overloads().size() == 1)
60 match_index = 0;
62 else
64 #endif
65 int num_params = lua_gettop(L);
66 ret = find_best_match(
68 , &rep->overloads().front()
69 , (int)rep->overloads().size()
70 , sizeof(overload_rep)
71 , ambiguous
72 , min_match
73 , match_index
74 , num_params
76 #ifdef LUABIND_NO_ERROR_CHECKING
78 #else
79 if (!ret)
81 // this bock is needed to make sure the std::string is destructed
83 std::string msg = "no match for function call '";
84 msg += rep->name();
85 msg += "' with the parameters (";
86 msg += stack_content_by_name(L, 1);
87 msg += ")\ncandidates are:\n";
89 msg += get_overload_signatures(
91 , rep->overloads().begin()
92 , rep->overloads().end()
93 , rep->name()
96 lua_pushstring(L, msg.c_str());
99 lua_error(L);
102 if (ambiguous)
104 // this bock is needed to make sure the std::string is destructed
106 std::string msg = "call of overloaded function '";
107 msg += rep->name();
108 msg += "(";
109 msg += stack_content_by_name(L, 1);
110 msg += ") is ambiguous\nnone of the overloads "
111 "have a best conversion:";
113 std::vector<overload_rep_base const*> candidates;
114 find_exact_match(
116 , &rep->overloads().front()
117 , (int)rep->overloads().size()
118 , sizeof(overload_rep)
119 , min_match
120 , num_params
121 , candidates
124 msg += get_overload_signatures_candidates(
126 , candidates.begin()
127 , candidates.end()
128 , rep->name()
131 lua_pushstring(L, msg.c_str());
133 lua_error(L);
135 #endif
136 overload_rep const& ov_rep = rep->overloads()[match_index];
138 #ifndef LUABIND_NO_EXCEPTIONS
141 #endif
142 return ov_rep.call(L, ov_rep.fun);
143 #ifndef LUABIND_NO_EXCEPTIONS
145 catch(const luabind::error&)
148 catch(const std::exception& e)
150 lua_pushstring(L, e.what());
152 catch (const char* s)
154 lua_pushstring(L, s);
156 catch(...)
158 std::string msg = rep->name();
159 msg += "() threw an exception";
160 lua_pushstring(L, msg.c_str());
162 // we can only reach this line if an exception was thrown
163 lua_error(L);
164 return 0; // will never be reached
165 #endif
168 }}} // namespace luabind::detail::free_functions