diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2019-12-29 19:15:26 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2019-12-29 19:15:26 +0100 |
| commit | f14db076fe96a62a1f7b0f76f7fd851714147162 (patch) | |
| tree | 3027e24d266779d203c1a4dbb65db3c054edc0d8 /examples | |
| parent | 3a07d605ebb5d9dbee350800d551039a5b80ac6b (diff) | |
| download | newtype-f14db076fe96a62a1f7b0f76f7fd851714147162.tar.xz newtype-f14db076fe96a62a1f7b0f76f7fd851714147162.zip | |
doc: restructure documentation
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/src/basic_usage.cpp | 39 | ||||
| -rw-r--r-- | examples/src/basic_usage_with_read.cpp | 39 | ||||
| -rw-r--r-- | examples/src/basic_usage_with_show.cpp | 41 |
3 files changed, 119 insertions, 0 deletions
diff --git a/examples/src/basic_usage.cpp b/examples/src/basic_usage.cpp new file mode 100644 index 0000000..a2a50cd --- /dev/null +++ b/examples/src/basic_usage.cpp @@ -0,0 +1,39 @@ +#include <newtype/new_type.hpp> + +#include <iostream> + +using Width = nt::new_type<unsigned int, struct width_tag>; +using Height = nt::new_type<unsigned int, struct height_tag>; +using Area = nt::new_type<unsigned int, struct area_tag>; + +struct Rectangle +{ + constexpr Rectangle(Width w, Height h) + : width{w} + , height{h} + { + } + + auto constexpr area() const noexcept -> Area + { + return {width.decay() * height.decay()}; + } + +private: + Width width; + Height height; +}; + +int main() +{ + auto w{0u}, h{0u}; + + std::cin >> w >> h; + + auto width = Width{w}; + auto height = Height{h}; + + auto rect = Rectangle{width, height}; + + std::cout << rect.area().decay() << '\n'; +}
\ No newline at end of file diff --git a/examples/src/basic_usage_with_read.cpp b/examples/src/basic_usage_with_read.cpp new file mode 100644 index 0000000..87fcc1a --- /dev/null +++ b/examples/src/basic_usage_with_read.cpp @@ -0,0 +1,39 @@ +#include <newtype/derivable.hpp> +#include <newtype/deriving.hpp> +#include <newtype/new_type.hpp> + +#include <iostream> + +using Width = nt::new_type<unsigned int, struct width_tag, deriving(nt::Read)>; +using Height = nt::new_type<unsigned int, struct height_tag, deriving(nt::Read)>; +using Area = nt::new_type<unsigned int, struct area_tag, deriving(nt::Show)>; + +struct Rectangle +{ + constexpr Rectangle(Width w, Height h) + : width{w} + , height{h} + { + } + + auto constexpr area() const noexcept -> Area + { + return {width.decay() * height.decay()}; + } + +private: + Width width; + Height height; +}; + +int main() +{ + auto width = Width{}; + auto height = Height{}; + + std::cin >> width >> height; + + auto rect = Rectangle{width, height}; + + std::cout << rect.area() << '\n'; +}
\ No newline at end of file diff --git a/examples/src/basic_usage_with_show.cpp b/examples/src/basic_usage_with_show.cpp new file mode 100644 index 0000000..1a83968 --- /dev/null +++ b/examples/src/basic_usage_with_show.cpp @@ -0,0 +1,41 @@ +#include <newtype/derivable.hpp> +#include <newtype/deriving.hpp> +#include <newtype/new_type.hpp> + +#include <iostream> + +using Width = nt::new_type<unsigned int, struct width_tag>; +using Height = nt::new_type<unsigned int, struct height_tag>; +using Area = nt::new_type<unsigned int, struct area_tag, deriving(nt::Show)>; + +struct Rectangle +{ + constexpr Rectangle(Width w, Height h) + : width{w} + , height{h} + { + } + + auto constexpr area() const noexcept -> Area + { + return {width.decay() * height.decay()}; + } + +private: + Width width; + Height height; +}; + +int main() +{ + auto w{0u}, h{0u}; + + std::cin >> w >> h; + + auto width = Width{w}; + auto height = Height{h}; + + auto rect = Rectangle{width, height}; + + std::cout << rect.area() << '\n'; +}
\ No newline at end of file |
