Skip to content

Commit

Permalink
shorter vptr vector
Browse files Browse the repository at this point in the history
  • Loading branch information
jll63 committed Feb 23, 2024
1 parent 5b95eee commit ef40db8
Show file tree
Hide file tree
Showing 29 changed files with 466 additions and 408 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,15 @@ Let's look at an example.
// library code

struct matrix {
virtual ~matrix() {}
virtual ~matrix() {
}
// ...
};

struct dense_matrix : matrix { /* ... */ };
struct diagonal_matrix : matrix { /* ... */ };
struct dense_matrix : matrix { /* ... */
};
struct diagonal_matrix : matrix { /* ... */
};

// -----------------------------------------------------------------------------
// application code
Expand Down Expand Up @@ -113,11 +116,11 @@ int main() {
`<yorel/yomm2/keywords.hpp>` is the library's main entry point. It declares a
set of macros, and injects a single name, [`virtual_`](virtual_.md), in the global
set of macros, and injects a single name, [`virtual_`](/reference/virtual_.md), in the global
namespace. The purpose of the header is to make it look as if open methods
are part of the language.
[`register_classes`](use_classes.md) informs the library of the existence of the classes, and
[`register_classes`](/reference/use_classes.md) informs the library of the existence of the classes, and
their inheritance relationships. Any class that can appear in a method call
needs to be registered, even if it is not directly referenced by a method.
Expand Down
8 changes: 8 additions & 0 deletions dev/reformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

cd "$(dirname $0)/.."

srcs=$(find include src examples tests \
-name '*.?pp' | grep -v cmake_fetchcontent | grep -v /CMakeFiles)

clang-format-15 -i --verbose $srcs
3 changes: 2 additions & 1 deletion examples/adventure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ define_method(std::string, fight, (Warrior & x, Dragon& y, Axe& z)) {
}

define_method(std::string, fight, (Character & x, Dragon& y, Hands& z)) {
return "Congratulations! You have just vainquished a dragon with your bare hands"
return "Congratulations! You have just vainquished a dragon with your bare "
"hands"
" (unbelievable, isn't it?)";
}

Expand Down
21 changes: 10 additions & 11 deletions examples/asteroids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,42 @@

class Thing {
public:
virtual ~Thing() {}
virtual ~Thing() {
}
};

class Asteroid : public Thing {
};
class Asteroid : public Thing {};

class Spaceship : public Thing {
};
class Spaceship : public Thing {};

register_classes(Thing, Spaceship, Asteroid);

declare_method(void, collideWith, (virtual_<Thing&>, virtual_<Thing&>));

define_method(void, collideWith, (Thing& left, Thing& right)) {
define_method(void, collideWith, (Thing & left, Thing& right)) {
// default collision handling
}

define_method(void, collideWith, (Asteroid& left, Asteroid& right)) {
define_method(void, collideWith, (Asteroid & left, Asteroid& right)) {
// handle Asteroid-Asteroid collision
}

define_method(void, collideWith, (Asteroid& left, Spaceship& right)) {
define_method(void, collideWith, (Asteroid & left, Spaceship& right)) {
// handle Asteroid-Spaceship collision
}

define_method(void, collideWith, (Spaceship& left, Asteroid& right)) {
define_method(void, collideWith, (Spaceship & left, Asteroid& right)) {
// handle Spaceship-Asteroid collision
}

define_method(void, collideWith, (Spaceship& left, Spaceship& right)) {
define_method(void, collideWith, (Spaceship & left, Spaceship& right)) {
// handle Spaceship-Spaceship collision
}

int main() {
yorel::yomm2::update();

Asteroid a1, a2;
Asteroid a1, a2;
Spaceship s1, s2;

collideWith(a1, a2);
Expand Down
1 change: 0 additions & 1 deletion examples/cmakeyomm2/adventure.cpp

This file was deleted.

113 changes: 113 additions & 0 deletions examples/cmakeyomm2/adventure.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (c) 2018-2021 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 <yorel/yomm2/keywords.hpp>

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

register_classes(
Character, Warrior, Device, Hands, Axe, Banana, Creature, Dragon, Bear);

declare_method(
std::string, fight,
(virtual_<Character&>, virtual_<Creature&>, virtual_<Device&>));

define_method(std::string, fight, (Character & x, Creature& y, Banana& z)) {
return "are you insane?";
}

define_method(std::string, fight, (Character & x, Creature& y, Axe& z)) {
return "not agile enough to wield";
}

define_method(std::string, fight, (Warrior & x, Creature& y, Axe& z)) {
return "and cuts it into pieces";
}

define_method(std::string, fight, (Warrior & x, Dragon& y, Axe& z)) {
return "and dies a honorable death";
}

define_method(std::string, fight, (Character & x, Dragon& y, Hands& z)) {
return "Congratulations! You have just vainquished a dragon with your bare "
"hands"
" (unbelievable, isn't it?)";
}

int main() {
yorel::yomm2::update();

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);
}
15 changes: 8 additions & 7 deletions examples/containers/arc_painter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ namespace painter {
namespace paint1d {

define_method(
painters,
void, paintObject, (Painter& painter, const geometries::Arc& arc))
{
painters, void, paintObject,
(Painter & painter, const geometries::Arc& arc)) {
++painter.counter;
method_definition(painters, void, (Painter&, const geometries::Line&))(painter, arc);
std::cout << " " << "painting arc\n";
method_definition(painters, void, (Painter&, const geometries::Line&))(
painter, arc);
std::cout << " "
<< "painting arc\n";
}

}
}
} // namespace paint1d
} // namespace painter
20 changes: 10 additions & 10 deletions examples/containers/concrete_shape_painters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ namespace painter {
namespace paint2d {

define_method(
painters,
void, paintObject, (Painter& painter, const geometries::Square& square))
{
method_definition(painters, void, (Painter&, const geometries::Shape&))(painter, square);
painters, void, paintObject,
(Painter & painter, const geometries::Square& square)) {
method_definition(painters, void, (Painter&, const geometries::Shape&))(
painter, square);
std::cout << "painting square\n";
}

define_method(
painters,
void, paintObject, (Painter& painter, const geometries::Circle& circle))
{
method_definition(painters, void, (Painter&, const geometries::Shape&))(painter, circle);
painters, void, paintObject,
(Painter & painter, const geometries::Circle& circle)) {
method_definition(painters, void, (Painter&, const geometries::Shape&))(
painter, circle);
std::cout << "painting Circle\n";
}

}
}
} // namespace paint2d
} // namespace painter
26 changes: 10 additions & 16 deletions examples/containers/geometries.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,23 @@

namespace geometries {

class Geometry
{
public:
virtual ~Geometry() {}
class Geometry {
public:
virtual ~Geometry() {
}
};

class Line: public Geometry {
};
class Line : public Geometry {};

class Arc : public Line {
};
class Arc : public Line {};

class Segment : public Line {
};
class Segment : public Line {};

class Shape : public Geometry {
};
class Shape : public Geometry {};

class Square : public Shape {
};
class Square : public Shape {};

class Circle : public Shape {
};
class Circle : public Shape {};

} // namespace geometries

Expand Down
9 changes: 4 additions & 5 deletions examples/containers/line_painter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ namespace painter {
namespace paint1d {

define_method_inline(
painters,
void, paintObject, (Painter& painter, const geometries::Line& arc))
{
painters, void, paintObject,
(Painter & painter, const geometries::Line& arc)) {
std::cout << "#" << painter.counter;
}

}
}
} // namespace paint1d
} // namespace painter

#endif
2 changes: 1 addition & 1 deletion examples/containers/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "geometries.hpp"
#include "painter.hpp"

using yorel::yomm2::virtual_;
using std::cout;
using yorel::yomm2::virtual_;

int main() {
yorel::yomm2::update();
Expand Down
22 changes: 13 additions & 9 deletions examples/containers/painter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@

namespace painter {

namespace paint1d { method_container(painters); }
namespace paint2d { method_container(painters); }
namespace paint1d {
method_container(painters);
}
namespace paint2d {
method_container(painters);
}

class Painter
{
public:
class Painter {
public:
void paint(const geometries::Geometry& geometry);
int painted() const;
private:

private:
int counter = 0;
friend_method(paint1d::painters);
friend_method(paint2d::painters, void, (Painter&, const geometries::Shape&));
friend_method(
paint2d::painters, void, (Painter&, const geometries::Shape&));
};

// Implements paint
declare_method(
void,
paintObject,
void, paintObject,
(Painter&, yorel::yomm2::virtual_<const geometries::Geometry&>));

inline void Painter::paint(const geometries::Geometry& geometry) {
Expand Down
Loading

0 comments on commit ef40db8

Please sign in to comment.