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