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