aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2025-02-26 11:24:59 +0100
committerFelix Morgner <felix.morgner@gmail.com>2025-02-26 11:24:59 +0100
commit440d47cae6431de3332ac934b6056a970cc1a0d7 (patch)
treeaae63811972647f6ffe8a13d440171cfc8752860 /examples
parent124d4f363a9d86b023aadec0eb0a3eb6fc1cbfdd (diff)
downloadnewtype-440d47cae6431de3332ac934b6056a970cc1a0d7.tar.xz
newtype-440d47cae6431de3332ac934b6056a970cc1a0d7.zip
build: remove conan
Diffstat (limited to 'examples')
-rw-r--r--examples/CMakeLists.txt15
-rw-r--r--examples/src/basic_usage.cpp39
-rw-r--r--examples/src/basic_usage_with_read.cpp37
-rw-r--r--examples/src/basic_usage_with_show.cpp39
4 files changed, 130 insertions, 0 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
new file mode 100644
index 0000000..73d961e
--- /dev/null
+++ b/examples/CMakeLists.txt
@@ -0,0 +1,15 @@
+file(GLOB SOURCES
+ RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
+ CONFIGURE_DEPENDS
+ "src/*.cpp"
+)
+
+foreach(EXAMPLE IN LISTS SOURCES)
+ get_filename_component(NAME "${EXAMPLE}" NAME_WE)
+ add_executable("ex_${NAME}" "${EXAMPLE}")
+ target_link_libraries("ex_${NAME}" "newtype::newtype")
+endforeach()
+
+install(DIRECTORY "src/"
+ TYPE DOC
+)
diff --git a/examples/src/basic_usage.cpp b/examples/src/basic_usage.cpp
new file mode 100644
index 0000000..35d1d2c
--- /dev/null
+++ b/examples/src/basic_usage.cpp
@@ -0,0 +1,39 @@
+#include <newtype/newtype.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..531a8e3
--- /dev/null
+++ b/examples/src/basic_usage_with_read.cpp
@@ -0,0 +1,37 @@
+#include <newtype/newtype.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..9e4d985
--- /dev/null
+++ b/examples/src/basic_usage_with_show.cpp
@@ -0,0 +1,39 @@
+#include <newtype/newtype.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