Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
cereal & symengine
#!/bin/bash -xe
export CMAKE_PREFIX_PATH="/opt/symengine-46090cf-dbg:$CMAKE_PREFIX_PATH"
cmake -B build/ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .
ln -fs build/compile_commands.json .
cmake --build build/
./build/serial3_struct
./build/serial3_syme
cmake_minimum_required(VERSION 3.16.2)
enable_language(CXX)
project(serial)
find_package(SymEngine REQUIRED CONFIG PATH_SUFFIXES lib/cmake/symengine)
add_executable(serial3_syme serial3_syme.cpp)
target_link_libraries(serial3_syme ${SYMENGINE_LIBRARIES})
target_include_directories(serial3_syme PRIVATE ${SYMENGINE_INCLUDE_DIRS})
set_target_properties(serial3_syme PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)
add_executable(serial3_struct serial3_struct.cpp)
set_target_properties(serial3_struct PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)
// -*- compile-command: "./build.sh" -*-
#include <iostream>
#include <vector>
#include <cereal/types/vector.hpp>
#include <cereal/archives/binary.hpp>
namespace Bar {
struct Foo {
int a;
int b;
};
}
namespace cereal {
template<typename Archive>
void save(Archive& ar, const Bar::Foo& f) {
ar(f.a, f.b);
}
template<typename Archive>
void load(Archive& ar, Bar::Foo& f) {
ar(f.a, f.b);
}
}
int main() {
Bar::Foo f1 = {.a = 3, .b = 4};
std::ostringstream oss;
cereal::BinaryOutputArchive aro{oss};
aro(f1);
auto data = oss.str();
std::istringstream iss(data);
cereal::BinaryInputArchive iar{iss};
Bar::Foo f2;
iar(f2);
if (f2.a != 3) {
throw std::logic_error("a incorrect");
}
if (f2.b != 4) {
throw std::logic_error("a incorrect");
}
return 0;
}
// -*- compile-command: "./build.sh" -*-
#include <symengine/basic.h>
#include <symengine/parser.h>
#include <cereal/archives/binary.hpp>
namespace se = SymEngine;
using se_basic = se::RCP<const se::Basic>;
namespace cereal {
template<typename Archive>
void save(Archive& ar, const se_basic& b) {
ar(b->__str__());
}
template <typename Archive> void load(Archive &ar, se_basic &b) {
std::string srepr;
ar(srepr);
b = se::parse(srepr);
}
} // namespace cereal
int main() {
se_basic x = se::parse("x + 1");
std::ostringstream oss;
cereal::BinaryOutputArchive boa{oss};
boa(x);
// logic to save to string and load from string omitted since it already fails to compile
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment