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
/// @file real_world.cpp
/// @brief A realistic C++20 module unit, written the way a person writes one.
///
/// This file is a test fixture, not a sample: `tests/ast.rs` and `tests/doc.rs` parse it and assert
/// what comes out. It is deliberately written with the layout a real file has — blank-line-separated
/// sections, a namespace wrapping everything, and documentation comments in the positions people
/// actually write them — because per-construct tests cannot catch a regression in how constructs
/// interact.
module;
#include <vector>
#include <string>
export module shapes;
import <iostream>;
import :geometry;
namespace shapes {
/// The largest grid this module will allocate.
inline constexpr int kMax = 16;
/// Which primitive a shape is.
enum class Kind : unsigned char { Circle, Square, Triangle };
/// A point in the plane.
///
/// The coordinates are private and read through accessors, so the invariant that a point is always
/// finite has one place to live.
struct Point {
public:
/// Constructs a point.
/// @param x the horizontal coordinate
/// @param y the vertical coordinate
Point(double x, double y) : x_(x), y_(y) {}
/// @returns the horizontal coordinate
double x() const noexcept { return x_; }
/// @returns the vertical coordinate
double y() const noexcept { return y_; }
private:
double x_ = 0.0;
double y_ = 0.0;
};
/// A fixed-size grid.
///
/// @tparam T the element type
/// @tparam N how many elements fit, which is part of the type rather than the value
template <typename T, int N = 3>
class Grid {
public:
/// @param i the index, which must be less than `N`
/// @returns a reference to the element
/// @code
/// Grid<double, 4> g;
/// g.at(0) = 1.0;
/// @endcode
T& at(int i) { return cells_[i]; }
/// @copydoc at(int)
const T& at(int i) const { return cells_[i]; }
private:
T cells_[N];
};
/// The interface every shape implements.
///
/// @note Deliberately has no data members: a shape's state belongs to the shape.
class Shape {
public:
virtual ~Shape() = default;
/// @returns the area, in the unit the shape was constructed with
/// @warning Not constexpr: the calculation may not be usable at compile time.
virtual double area() const = 0;
auto name() -> const char* { return name_; }
protected:
const char* name_ = "shape";
};
/// The squared distance between two points.
///
/// Squared, not Euclidean: the caller usually only compares distances, and the square root is the
/// expensive part.
///
/// @param a the first point
/// @param b the second point
/// @returns the squared distance
double distance(const Point& a, const Point& b) {
double dx = a.x() - b.x();
double dy = a.y() - b.y();
return dx * dx + dy * dy;
}
} // namespace shapes
export using Point = shapes::Point;
export int helper();