Skip to content

Commit 315b28c

Browse files
committed
Adjust examples
1 parent 6f01974 commit 315b28c

File tree

4 files changed

+47
-45
lines changed

4 files changed

+47
-45
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ BasedOnStyle: Google
22
IndentWidth: 4
33
ColumnLimit: 100
44
SpaceAfterLogicalNot: true
5+
AllowShortLoopsOnASingleLine: false

README.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This library is inspired by [Rust Traits](https://doc.rust-lang.org/book/ch10-02
2222
## Quick Start
2323

2424
*traits* is a single header C++20 library. To use the library, make sure you meet the [minimum requirements](#minimum-requirements) and just include the header file [traits.h](https://github.com/VolumeGraphics/traits/blob/main/include/traits.h) in your source code.
25-
Alternatively, you can first try it out in [Compiler Explorer](https://godbolt.org/z/be31xGjrK).
25+
Alternatively, you can first try it out in [Compiler Explorer](https://godbolt.org/z/Eh5nKr3jT).
2626

2727
CMake projects might build, install and `find_package(traits)` or use fetch content:
2828

@@ -37,46 +37,45 @@ There are currently no plans to support [vcpkg](https://learn.microsoft.com/en-u
3737
### Canonical usage example
3838

3939
```c++
40-
#include <format>
4140
#include <iostream>
41+
#include <numbers>
4242
#include <vector>
4343

4444
#include "traits.h"
4545
using namespace traits;
4646

47-
constexpr auto Drawable = trait{
48-
TRAITS_METHOD(draw, void(std::ostream& stream) const)
47+
struct Circle {
48+
double radius{0.0};
4949
};
5050

51-
struct Circle {
52-
double radius{ 0.0 };
51+
struct Square {
52+
double length{0.0};
53+
};
54+
55+
constexpr auto Shape = trait{
56+
TRAITS_METHOD(area, double() const),
5357
};
5458

55-
constexpr auto get(impl_for<Drawable, Circle>) {
56-
return "draw"_method = [](Circle const& circle, std::ostream& stream) {
57-
stream << std::format("Circle {{ radius = {} }}\n", circle.radius);
59+
constexpr auto get(impl_for<Shape, Circle>) {
60+
return "area"_method = [](Circle const& circle) {
61+
return std::numbers::pi * circle.radius * circle.radius;
5862
};
5963
}
6064

61-
struct Square {
62-
double length{ 0.0 };
63-
};
64-
65-
constexpr auto get(impl_for<Drawable, Square>) {
66-
return "draw"_method = [](Square const& square, std::ostream& stream) {
67-
stream << std::format("Square {{ length = {} }}\n", square.length);
65+
constexpr auto get(impl_for<Shape, Square>) {
66+
return "area"_method = [](Square const& square) {
67+
return square.length * square.length;
6868
};
6969
}
7070

71-
int main()
72-
{
73-
std::vector<some<Drawable>> someDrawables;
71+
int main() {
72+
std::vector<some<Shape>> someShapes;
7473

75-
someDrawables.emplace_back(Circle{ 1.0 });
76-
someDrawables.emplace_back(Square{ 2.0 });
74+
someShapes.emplace_back(Circle{1.0});
75+
someShapes.emplace_back(Square{1.0});
7776

78-
for (auto const& drawable : someDrawables)
79-
drawable.draw(std::cout);
77+
for (auto const& shape : someShapes)
78+
std::cout << "Shape with area " << shape.area() << "\n";
8079
}
8180
```
8281

examples/quickstart.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
1-
#include <algorithm>
2-
#include <format>
31
#include <iostream>
4-
#include <string>
2+
#include <numbers>
53
#include <vector>
64

75
#include "traits.h"
86
using namespace traits;
97

10-
constexpr auto Drawable = trait{TRAITS_METHOD(draw, void(std::ostream& stream) const)};
11-
128
struct Circle {
139
double radius{0.0};
1410
};
1511

16-
constexpr auto get(impl_for<Drawable, Circle>) {
17-
return "draw"_method = [](Circle const& circle, std::ostream& stream) {
18-
stream << std::format("Circle {{ radius = {} }}\n", circle.radius);
19-
};
20-
}
21-
2212
struct Square {
2313
double length{0.0};
2414
};
2515

26-
constexpr auto get(impl_for<Drawable, Square>) {
27-
return "draw"_method = [](Square const& square, std::ostream& stream) {
28-
stream << std::format("Square {{ length = {} }}\n", square.length);
16+
constexpr auto Shape = trait{
17+
TRAITS_METHOD(area, double() const),
18+
};
19+
20+
constexpr auto get(impl_for<Shape, Circle>) {
21+
return "area"_method = [](Circle const& circle) {
22+
return std::numbers::pi * circle.radius * circle.radius;
2923
};
3024
}
3125

32-
auto main() -> int {
33-
std::vector<some<Drawable>> someDrawables;
26+
constexpr auto get(impl_for<Shape, Square>) {
27+
return "area"_method = [](Square const& square) { return square.length * square.length; };
28+
}
29+
30+
int main() {
31+
std::vector<some<Shape>> someShapes;
3432

35-
someDrawables.emplace_back(Circle{1.0});
36-
someDrawables.emplace_back(Square{2.0});
33+
someShapes.emplace_back(Circle{1.0});
34+
someShapes.emplace_back(Square{1.0});
3735

38-
for (auto const& drawable : someDrawables) drawable.draw(std::cout);
36+
for (auto const& shape : someShapes)
37+
std::cout << "Shape with area " << shape.area() << "\n";
3938
}

examples/readme.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ struct test {
5454
test(auto callable) { tests.emplace_back(callable); }
5555

5656
static void run_all() {
57-
for (auto const& test : tests) test();
57+
for (auto const& test : tests)
58+
test();
5859
}
5960

6061
private:
@@ -96,7 +97,8 @@ const auto MotivationalExample = test([] {
9697
someDrawables.emplace_back(Circle{1.0});
9798
someDrawables.emplace_back(Square{2.0});
9899

99-
for (auto const& drawable : someDrawables) drawable.draw(std::cout);
100+
for (auto const& drawable : someDrawables)
101+
drawable.draw(std::cout);
100102
});
101103

102104
// traits step by step
@@ -503,7 +505,8 @@ auto InvokeCallbacks = test([] {
503505
someCallbacks.emplace_back(FirstCallback{});
504506
someCallbacks.emplace_back(SecondCallback{});
505507

506-
for (auto& callback : someCallbacks) callback();
508+
for (auto& callback : someCallbacks)
509+
callback();
507510
});
508511

509512
// last example

0 commit comments

Comments
 (0)