packages.biu: partially implement glaze

This commit is contained in:
2024-12-02 13:30:43 +08:00
parent 22737dedbd
commit 063d5d3f15
9 changed files with 80 additions and 6 deletions

6
flake.lock generated
View File

@@ -895,11 +895,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1732972899,
"narHash": "sha256-IAW8K/yCZCb2wa0UPHZtwjRrLlAe0hLooowjB5/qw6w=",
"lastModified": 1733112343,
"narHash": "sha256-m3lTc9Ox5dSk4qh21KYHdTM8XawO3Aw1Vo8amzQdYOs=",
"owner": "CHN-beta",
"repo": "nixpkgs",
"rev": "89c9bd0fe0d9761ed277da239758470c2dc44412",
"rev": "14db57c1be0752022482dabbc6229a7a10ccd6c5",
"type": "github"
},
"original": {

View File

@@ -27,6 +27,7 @@ find_package(HDF5 REQUIRED)
find_package(concurrencpp REQUIRED)
find_path(POCKETFFT_INCLUDE_DIR pocketfft.h REQUIRED)
find_package(yaml-cpp REQUIRED)
find_package(glaze REQUIRED)
add_library(biu src/common.cpp src/hdf5.cpp src/string.cpp)
target_include_directories(biu PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
@@ -34,7 +35,7 @@ target_include_directories(biu PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_D
${LIBBACKTRACE_INCLUDE_DIR} ${POCKETFFT_INCLUDE_DIR})
target_link_libraries(biu PUBLIC magic_enum::magic_enum fmt::fmt Boost::headers Boost::iostreams Boost::filesystem
range-v3::range-v3 Eigen3::Eigen HighFive TgBot::TgBot ${LIBBACKTRACE_LIBRARY} hdf5::hdf5 concurrencpp::concurrencpp
yaml-cpp::yaml-cpp)
yaml-cpp::yaml-cpp glaze::glaze)
target_compile_features(biu PUBLIC cxx_std_23)
target_compile_options(biu PUBLIC -Wno-gnu-string-literal-operator-template)
install(TARGETS biu EXPORT biuTargets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
@@ -86,3 +87,7 @@ target_link_libraries(test-logger PRIVATE biu)
target_compile_definitions(test-logger PRIVATE BIU_LOGGING_DEBUG BIU_LOGGING_SOURCE_ROOT="${CMAKE_CURRENT_SOURCE_DIR}")
set_property(TARGET test-logger PROPERTY CXX_STANDARD 23 CXX_STANDARD_REQUIRED ON)
add_test(NAME test-logger COMMAND test-logger)
add_executable(test-glaze test/glaze.cpp)
target_link_libraries(test-glaze PRIVATE biu)
set_property(TARGET test-glaze PROPERTY CXX_STANDARD 23 CXX_STANDARD_REQUIRED ON)
add_test(NAME test-glaze COMMAND test-glaze)

View File

@@ -14,3 +14,4 @@ find_package(HDF5 REQUIRED)
find_package(concurrencpp REQUIRED)
find_path(POCKETFFT_INCLUDE_DIR pocketfft.h REQUIRED)
find_package(yaml-cpp REQUIRED)
find_package(glaze REQUIRED)

View File

@@ -1,7 +1,7 @@
{
stdenv, cmake, lib,
magic-enum, fmt, boost, eigen, range-v3, nameof, zpp-bits, highfive, tgbot-cpp, libbacktrace, hdf5, concurrencpp,
pocketfft, yaml-cpp
pocketfft, yaml-cpp, glaze
}: stdenv.mkDerivation rec
{
name = "biu";
@@ -9,7 +9,7 @@
buildInputs =
[
magic-enum fmt boost range-v3 nameof zpp-bits eigen highfive tgbot-cpp libbacktrace hdf5
concurrencpp pocketfft yaml-cpp
concurrencpp pocketfft yaml-cpp glaze
];
propagatedBuildInputs = buildInputs;
nativeBuildInputs = [ cmake ];

View File

@@ -15,4 +15,5 @@
# include <biu/fft.tpp>
# include <biu/yaml.tpp>
# include <biu/serialize.tpp>
# include <biu/glaze.tpp>
# include <range/v3/all.hpp>

View File

@@ -0,0 +1,31 @@
# pragma once
# include <biu/eigen.hpp>
# include <glaze/glaze.hpp>
namespace glz::detail
{
template <biu::EigenMatrix Matrix> struct from<JSON, Matrix>
{
template <auto Opts> static void op(Matrix& matrix, auto&&... args);
};
template <biu::EigenMatrix Matrix> struct to<JSON, Matrix>
{
template <auto Opts> static void op(Matrix& matrix, auto&&... args) noexcept;
};
}
template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
struct glz::meta<Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>>
{
static constexpr std::string_view name = join_v
<
chars<"Eigen::Matrix<">, name_v<Scalar>, chars<",">,
chars<num_to_string<Rows>::value>, chars<",">,
chars<num_to_string<Cols>::value>, chars<",">,
chars<num_to_string<Options>::value>, chars<",">,
chars<num_to_string<MaxRows>::value>, chars<",">,
chars<num_to_string<MaxCols>::value>,
chars<">">
>;
};

View File

@@ -0,0 +1,15 @@
# pragma once
# include <biu/glaze.hpp>
template <biu::EigenMatrix Matrix> template <auto Opts> void
glz::detail::from<glz::JSON, Matrix>::op(Matrix& matrix, auto&&... args)
{
decltype(matrix | biu::fromEigen) std_matrix;
read<JSON>::op<Opts>(std_matrix, args...);
matrix = std_matrix | biu::toEigen<>;
}
template <biu::EigenMatrix Matrix> template <auto Opts> void
glz::detail::to<glz::JSON, Matrix>::op(Matrix& matrix, auto&&... args) noexcept
{
write<JSON>::op<Opts>(matrix | biu::fromEigen, args...);
}

View File

@@ -1,4 +1,5 @@
# include <biu.hpp>
# include <glaze/glaze.hpp>
int main()
{

View File

@@ -0,0 +1,20 @@
// # include <biu.hpp>
# include <glaze/glaze.hpp>
# include <Eigen/Dense>
# include <glaze/ext/eigen.hpp>
struct aaa
{
int a = 1;
double b = 2;
std::string c = "3";
Eigen::Matrix3d d = Eigen::Matrix3d::Identity();
bool operator==(const aaa&) const = default;
} bbb;
int main()
{
auto result = glz::write_json(bbb.d).value();
// auto result2 = glz::read_json<aaa>(result).value();
// assert(bbb == result2);
}