提升多边形序列化:环

 芹牵一线 发布于 2023-01-30 15:34

根据这个相关问题(Boost Polygon Serialization).我试图用Boost序列化多边形.我现在的问题是我正在尝试使用自定义X,Y,点的多边形编译示例,但编译器在编译时抛出此错误:

error: 'class boost::geometry::model::ring >' has no member named 'serialize'

就像没有定义任何序列化环的功能一样.由于环从std :: vector扩展,并且如相关问题中所述,因此没有必要为其序列化定义方法.但是编译器抱怨道.

这里我有一个关于定义多边形及其序列化的完整示例:

#include 

#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 

typedef boost::geometry::model::d2::point_xy< double > point;
typedef boost::geometry::model::ring< point > ring;
typedef boost::geometry::model::polygon< point > polygon;

namespace boost{
        namespace serialization{

                template
                inline void serialize(Archive & ar, point &point, const unsigned int file_version)
                {
                        std::cout << "Point: Serializing point" << std::endl;
                        ar & boost::serialization::make_nvp("x", point.x());
                        ar & boost::serialization::make_nvp("y", point.y());
                }

                template
                inline void serialize(Archive & ar, polygon &t, const unsigned int file_version)
                {
                        std::cout << "Polygon: Serializing outer ring" << std::endl;
                        ar & boost::serialization::make_nvp("outer", t.outer());

                        std::cout << "Polygon: Serializing inner rings" << std::endl;
                        ar & boost::serialization::make_nvp("inners", t.inners());
                }
        }
}

using namespace boost::geometry;
using namespace boost::archive;
using namespace std;

int main()
{
        polygon poly;
        append(poly, make(0.0, 0.0));
        append(poly, make(5.0, 5.0));
        append(poly, make(5.0, 0.0));
        correct(poly);

        ofstream ofs("polygon.xml");
        xml_oarchive oa(ofs);
        oa << BOOST_SERIALIZATION_NVP(poly);
}

有关如何使这个工作的任何想法?

编辑:关于多边形序列化的全功能代码

#include 
#include 

#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 

#include 
#include 

typedef boost::geometry::model::d2::point_xy< double > point;
typedef boost::geometry::model::ring< point > ring;
typedef boost::geometry::model::polygon< point > polygon;

namespace boost{
        namespace serialization{

                template
                inline void serialize(Archive & ar, point &point, const unsigned int file_version)
                {
                        std::cout << "Point: Serializing point" << std::endl;
                        ar & const_cast(point.x());
                        ar & const_cast(point.y());
                }

                template
                inline void serialize(Archive & ar, ring &ring, const unsigned int file_version)
                {
                        std::cout << "Ring: Serializing ring" << std::endl;
                        ar & static_cast& >(ring);
                }

                template
                inline void serialize(Archive & ar, polygon &t, const unsigned int file_version)
                {
                        std::cout << "Polygon: Serializing outer ring" << std::endl;
                        ar & t.outer();

                        std::cout << "Polygon: Serializing inner rings" << std::endl;
                        ar & t.inners();
                }
        }
}

using namespace boost::geometry;
using namespace boost::archive;
using namespace std;

int main()
{
        polygon poly;
        append(poly, make(0.0, 0.0));
        append(poly, make(5.0, 5.0));
        append(poly, make(5.0, 0.0));
        correct(poly);

        BOOST_FOREACH(point& p, poly.outer())
        {
                std::cout << "point " << p.x() << "," << p.y() << std::endl;
        }

        ofstream ofs("polygon.dat");
        binary_oarchive oa(ofs);
        oa << poly;
        ofs.close();

        polygon polyFromFile;
        ifstream ifs("polygon.dat");
        binary_iarchive ia(ifs);
        ia >> polyFromFile;

        BOOST_FOREACH(point& p, polyFromFile.outer())
        {
                std::cout << "point " << p.x() << "," << p.y() << std::endl;
        }
        ifs.close();
}

Diego Sevill.. 6

即使存在std:vector序列化的部分特化,这并不意味着它适用于子类,因此您必须为以下内容添加序列化方法ring:

template
inline void serialize(Archive & ar, ring &t, const unsigned int file_version)
{
    // Impl
}

那么实施中有什么?由于geometry不是为了序列化而构建的,因此您无法访问对序列化有用的类型(例如,为容器ring继承选择正确的默认实现),因此您可以以某种方式强制执行此操作.例如,这似乎有效:

template
inline void serialize(Archive & ar, ring &t, const unsigned int file_version)
{
     std::cout << "Ring: Serializing a ring" << std::endl;
     serialize(ar, static_cast< std::vector& >(t), file_version);
}

您还可以尝试编写一些基类序列化调用:

template
inline void serialize(Archive & ar, ring &t, const unsigned int file_version)
{
     std::cout << "Ring: Serializing a ring" << std::endl;
     ar & boost::serialization::make_nvp( "Base",
              boost::serialization::base_object >(t));
}

但问题是,您应该能够从内部访问该继承的类ring.事实上,它ringas 的定义中base_type,但它是私有的.如果它是公共的,你可以使用ring::base_type序列化的参数(而不是std::vector上面的裸)编写不那么糟糕的代码.

也许知道序列化库的内部,你可以"绑定"序列化机制,以便两个调用不是必需的,专门针对ring类本身的一些部分特化,但我怀疑这是可移植的.

1 个回答
  • 即使存在std:vector<T>序列化的部分特化,这并不意味着它适用于子类,因此您必须为以下内容添加序列化方法ring:

    template<class Archive>
    inline void serialize(Archive & ar, ring &t, const unsigned int file_version)
    {
        // Impl
    }
    

    那么实施中有什么?由于geometry不是为了序列化而构建的,因此您无法访问对序列化有用的类型(例如,为容器ring继承选择正确的默认实现),因此您可以以某种方式强制执行此操作.例如,这似乎有效:

    template<class Archive>
    inline void serialize(Archive & ar, ring &t, const unsigned int file_version)
    {
         std::cout << "Ring: Serializing a ring" << std::endl;
         serialize(ar, static_cast< std::vector<point>& >(t), file_version);
    }
    

    您还可以尝试编写一些基类序列化调用:

    template<class Archive>
    inline void serialize(Archive & ar, ring &t, const unsigned int file_version)
    {
         std::cout << "Ring: Serializing a ring" << std::endl;
         ar & boost::serialization::make_nvp( "Base",
                  boost::serialization::base_object<std::vector<point> >(t));
    }
    

    但问题是,您应该能够从内部访问该继承的类ring.事实上,它ringas 的定义中base_type,但它是私有的.如果它是公共的,你可以使用ring::base_type序列化的参数(而不是std::vector<point>上面的裸)编写不那么糟糕的代码.

    也许知道序列化库的内部,你可以"绑定"序列化机制,以便两个调用不是必需的,专门针对ring类本身的一些部分特化,但我怀疑这是可移植的.

    2023-01-30 15:37 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有