From 831ccb6cf3e9a95a6cf127ea984bad8906b6dcb2 Mon Sep 17 00:00:00 2001 From: Alfredo Correa Date: Thu, 6 Mar 2025 00:33:37 -0800 Subject: [PATCH] cmake --- include/boost/multi/array_ref.hpp | 22 +++++++-------- test/CMakeLists.txt | 5 +++- test/serialization.cpp | 46 +++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 test/serialization.cpp diff --git a/include/boost/multi/array_ref.hpp b/include/boost/multi/array_ref.hpp index 9f19f02df..df32fc417 100644 --- a/include/boost/multi/array_ref.hpp +++ b/include/boost/multi/array_ref.hpp @@ -1851,13 +1851,13 @@ struct const_subarray : array_types { } template - auto serialize(Archive& arxiv, unsigned int version) const { + auto serialize(Archive& arxiv, unsigned int /*version*/) { using AT = multi::archive_traits; - if(version == 0) { - std::for_each(this->begin(), this->end(), [&](reference&& item) {arxiv & AT ::make_nvp("item", std::move(item));}); - } else { - std::for_each(this->elements().begin(), this->elements().end(), [&](element& elem) {arxiv & AT ::make_nvp("elem", elem);}); - } + // if(version == 0) { + // std::for_each(this->begin(), this->end(), [&](reference&& item) {arxiv & AT ::make_nvp("item", std::move(item));}); + // } else { + std::for_each(this->elements().begin(), this->elements().end(), [&](element const& elem) {arxiv & AT ::make_nvp("elem", elem);}); + // } // std::for_each(this->begin(), this->end(), [&](auto&& item) {arxiv & cereal::make_nvp("item", item);}); // std::for_each(this->begin(), this->end(), [&](auto&& item) {arxiv & item ;}); } @@ -2362,13 +2362,13 @@ class subarray : public const_subarray { constexpr auto element_moved() && {return element_moved();} template - auto serialize(Archive& arxiv, unsigned int version) { + auto serialize(Archive& arxiv, unsigned int /*version*/) { using AT = multi::archive_traits; - if(version == 0) { - std::for_each(this->begin(), this->end(), [&](typename subarray::reference item) {arxiv & AT ::make_nvp("item", item);}); - } else { + // if(version == 0) { + // std::for_each(this->begin(), this->end(), [&](typename subarray::reference item) {arxiv & AT ::make_nvp("item", item);}); + // } else { std::for_each(this->elements().begin(), this->elements().end(), [&](typename subarray::element& elem) {arxiv & AT ::make_nvp("elem", elem);}); - } + //} // std::for_each(this->begin(), this->end(), [&](auto&& item) {arxiv & cereal::make_nvp("item", item);}); // std::for_each(this->begin(), this->end(), [&](auto&& item) {arxiv & item ;}); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a57ee1abc..d019eb5d5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -339,7 +339,7 @@ else() *.cpp ) - if((NOT Boost_FOUND) AND (NOT (CMAKE_CXX_COMPILER_ID STREQUAL "NVHPC"))) + if(NOT Boost_FOUND) message(WARNING "Cannot find Boost, Multi library will have a very minimal test. If you want to test the library install Boost.Test, for example please run:\n sudo apt install libboost-test-dev\n sudo dnf install boost-devel") add_executable(main main.cpp) @@ -382,5 +382,8 @@ else() endif() add_test(NAME ${TEST_EXE} COMMAND $) endforeach() + + target_link_libraries (${TEST_EXE} PRIVATE TBB::tbb) + endif() endif() diff --git a/test/serialization.cpp b/test/serialization.cpp new file mode 100644 index 000000000..c07cc85e4 --- /dev/null +++ b/test/serialization.cpp @@ -0,0 +1,46 @@ +// Copyright 2025 Alfredo A. Correa +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt +#include + +#include // for array, layout_t, subarray, range + +#include +#include +// for serialization of array elements (in this case strings) +#include + +#include // saving to files in example + +using input_archive = boost::archive::xml_iarchive; +using output_archive = boost::archive::xml_oarchive; + +using boost::serialization::make_nvp; + +namespace multi = boost::multi; + +template +auto array_load(IStream&& is) { + multi::array value; + auto&& vv = value(); + input_archive{is} >> make_nvp("value", vv); + return value; +} + +template +void array_save(OStream&& os, multi::array const& value) { + auto const& vv = value(); + output_archive{os} << make_nvp("value", vv); +} + +auto main() -> int { // NOLINT(readability-function-cognitive-complexity,bugprone-exception-escape) + + multi::array const A = {{"w", "x"}, {"y", "z"}}; + array_save(std::ofstream{"file.string2D.json"}, A); // use std::cout to print serialization to the screen + + auto const B = array_load(std::ifstream{"file.string2D.json"}); + BOOST_TEST(A == B); + + + return boost::report_errors(); +}