Jason Rhinelander | 5fffe20 | 2016-09-06 12:17:06 -0400 | [diff] [blame] | 1 | /* |
| 2 | tests/test_class_args.cpp -- tests that various way of defining a class work |
| 3 | |
| 4 | Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> |
| 5 | |
| 6 | All rights reserved. Use of this source code is governed by a |
| 7 | BSD-style license that can be found in the LICENSE file. |
| 8 | */ |
| 9 | |
| 10 | #include "pybind11_tests.h" |
| 11 | |
Jason Rhinelander | 5fffe20 | 2016-09-06 12:17:06 -0400 | [diff] [blame] | 12 | |
| 13 | template <int N> class BreaksBase {}; |
| 14 | template <int N> class BreaksTramp : public BreaksBase<N> {}; |
| 15 | // These should all compile just fine: |
| 16 | typedef py::class_<BreaksBase<1>, std::unique_ptr<BreaksBase<1>>, BreaksTramp<1>> DoesntBreak1; |
| 17 | typedef py::class_<BreaksBase<2>, BreaksTramp<2>, std::unique_ptr<BreaksBase<2>>> DoesntBreak2; |
| 18 | typedef py::class_<BreaksBase<3>, std::unique_ptr<BreaksBase<3>>> DoesntBreak3; |
| 19 | typedef py::class_<BreaksBase<4>, BreaksTramp<4>> DoesntBreak4; |
| 20 | typedef py::class_<BreaksBase<5>> DoesntBreak5; |
| 21 | typedef py::class_<BreaksBase<6>, std::shared_ptr<BreaksBase<6>>, BreaksTramp<6>> DoesntBreak6; |
| 22 | typedef py::class_<BreaksBase<7>, BreaksTramp<7>, std::shared_ptr<BreaksBase<7>>> DoesntBreak7; |
| 23 | typedef py::class_<BreaksBase<8>, std::shared_ptr<BreaksBase<8>>> DoesntBreak8; |
| 24 | #define CHECK_BASE(N) static_assert(std::is_same<typename DoesntBreak##N::type, BreaksBase<N>>::value, \ |
| 25 | "DoesntBreak" #N " has wrong type!") |
| 26 | CHECK_BASE(1); CHECK_BASE(2); CHECK_BASE(3); CHECK_BASE(4); CHECK_BASE(5); CHECK_BASE(6); CHECK_BASE(7); CHECK_BASE(8); |
| 27 | #define CHECK_ALIAS(N) static_assert(DoesntBreak##N::has_alias && std::is_same<typename DoesntBreak##N::type_alias, BreaksTramp<N>>::value, \ |
| 28 | "DoesntBreak" #N " has wrong type_alias!") |
| 29 | #define CHECK_NOALIAS(N) static_assert(!DoesntBreak##N::has_alias && std::is_void<typename DoesntBreak##N::type_alias>::value, \ |
| 30 | "DoesntBreak" #N " has type alias, but shouldn't!") |
| 31 | CHECK_ALIAS(1); CHECK_ALIAS(2); CHECK_NOALIAS(3); CHECK_ALIAS(4); CHECK_NOALIAS(5); CHECK_ALIAS(6); CHECK_ALIAS(7); CHECK_NOALIAS(8); |
| 32 | #define CHECK_HOLDER(N, TYPE) static_assert(std::is_same<typename DoesntBreak##N::holder_type, std::TYPE##_ptr<BreaksBase<N>>>::value, \ |
| 33 | "DoesntBreak" #N " has wrong holder_type!") |
| 34 | CHECK_HOLDER(1, unique); CHECK_HOLDER(2, unique); CHECK_HOLDER(3, unique); CHECK_HOLDER(4, unique); CHECK_HOLDER(5, unique); |
| 35 | CHECK_HOLDER(6, shared); CHECK_HOLDER(7, shared); CHECK_HOLDER(8, shared); |
| 36 | |
| 37 | // There's no nice way to test that these fail because they fail to compile; leave them here, |
| 38 | // though, so that they can be manually tested by uncommenting them (and seeing that compilation |
| 39 | // failures occurs). |
| 40 | |
| 41 | // We have to actually look into the type: the typedef alone isn't enough to instantiate the type: |
| 42 | #define CHECK_BROKEN(N) static_assert(std::is_same<typename Breaks##N::type, BreaksBase<-N>>::value, \ |
| 43 | "Breaks1 has wrong type!"); |
| 44 | |
| 45 | //// Two holder classes: |
| 46 | //typedef py::class_<BreaksBase<-1>, std::unique_ptr<BreaksBase<-1>>, std::unique_ptr<BreaksBase<-1>>> Breaks1; |
| 47 | //CHECK_BROKEN(1); |
| 48 | //// Two aliases: |
| 49 | //typedef py::class_<BreaksBase<-2>, BreaksTramp<-2>, BreaksTramp<-2>> Breaks2; |
| 50 | //CHECK_BROKEN(2); |
| 51 | //// Holder + 2 aliases |
| 52 | //typedef py::class_<BreaksBase<-3>, std::unique_ptr<BreaksBase<-3>>, BreaksTramp<-3>, BreaksTramp<-3>> Breaks3; |
| 53 | //CHECK_BROKEN(3); |
| 54 | //// Alias + 2 holders |
| 55 | //typedef py::class_<BreaksBase<-4>, std::unique_ptr<BreaksBase<-4>>, BreaksTramp<-4>, std::shared_ptr<BreaksBase<-4>>> Breaks4; |
| 56 | //CHECK_BROKEN(4); |
| 57 | //// Invalid option (not a subclass or holder) |
| 58 | //typedef py::class_<BreaksBase<-5>, BreaksTramp<-4>> Breaks5; |
| 59 | //CHECK_BROKEN(5); |
| 60 | //// Invalid option: multiple inheritance not supported: |
| 61 | //template <> struct BreaksBase<-8> : BreaksBase<-6>, BreaksBase<-7> {}; |
| 62 | //typedef py::class_<BreaksBase<-8>, BreaksBase<-6>, BreaksBase<-7>> Breaks8; |
| 63 | //CHECK_BROKEN(8); |
| 64 | |
| 65 | test_initializer class_args([](py::module &m) { |
| 66 | // Just test that this compiled okay |
| 67 | m.def("class_args_noop", []() {}); |
| 68 | }); |