scope update
[luabind.git] / src / implicit_cast.cpp
blob7665fba4e9ce395a94fdfd5f4c18b7048450de9b
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 #define LUABIND_BUILDING
27 #include <luabind/luabind.hpp>
28 #include <luabind/detail/implicit_cast.hpp>
30 using namespace luabind::detail;
32 // returns -1 if there exists no implicit cast from the given class_rep
33 // to T. If there exists an implicit cast to T, the number of steps times 2
34 // is returned and pointer_offset is filled with the number of bytes
35 // the pointer have to be offseted to perform the cast
36 // the reason why we return the number of cast-steps times two, instead of
37 // just the number of steps is to be consistent with the function matchers. They
38 // have to give one matching-point extra to match const functions. There may be
39 // two functions that match their parameters exactly, but there is one const
40 // function and one non-const function, then (if the this-pointer is non-const)
41 // both functions will match. To avoid amiguaties, the const version will get
42 // one penalty point to make the match-selector select the non-const version. It
43 // the this-pointer is const, there's no problem, since the non-const function
44 // will not match at all.
45 namespace luabind { namespace detail
47 int implicit_cast(const class_rep* crep,
48 LUABIND_TYPE_INFO const& type_id,
49 int& pointer_offset)
51 int offset = 0;
52 if (LUABIND_TYPE_INFO_EQUAL(crep->type(), type_id)) return 0;
54 for (std::vector<class_rep::base_info>::const_iterator
55 i = crep->bases().begin(); i != crep->bases().end(); ++i)
57 int steps = implicit_cast(i->base, type_id, offset);
58 pointer_offset = offset + i->pointer_offset;
59 if (steps >= 0) return steps + 2;
61 return -1;