我希望能够序列化 std::unique_ptr 的 STL 容器。可以吗? 顺便说一句,单个 std::unique_ptr 一切正常。 下面是我正在处理的代码,gcc 给出了以下错误:
use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const
std::unique_ptr<_Tp, _Dp>&) [with _Tp = MyDegrees; _Dp =
std::default_delete<MyDegrees>; std::unique_ptr<_Tp, _Dp> =
std::unique_ptr<MyDegrees>]’
如何使代码正常工作?
#include <iostream>
#include <memory>
#include <fstream>
#include <map>
#include <vector>
#include <boost/serialization/map.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
namespace boost {
namespace serialization {
template<class Archive, class T>
inline void save(
Archive & ar,
const std::unique_ptr< T > &t,
const unsigned int file_version
) {
// only the raw pointer has to be saved
const T * const tx = t.get();
//ar << tx;
ar << boost::serialization::make_nvp("px", tx);
}
template<class Archive, class T>
inline void load(
Archive & ar,
std::unique_ptr< T > &t,
const unsigned int file_version
) {
T *pTarget;
//ar >> pTarget;
ar >> boost::serialization::make_nvp("px", pTarget);
#if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
t.release();
t = std::unique_ptr< T >(pTarget);
#else
t.reset(pTarget);
#endif
}
template<class Archive, class T>
inline void serialize(
Archive & ar,
std::unique_ptr< T > &t,
const unsigned int file_version
) {
boost::serialization::split_free(ar, t, file_version);
}
} // namespace serialization
} // namespace boost
class MyDegrees
{
public:
void setDeg(int d) {
deg = d;
}
int getDeg()const {
return deg;
}
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
//{ ar & deg; }
{
ar & boost::serialization::make_nvp("DEGS", deg);
}
int deg;
};
class gps_position
{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
//{ ar & degrees; }
{
ar & boost::serialization::make_nvp("DEGS2", degrees);
ar & boost::serialization::make_nvp("DEGMAP", deg_map);
}
std::unique_ptr<MyDegrees> degrees;
std::vector<std::unique_ptr<MyDegrees> > deg_map;
public:
gps_position(): degrees(std::unique_ptr<MyDegrees>(new MyDegrees)) {};
void setDeg(int d) {
degrees->setDeg(d);
}
int getDeg() const {
return degrees->getDeg();
}
};
int TestBasicSerialize(int, char *[])
{
int numErr = 0;
double a;
std::ofstream ofs("filename");
gps_position g;
g.setDeg(45);
std::cout<<g.getDeg()<<std::endl;
{
boost::archive::text_oarchive oa(ofs);
oa << g;
}
//{ boost ::archive::xml_oarchive oa(ofs); oa << g;}
gps_position newg;
{
std::ifstream ifs("filename");
boost::archive::text_iarchive ia(ifs);
ia >> newg;
std::cout<<newg.getDeg()<<std::endl;
}
return numErr;
}
最佳答案
问题是容器反序列化器正在尝试复制构造一个 unique_ptr。 为了演示,请考虑以下产生相同错误的代码:
std::vector< std::unique_ptr<int> > vec;
std::unique_ptr<int> p;
vec.push_back(p); // does not compile!
但是,这可以使用 std::move 解决:
vec.push_back(std::move(p)); // ok
最省力的解决方案是使用可复制构造的智能指针,
例如 boost::shared_ptr,它带有
它在 boost/serialization/shared_ptr.hpp 中的预定义序列化实现。
下一个解决方案是在类中手动序列化 vector :
//NOTE: this replaces void serialize(...) in gps_position
template<class Archive>
void save(Archive & ar, const unsigned int version) const
{
ar & BOOST_SERIALIZATION_NVP(degrees);
size_t size = deg_map.size();
ar & BOOST_SERIALIZATION_NVP(size);
for( auto it=deg_map.begin(), end=deg_map.end(); it!=end; ++it )
ar & boost::serialization::make_nvp("item",*it);
}
template<class Archive>
void load(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(degrees);
size_t size = 0;
ar & BOOST_SERIALIZATION_NVP(size);
deg_map.clear();
deg_map.reserve(size);
while( size-- >= 0 ) {
std::unique_ptr<MyDegrees> p;
ar & boost::serialization::make_nvp("item",p);
deg_map.push_back(std::move(p));
}
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
最后一个解决方案涉及编写您自己的容器序列化程序,
就像您对 unique_ptr 所做的那样,它使用 std::move 添加项目:
namespace boost { namespace serialization {
//NOTE: do not include boost/serialization/vector.hpp
template<class Archive, class T, class Allocator>
inline void save(
Archive & ar,
const std::vector<T, Allocator> &t,
const unsigned int
){
collection_size_type count (t.size());
ar << BOOST_SERIALIZATION_NVP(count);
for(auto it=t.begin(), end=t.end(); it!=end; ++it)
ar << boost::serialization::make_nvp("item", (*it));
}
template<class Archive, class T, class Allocator>
inline void load(
Archive & ar,
std::vector<T, Allocator> &t,
const unsigned int
){
collection_size_type count;
ar >> BOOST_SERIALIZATION_NVP(count);
t.clear();
t.reserve(count);
while( count-- > 0 ) {
T i;
ar >> boost::serialization::make_nvp("item", i);
t.push_back(std::move(i)); // use std::move
}
}
template<class Archive, class T, class Allocator>
inline void serialize(
Archive & ar,
std::vector<T, Allocator> & t,
const unsigned int file_version
){
boost::serialization::split_free(ar, t, file_version);
}
} } // namespace boost::serialization
最后但同样重要的是,您应该使用:
oa << BOOST_SERIALIZATION_NVP(g);
// and
ia >> BOOST_SERIALIZATION_NVP(newg);
在使用 xml 存档时在 TestBasicSerialize() 中。
关于c++ - boost std unique_ptr 的 STL 集合的序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13347776/
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
给定一个复杂的对象层次结构,幸运的是它不包含循环引用,我如何实现支持各种格式的序列化?我不是来讨论实际实现的。相反,我正在寻找可能会派上用场的设计模式提示。更准确地说:我正在使用Ruby,我想解析XML和JSON数据以构建复杂的对象层次结构。此外,应该可以将该层次结构序列化为JSON、XML和可能的HTML。我可以为此使用Builder模式吗?在任何提到的情况下,我都有某种结构化数据-无论是在内存中还是文本中-我想用它来构建其他东西。我认为将序列化逻辑与实际业务逻辑分开会很好,这样我以后就可以轻松支持多种XML格式。 最佳答案 我最
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
//1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
假设我必须(小型到中型)阵列:tokens=["aaa","ccc","xxx","bbb","ccc","yyy","zzz"]template=["aaa","bbb","ccc"]如何确定tokens是否以相同的顺序包含template的所有条目?(请注意,在上面的示例中,应忽略第一个“ccc”,从而由于最后一个“ccc”而导致匹配。) 最佳答案 这适用于您的示例数据。tokens=["aaa","ccc","xxx","bbb","ccc","yyy","zzz"]template=["aaa","bbb","ccc"]po
我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“
我正在构建一个小部件来显示奥运会的奖牌数。我有一个“国家”对象的集合,其中每个对象都有一个“名称”属性,以及奖牌计数的“金”、“银”、“铜”。列表应该排序:1.首先是奖牌总数2.如果奖牌相同,按类型分割(金>银>铜,即2金>1金+1银)3.如果奖牌和类型相同,则按字母顺序子排序我正在用ruby做这件事,但我想语言并不重要。我确实找到了一个解决方案,但如果感觉必须有更优雅的方法来实现它。这是我做的:使用加权奖牌总数创建一个虚拟属性。因此,如果他们有2个金牌和1个银牌,加权总数将为“3.020100”。1金1银1铜为“3.010101”由于我们希望将奖牌数排序为最高的,因此列表按降序排
首先,我使用的是rails3.1.3和来自master的carrierwavegithub仓库的分支。我使用after_init钩子(Hook)来确定基于属性的字段页面模型实例并为这些字段定义属性访问器将值存储在序列化哈希中(希望它清楚我是什么谈论)。这是我正在做的事情的精简版:classPage省略mount_uploader命令让我可以访问我想要的属性。但是当我安装uploader时出现错误消息说“nil类的未定义新方法”我在源代码中读到有方法read_uploader和扩展模块中的write_uploader。我如何必须覆盖这些来制作mount_uploader命令使用我的“虚拟
有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=