File: main.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (126 lines) | stat: -rw-r--r-- 3,716 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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 <iostream>
#include <memory>
#include <string>

#include <boost/openmethod.hpp>
#include <boost/openmethod/initialize.hpp>

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<Character>, virtual_ptr<Creature>, virtual_ptr<Device>),
    std::string);

BOOST_OPENMETHOD_OVERRIDE(
    fight, (virtual_ptr<Character>, virtual_ptr<Creature>, virtual_ptr<Banana>),
    std::string) {
    return "are you insane?";
}

BOOST_OPENMETHOD_OVERRIDE(
    fight, (virtual_ptr<Character>, virtual_ptr<Creature>, virtual_ptr<Axe>),
    std::string) {
    return "not agile enough to wield";
}

BOOST_OPENMETHOD_OVERRIDE(
    fight, (virtual_ptr<Warrior>, virtual_ptr<Creature>, virtual_ptr<Axe>),
    std::string) {
    return "and cuts it into pieces";
}

BOOST_OPENMETHOD_OVERRIDE(
    fight, (virtual_ptr<Warrior>, virtual_ptr<Dragon>, virtual_ptr<Axe>),
    std::string) {
    return "and dies a honorable death";
}

BOOST_OPENMETHOD_OVERRIDE(
    fight, (virtual_ptr<Character>, virtual_ptr<Dragon>, virtual_ptr<Hands>),
    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<Character> bob = std::make_unique<Character>(),
                               rambo = std::make_unique<Warrior>();

    std::unique_ptr<Creature> elliott = std::make_unique<Dragon>(),
                              paddington = std::make_unique<Bear>();

    std::unique_ptr<Device> hands = std::make_unique<Hands>(),
                            axe = std::make_unique<Axe>(),
                            chiquita = std::make_unique<Banana>();

    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);
}