如何在C ++ Actor Framework中使用Eigen :: Matrix4d作为消息类型?

问题描述

我想使用Eigen :: Matrix4d类作为CAF中的消息。但我似乎无法为此编写出色的Inspector。

错误如下:

usr/local/include/caf/write_inspector.hpp:133:7: error: static assertion Failed: 
T is neither inspectable nor default-applicable

我尝试过按元素传递Matrix4d元素的内容,并尝试使用Boost(boost_serialization_eigen.h)做一些更复杂的方法,但我一直遇到相同的错误

#include <iostream> 
#include <caf/all.hpp>
#include <Eigen/Core>
#include <Eigen/Geometry>
using namespace caf;
using namespace std;
using namespace Eigen;


CAF_BEGIN_TYPE_ID_BLOCK(custom_types,first_custom_type_id)
  CAF_ADD_TYPE_ID(custom_types,(Matrix4d))
CAF_END_TYPE_ID_BLOCK(custom_types)

#include <iostream>

template <class Inspector> 
typename Inspector::result_type inspect(Inspector& f,Matrix4d& m) {
  return f(m.data());
}


void caf_main(actor_system& system) {
    Eigen::Matrix4d Trans; // Your Transformation Matrix
    Trans.setIdentity();   // Set to Identity to make bottom row of Matrix 0,1
    Trans(0,0) = 42;
    std::cout << Trans << endl;
    // Spawn the actor
}

// creates a main function for us that calls our caf_main
CAF_MAIN(id_block::custom_types)

我意识到这可能是一个广泛的问题,但是任何正确方向的指点都值得赞赏。

解决方法

(假设CAF为0.17)

检查DSL似乎并没有很好地涵盖此特殊情况。目前,CAF 0.17的最佳解决方案可能是专门研究data_processor并调用consume_range

template <class Derived>
auto inspect(data_processor<Derived>& f,Matrix4d& x) {
  auto range = make_span(x.data(),x.size());
  return f.consume_range(range);
}

这不适用于CAF的自动字符串转换,但是您可以根据需要提供to_string重载。