diff options
author | Nao Pross <naopross@thearcway.org> | 2019-02-22 23:10:07 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2019-02-22 23:15:47 +0100 |
commit | 50ab88d5d37e2af635997fce7b103f366594c511 (patch) | |
tree | 123585c0deabc01c96e0aa180353016844f19396 | |
parent | Undo directory structure change (diff) | |
parent | Fix typos and other minor errors (diff) | |
download | libmm-50ab88d5d37e2af635997fce7b103f366594c511.tar.gz libmm-50ab88d5d37e2af635997fce7b103f366594c511.zip |
Merge branch 'master' into matrices
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | CMakeLists.txt | 115 | ||||
-rw-r--r-- | build.ninja | 12 | ||||
-rw-r--r-- | cmake/MMConfig.cmake.in | 13 | ||||
-rw-r--r-- | include/mm (renamed from include/mm.hpp) | 0 | ||||
-rw-r--r-- | include/mmvec.hpp | 34 | ||||
-rw-r--r-- | ninja/rules.ninja | 27 | ||||
-rw-r--r-- | test/example.cpp (renamed from example.cpp) | 0 |
8 files changed, 157 insertions, 45 deletions
@@ -1,2 +1,3 @@ build .ninja* +CMakeFiles diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..aa63cd5 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,115 @@ +cmake_minimum_required(VERSION 3.10) +project( + MM + VERSION 0.1 + DESCRIPTION "MiniMath, a mathematical library that (ab)uses abstraction" + LANGUAGES CXX +) + +list(INSERT CMAKE_MODULE_PATH 0 ${CMAKE_SOURCE_DIR}/cmake) + +############################ +# build libmm +add_library(mm STATIC + ${CMAKE_CURRENT_SOURCE_DIR}/mmvec.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/mmmatrix.cpp +) + +add_library(MM::mm ALIAS mm) + +# set up headers +target_include_directories(mm + PUBLIC + $<INSTALL_INTERFACE:include> + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> + PRIVATE + ${CMAKE_CURRENT_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/include +) + +target_compile_options(mm + PRIVATE + $<$<CXX_COMPILER_ID:GNU>: + -pedantic -Wall -Wextra -Wcast-qual -Wcast-align -Wpointer-arith + -Winit-self -Wshadow -Wswitch-enum -Wredundant-decls -Wfloat-equal + -Wundef -Wvla -Wconversion -Wstrict-aliasing + > + $<$<CXX_COMPILER_ID:MSVC>:/W4> +) + +target_compile_definitions(mm + PRIVATE + $<$<CONFIG:Debug>:DEBUG> +) + +# set compiler features +# For a complete list see: +# https://cmake.org/cmake/help/latest/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html +target_compile_features(mm + PRIVATE + cxx_std_17 +) + + +############################ +# installation +include(GNUInstallDirs) +set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/MM) + +install(TARGETS mm + EXPORT mm-targets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +set_target_properties(mm PROPERTIES EXPORT_NAME MM) + + +# install public headers +install( + FILES + ${CMAKE_CURRENT_SOURCE_DIR}/include/mmvec.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/include/mm + DESTINATION + ${CMAKE_INSTALL_INCLUDEDIR}/mm +) + +# export targets +install(EXPORT mm-targets + FILE + MMTargets.cmake + NAMESPACE + MM:: + DESTINATION + ${INSTALL_CONFIGDIR} +) + +# create ConfigVersion.cmake +include(CMakePackageConfigHelpers) +write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/MMConfigVersion.cmake + VERSION ${PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion +) + +configure_package_config_file( + ${CMAKE_CURRENT_LIST_DIR}/cmake/MMConfig.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/MMConfig.cmake + INSTALL_DESTINATION ${INSTALL_CONFIGDIR} +) + +# install config, configversion +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/MMConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/MMConfigVersion.cmake + DESTINATION ${INSTALL_CONFIGDIR} +) + +############################ +# export from the build tree +export(EXPORT mm-targets + FILE ${CMAKE_CURRENT_BINARY_DIR}/MMTargets.cmake + NAMESPACE MM:: +) + +export(PACKAGE MM) diff --git a/build.ninja b/build.ninja deleted file mode 100644 index f5051b4..0000000 --- a/build.ninja +++ /dev/null @@ -1,12 +0,0 @@ -include ninja/rules.ninja - -# libmm -build build/mmvec.o: cpp mmvec.cpp -build build/mmmatrix.o: cpp mmmatrix.cpp - -build build/libmm.a: link-static build/mmvec.o build/mmmatrix.o -build build/libmm.so: link-shared build/mmvec.o build/mmmatrix.o - -# examples -build build/example.o: cpp example.cpp -build build/example: link build/example.o diff --git a/cmake/MMConfig.cmake.in b/cmake/MMConfig.cmake.in new file mode 100644 index 0000000..de91981 --- /dev/null +++ b/cmake/MMConfig.cmake.in @@ -0,0 +1,13 @@ +get_filename_component(MM_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) +include(CMakeFindDependencyMacro) + +list(APPEND CMAKE_MODULE_PATH ${MM_CMAKE_DIR}) +# add dependencies if needed +# add_dependency() +list(REMOVE_AT CMAKE_MODULE_PATH -1) + +if (NOT TARGET MM::MM) + include("${MM_CMAKE_DIR}/MMTargets.cmake") +endif() + +set(MM_LIBRARIES MM::MM) diff --git a/include/mm.hpp b/include/mm index 7e9f02b..7e9f02b 100644 --- a/include/mm.hpp +++ b/include/mm diff --git a/include/mmvec.hpp b/include/mmvec.hpp index 4bac658..db3c390 100644 --- a/include/mmvec.hpp +++ b/include/mmvec.hpp @@ -51,13 +51,22 @@ struct mm::basic_vec : public std::array<T, d> { basic_vec(); basic_vec(const std::initializer_list<T> l); + + // copyable to a vector of size n <= d template<std::size_t n> basic_vec(const basic_vec<T, n>& other); + // movable to a vector of size n <= d + template<std::size_t n> basic_vec(basic_vec<T, n>&& other); T length() const; + // copy operator= template<std::size_t n> basic_vec<T, d>& operator=(const mm::basic_vec<T, n>& other); + // move operator= + template<std::size_t n> + basic_vec<T, d>& operator=(mm::basic_vec<T, n>&& other); + template<std::size_t n> basic_vec<T, d>& operator+=(const mm::basic_vec<T, n>& other); @@ -78,9 +87,6 @@ mm::basic_vec<T, d>::basic_vec() : std::array<T, d>() { template<typename T, std::size_t d> mm::basic_vec<T, d>::basic_vec(const std::initializer_list<T> l) { - // construct with empty values - basic_vec(); - // why can't this sh*t be a constexpr with static_assert??? assert(l.size() <= d); std::copy(l.begin(), l.end(), this->begin()); @@ -89,9 +95,12 @@ mm::basic_vec<T, d>::basic_vec(const std::initializer_list<T> l) { template<typename T, std::size_t d> template<std::size_t n> mm::basic_vec<T, d>::basic_vec(const mm::basic_vec<T, n>& other) { - // construct with empty values - basic_vec(); - // uses operator= + *this = other; +} + +template<typename T, std::size_t d> +template<std::size_t n> +mm::basic_vec<T, d>::basic_vec(basic_vec<T, n>&& other) { *this = other; } @@ -122,6 +131,19 @@ mm::basic_vec<T, d>& mm::basic_vec<T, d>::operator=(const mm::basic_vec<T, n>& o template<typename T, std::size_t d> template<std::size_t n> +mm::basic_vec<T, d>& mm::basic_vec<T, d>::operator=(mm::basic_vec<T, n>&& other) { + static_assert( + d >= n, "cannot move a higher dimensional vector into a smaller one" + ); + + std::move(other.begin(), other.end(), this->begin()); + + return *this; +} + + +template<typename T, std::size_t d> +template<std::size_t n> mm::basic_vec<T, d>& mm::basic_vec<T, d>::operator+=(const mm::basic_vec<T, n>& other) { *this = *this + other; return *this; diff --git a/ninja/rules.ninja b/ninja/rules.ninja deleted file mode 100644 index 8ff3e02..0000000 --- a/ninja/rules.ninja +++ /dev/null @@ -1,27 +0,0 @@ -includes = -I include -cflags = -Wall -Werror -pedantic -fPIC -std=c++17 $includes - -libs = -lflags = $libs - -flags = -fdiagnostics-color - -rule mkdir - command = mkdir -p $out - description = creating directory $out - -rule cpp - command = g++ $flags $cflags -c $in -o $out $lflags - description = compiling $in - -rule link - command = g++ $flags $cflags -o $out $in $lflags - description = linking $out - -rule link-shared - command = g++ $flags $cflags -shared -o $out $in $lflags - description = linking shared object $out - -rule link-static - command = ar rvs $out $in - description = creating archive $out diff --git a/example.cpp b/test/example.cpp index 3a00f58..3a00f58 100644 --- a/example.cpp +++ b/test/example.cpp |