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