// Copyright (c) 2018-2025 Jean-Louis Leroy // Distributed under the Boost Software License, Version 1.0. // See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include #include using boost::openmethod::virtual_ptr; struct Character { virtual ~Character() { } }; struct Warrior : Character {}; struct Device { virtual ~Device() { } }; struct Hands : Device {}; struct Axe : Device {}; struct Banana : Device {}; struct Creature { virtual ~Creature() { } }; struct Dragon : Creature {}; struct Bear : Creature {}; BOOST_OPENMETHOD_CLASSES( Character, Warrior, Device, Hands, Axe, Banana, Creature, Dragon, Bear); BOOST_OPENMETHOD( fight, (virtual_ptr, virtual_ptr, virtual_ptr), std::string); BOOST_OPENMETHOD_OVERRIDE( fight, (virtual_ptr, virtual_ptr, virtual_ptr), std::string) { return "are you insane?"; } BOOST_OPENMETHOD_OVERRIDE( fight, (virtual_ptr, virtual_ptr, virtual_ptr), std::string) { return "not agile enough to wield"; } BOOST_OPENMETHOD_OVERRIDE( fight, (virtual_ptr, virtual_ptr, virtual_ptr), std::string) { return "and cuts it into pieces"; } BOOST_OPENMETHOD_OVERRIDE( fight, (virtual_ptr, virtual_ptr, virtual_ptr), std::string) { return "and dies a honorable death"; } BOOST_OPENMETHOD_OVERRIDE( fight, (virtual_ptr, virtual_ptr, virtual_ptr), std::string) { return "Congratulations! You have just vainquished a dragon with your bare " "hands" " (unbelievable, isn't it?)"; } auto main() -> int { boost::openmethod::initialize(); std::unique_ptr bob = std::make_unique(), rambo = std::make_unique(); std::unique_ptr elliott = std::make_unique(), paddington = std::make_unique(); std::unique_ptr hands = std::make_unique(), axe = std::make_unique(), chiquita = std::make_unique(); std::cout << "bob fights elliot with axe:\n" << fight(*bob, *elliott, *axe) << "\n"; // bob fights elliot with axe: // not agile enough to wield std::cout << "rambo fights paddington with axe:\n" << fight(*rambo, *paddington, *axe) << "\n"; // rambo fights paddington with axe: // and cuts it into pieces std::cout << "rambo fights paddington with banana:\n" << fight(*rambo, *paddington, *chiquita) << "\n"; // rambo fights paddington with banana: // are you insane? std::cout << "rambo fights elliott with axe:\n" << fight(*rambo, *elliott, *axe) << "\n"; // rambo fights elliott with axe: // and dies a honorable death std::cout << "bob fights elliot with hands:\n" << fight(*bob, *elliott, *hands) << "\n"; // bob fights elliot with hands: Congratulations! You have just vainquished // a dragon with your bare hands (unbelievable, isn't it?) std::cout << "rambo fights elliot with hands:\n" << fight(*rambo, *elliott, *hands) << "\n"; // rambo fights elliot with hands: // you just killed a dragon with your bare hands. Incredible isn't it? return 0; } auto call_fight(Character& character, Creature& creature, Device& device) { return fight(character, creature, device); }