summaryrefslogtreecommitdiff
path: root/Help/guide/tutorial/Step5/MathFunctions
diff options
context:
space:
mode:
Diffstat (limited to 'Help/guide/tutorial/Step5/MathFunctions')
-rw-r--r--Help/guide/tutorial/Step5/MathFunctions/CMakeLists.txt23
-rw-r--r--Help/guide/tutorial/Step5/MathFunctions/MathFunctions.cxx19
-rw-r--r--Help/guide/tutorial/Step5/MathFunctions/MathFunctions.h6
-rw-r--r--Help/guide/tutorial/Step5/MathFunctions/mysqrt.cxx8
-rw-r--r--Help/guide/tutorial/Step5/MathFunctions/mysqrt.h7
5 files changed, 56 insertions, 7 deletions
diff --git a/Help/guide/tutorial/Step5/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step5/MathFunctions/CMakeLists.txt
index 6cd88d7ec2..0c688f274d 100644
--- a/Help/guide/tutorial/Step5/MathFunctions/CMakeLists.txt
+++ b/Help/guide/tutorial/Step5/MathFunctions/CMakeLists.txt
@@ -1,13 +1,28 @@
-add_library(MathFunctions mysqrt.cxx)
+add_library(MathFunctions MathFunctions.cxx)
# state that anybody linking to us needs to include the current source dir
# to find MathFunctions.h, while we don't.
target_include_directories(MathFunctions
- INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
- )
+ INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
+ )
+
+# should we use our own math functions
+option(USE_MYMATH "Use tutorial provided math implementation" ON)
+if (USE_MYMATH)
+ target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
+
+ # library that just does sqrt
+ add_library(SqrtLibrary STATIC
+ mysqrt.cxx
+ )
+
+ # link our compiler flags interface library
+ target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)
+ target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
+endif()
# link our compiler flags interface library
-target_link_libraries(MathFunctions tutorial_compiler_flags)
+target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags)
# TODO 1: Create a variable called installable_libs that is a list of all
# libraries we want to install (e.g. MathFunctions and tutorial_compiler_flags)
diff --git a/Help/guide/tutorial/Step5/MathFunctions/MathFunctions.cxx b/Help/guide/tutorial/Step5/MathFunctions/MathFunctions.cxx
new file mode 100644
index 0000000000..dc28b4b530
--- /dev/null
+++ b/Help/guide/tutorial/Step5/MathFunctions/MathFunctions.cxx
@@ -0,0 +1,19 @@
+#include "MathFunctions.h"
+
+#include <cmath>
+
+#ifdef USE_MYMATH
+# include "mysqrt.h"
+#endif
+
+namespace mathfunctions {
+double sqrt(double x)
+{
+// which square root function should we use?
+#ifdef USE_MYMATH
+ return detail::mysqrt(x);
+#else
+ return std::sqrt(x);
+#endif
+}
+}
diff --git a/Help/guide/tutorial/Step5/MathFunctions/MathFunctions.h b/Help/guide/tutorial/Step5/MathFunctions/MathFunctions.h
index cd36bccffd..d5c2f224bf 100644
--- a/Help/guide/tutorial/Step5/MathFunctions/MathFunctions.h
+++ b/Help/guide/tutorial/Step5/MathFunctions/MathFunctions.h
@@ -1 +1,5 @@
-double mysqrt(double x);
+#pragma once
+
+namespace mathfunctions {
+double sqrt(double x);
+}
diff --git a/Help/guide/tutorial/Step5/MathFunctions/mysqrt.cxx b/Help/guide/tutorial/Step5/MathFunctions/mysqrt.cxx
index abe767d5ae..ba0ac645b5 100644
--- a/Help/guide/tutorial/Step5/MathFunctions/mysqrt.cxx
+++ b/Help/guide/tutorial/Step5/MathFunctions/mysqrt.cxx
@@ -1,7 +1,9 @@
-#include <iostream>
+#include "mysqrt.h"
-#include "MathFunctions.h"
+#include <iostream>
+namespace mathfunctions {
+namespace detail {
// a hack square root calculation using simple operations
double mysqrt(double x)
{
@@ -22,3 +24,5 @@ double mysqrt(double x)
}
return result;
}
+}
+}
diff --git a/Help/guide/tutorial/Step5/MathFunctions/mysqrt.h b/Help/guide/tutorial/Step5/MathFunctions/mysqrt.h
new file mode 100644
index 0000000000..593d41e7a6
--- /dev/null
+++ b/Help/guide/tutorial/Step5/MathFunctions/mysqrt.h
@@ -0,0 +1,7 @@
+#pragma once
+
+namespace mathfunctions {
+namespace detail {
+double mysqrt(double x);
+}
+}