summaryrefslogtreecommitdiffstats
path: root/Help/guide/tutorial/Step7
diff options
context:
space:
mode:
authorMarkus Ferrell <markus.ferrell@kitware.com>2023-02-10 21:07:06 (GMT)
committerMarkus Ferrell <markus.ferrell@kitware.com>2023-02-23 14:42:10 (GMT)
commit8ddf32196c6bca9c3a80414222327eebd5d51835 (patch)
tree40ad09ab3e93b4d1bbc642fc921f1ddb3737853b /Help/guide/tutorial/Step7
parent78299083d24fc09c777292effb4fdb9d9ed1d7d3 (diff)
downloadCMake-8ddf32196c6bca9c3a80414222327eebd5d51835.zip
CMake-8ddf32196c6bca9c3a80414222327eebd5d51835.tar.gz
CMake-8ddf32196c6bca9c3a80414222327eebd5d51835.tar.bz2
Tutorial: Refactor MathFunctions code
Propagate the refactor in Step 10 MathFunctions through all of the steps. Use MathFunctions/MathFunctions.cxx instead of Tutorial.cxx to determine which sqrt library is called. Adds .h files which correspond to their .cxx files by name.
Diffstat (limited to 'Help/guide/tutorial/Step7')
-rw-r--r--Help/guide/tutorial/Step7/CMakeLists.txt11
-rw-r--r--Help/guide/tutorial/Step7/MathFunctions/CMakeLists.txt46
-rw-r--r--Help/guide/tutorial/Step7/MathFunctions/MathFunctions.cxx19
-rw-r--r--Help/guide/tutorial/Step7/MathFunctions/MathFunctions.h6
-rw-r--r--Help/guide/tutorial/Step7/MathFunctions/mysqrt.cxx9
-rw-r--r--Help/guide/tutorial/Step7/MathFunctions/mysqrt.h7
-rw-r--r--Help/guide/tutorial/Step7/TutorialConfig.h.in1
-rw-r--r--Help/guide/tutorial/Step7/tutorial.cxx13
8 files changed, 68 insertions, 44 deletions
diff --git a/Help/guide/tutorial/Step7/CMakeLists.txt b/Help/guide/tutorial/Step7/CMakeLists.txt
index d26a90c..97ec6aa 100644
--- a/Help/guide/tutorial/Step7/CMakeLists.txt
+++ b/Help/guide/tutorial/Step7/CMakeLists.txt
@@ -16,22 +16,17 @@ target_compile_options(tutorial_compiler_flags INTERFACE
"$<${msvc_cxx}:$<BUILD_INTERFACE:-W3>>"
)
-# should we use our own math functions
-option(USE_MYMATH "Use tutorial provided math implementation" ON)
-
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)
# add the MathFunctions library
-if(USE_MYMATH)
- add_subdirectory(MathFunctions)
- list(APPEND EXTRA_LIBS MathFunctions)
-endif()
+add_subdirectory(MathFunctions)
# add the executable
add_executable(Tutorial tutorial.cxx)
-target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS} tutorial_compiler_flags)
+
+target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags)
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
diff --git a/Help/guide/tutorial/Step7/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step7/MathFunctions/CMakeLists.txt
index e5bdc4d..c0d1e72 100644
--- a/Help/guide/tutorial/Step7/MathFunctions/CMakeLists.txt
+++ b/Help/guide/tutorial/Step7/MathFunctions/CMakeLists.txt
@@ -1,33 +1,39 @@
-add_library(MathFunctions mysqrt.cxx)
+add_library(MathFunctions MathFunctions.cxx mysqrt.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}
+ )
-# link our compiler flags interface library
-target_link_libraries(MathFunctions tutorial_compiler_flags)
+# 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")
+
+ # TODO 1: Include CheckCXXSourceCompiles
-# TODO 1: Include CheckCXXSourceCompiles
+ # TODO 2: Use check_cxx_source_compiles with simple C++ code to verify
+ # availability of:
+ # * std::log
+ # * std::exp
+ # Store the results in HAVE_LOG and HAVE_EXP respectively.
-# TODO 2: Use check_cxx_source_compiles with simple C++ code to verify
-# availability of:
-# * std::log
-# * std::exp
-# Store the results in HAVE_LOG and HAVE_EXP respectively.
+ # Hint: Sample C++ code which uses log:
+ # #include <cmath>
+ # int main() {
+ # std::log(1.0);
+ # return 0;
+ # }
-# Hint: Sample C++ code which uses log:
-# #include <cmath>
-# int main() {
-# std::log(1.0);
-# return 0;
-# }
+ # TODO 3: Conditionally on HAVE_LOG and HAVE_EXP, add private compile
+ # definitions "HAVE_LOG" and "HAVE_EXP" to the MathFunctions target.
-# TODO 3: Conditionally on HAVE_LOG and HAVE_EXP, add private compile
-# definitions "HAVE_LOG" and "HAVE_EXP" to the MathFunctions target.
+ # Hint: Use target_compile_definitions()
+endif()
-#Hint: Use target_compile_definitions()
+# link our compiler flags interface library
+target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags)
# install libs
set(installable_libs MathFunctions tutorial_compiler_flags)
diff --git a/Help/guide/tutorial/Step7/MathFunctions/MathFunctions.cxx b/Help/guide/tutorial/Step7/MathFunctions/MathFunctions.cxx
new file mode 100644
index 0000000..dc28b4b
--- /dev/null
+++ b/Help/guide/tutorial/Step7/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/Step7/MathFunctions/MathFunctions.h b/Help/guide/tutorial/Step7/MathFunctions/MathFunctions.h
index cd36bcc..d5c2f22 100644
--- a/Help/guide/tutorial/Step7/MathFunctions/MathFunctions.h
+++ b/Help/guide/tutorial/Step7/MathFunctions/MathFunctions.h
@@ -1 +1,5 @@
-double mysqrt(double x);
+#pragma once
+
+namespace mathfunctions {
+double sqrt(double x);
+}
diff --git a/Help/guide/tutorial/Step7/MathFunctions/mysqrt.cxx b/Help/guide/tutorial/Step7/MathFunctions/mysqrt.cxx
index 3d2492a..9963cff 100644
--- a/Help/guide/tutorial/Step7/MathFunctions/mysqrt.cxx
+++ b/Help/guide/tutorial/Step7/MathFunctions/mysqrt.cxx
@@ -1,8 +1,9 @@
-#include <iostream>
+#include "mysqrt.h"
-// TODO 4: include cmath
-#include "MathFunctions.h"
+#include <iostream>
+namespace mathfunctions {
+namespace detail {
// a hack square root calculation using simple operations
double mysqrt(double x)
{
@@ -32,3 +33,5 @@ double mysqrt(double x)
return result;
}
+}
+}
diff --git a/Help/guide/tutorial/Step7/MathFunctions/mysqrt.h b/Help/guide/tutorial/Step7/MathFunctions/mysqrt.h
new file mode 100644
index 0000000..593d41e
--- /dev/null
+++ b/Help/guide/tutorial/Step7/MathFunctions/mysqrt.h
@@ -0,0 +1,7 @@
+#pragma once
+
+namespace mathfunctions {
+namespace detail {
+double mysqrt(double x);
+}
+}
diff --git a/Help/guide/tutorial/Step7/TutorialConfig.h.in b/Help/guide/tutorial/Step7/TutorialConfig.h.in
index e23f521..7e4d7fa 100644
--- a/Help/guide/tutorial/Step7/TutorialConfig.h.in
+++ b/Help/guide/tutorial/Step7/TutorialConfig.h.in
@@ -1,4 +1,3 @@
// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
-#cmakedefine USE_MYMATH
diff --git a/Help/guide/tutorial/Step7/tutorial.cxx b/Help/guide/tutorial/Step7/tutorial.cxx
index b3c6a4f..a3a2bdc 100644
--- a/Help/guide/tutorial/Step7/tutorial.cxx
+++ b/Help/guide/tutorial/Step7/tutorial.cxx
@@ -3,13 +3,9 @@
#include <iostream>
#include <string>
+#include "MathFunctions.h"
#include "TutorialConfig.h"
-// should we include the MathFunctions header?
-#ifdef USE_MYMATH
-# include "MathFunctions.h"
-#endif
-
int main(int argc, char* argv[])
{
if (argc < 2) {
@@ -23,12 +19,7 @@ int main(int argc, char* argv[])
// convert input to double
const double inputValue = std::stod(argv[1]);
- // which square root function should we use?
-#ifdef USE_MYMATH
- const double outputValue = mysqrt(inputValue);
-#else
- const double outputValue = sqrt(inputValue);
-#endif
+ const double outputValue = mathfunctions::sqrt(inputValue);
std::cout << "The square root of " << inputValue << " is " << outputValue
<< std::endl;