Skip to content

Commit

Permalink
cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
alfC committed Mar 6, 2025
1 parent 015103c commit 831ccb6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
22 changes: 11 additions & 11 deletions include/boost/multi/array_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1851,13 +1851,13 @@ struct const_subarray : array_types<T, D, ElementPtr, Layout> {
}

template<class Archive>
auto serialize(Archive& arxiv, unsigned int version) const {
auto serialize(Archive& arxiv, unsigned int /*version*/) {
using AT = multi::archive_traits<Archive>;
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 ;});
}
Expand Down Expand Up @@ -2362,13 +2362,13 @@ class subarray : public const_subarray<T, D, ElementPtr, Layout> {
constexpr auto element_moved() && {return element_moved();}

template<class Archive>
auto serialize(Archive& arxiv, unsigned int version) {
auto serialize(Archive& arxiv, unsigned int /*version*/) {
using AT = multi::archive_traits<Archive>;
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 ;});
}
Expand Down
5 changes: 4 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -382,5 +382,8 @@ else()
endif()
add_test(NAME ${TEST_EXE} COMMAND $<TARGET_FILE:${TEST_EXE}>)
endforeach()

target_link_libraries (${TEST_EXE} PRIVATE TBB::tbb)

endif()
endif()
46 changes: 46 additions & 0 deletions test/serialization.cpp
Original file line number Diff line number Diff line change
@@ -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 <boost/core/lightweight_test.hpp>

#include <boost/multi/array.hpp> // for array, layout_t, subarray, range

#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
// for serialization of array elements (in this case strings)
#include <boost/serialization/string.hpp>

#include<fstream> // 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<class Element, multi::dimensionality_type D, class IStream>
auto array_load(IStream&& is) {
multi::array<Element, D> value;
auto&& vv = value();
input_archive{is} >> make_nvp("value", vv);
return value;
}

template<class Element, multi::dimensionality_type D, class OStream>
void array_save(OStream&& os, multi::array<Element, D> 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<std::string, 2> 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::string, 2>(std::ifstream{"file.string2D.json"});
BOOST_TEST(A == B);


return boost::report_errors();
}

0 comments on commit 831ccb6

Please sign in to comment.