gpt4 book ai didi

c++ - 将 Qt 与 STL 和 Boost 混合使用 - 是否有任何桥梁可以让它变得简单?

转载 作者:IT老高 更新时间:2023-10-28 22:23:28 26 4
gpt4 key购买 nike

是否有任何桥梁可以使 Qt 与 STL 和 Boost 的混合尽可能无缝和容易?

这是 Mixing Qt and Boost 的后续内容,但没有给出具体的解决方法。

最佳答案

您需要哪些桥梁?

您可以将所有 Qt 容器类与标准算法一起使用。大多数时候我更喜欢 Qt 容器类,因为我确信它们使用写时复制习语(恒定时间操作)。 Qt 的 foreach 函数会创建容器的拷贝,因此您可以确定它是一个恒定时间操作,这很好。

如果 Qt 信号槽机制变慢,您可以切换到 boost 替代方案。Qt 信号/槽的伟大之处在于两个线程之间的信号/槽连接。

QtConcurrent 与 BOOST.Lambda 配合得很好


对于“共享”的父子关系,我使用这个辅助函数。

template <class Object>
static boost::shared_ptr<Object> makeSharedObject()
{
using namespace boost;
using namespace boost::lambda;
return boost::shared_ptr<Object>(
new Object(),
bind( &Object::deleteLater, _1 ) );
}

Boost.serialize 不支持 Qt 容器,您必须自己编写序列化函数。我希望在 Qt 流类和 Boost.archive 之间架起一座桥梁。

这是我的 QList 序列化模板,你可以找出其余的...

///\file document is based on "boost/serialization/list.hpp"

namespace boost {
namespace serialization {

//---------------------------------------------------------------------------
/// Saves a QList object to a collection
template<class Archive, class U >
inline void save(Archive &ar, const QList< U > &t, const uint /* file_version */ )
{
boost::serialization::stl::save_collection< Archive, QList<U> >(ar, t);
}

//---------------------------------------------------------------------------
/// Loads a QList object from a collection
template<class Archive, class U>
inline void load(Archive &ar, QList<U > &t, const uint /* file_version */ )
{
boost::serialization::stl::load_collection<
Archive,
QList<U>,
boost::serialization::stl::archive_input_seq<Archive, QList<U> >,
boost::serialization::stl::no_reserve_imp< QList<U> > >(ar, t);
}

//---------------------------------------------------------------------------
/// split non-intrusive serialization function member into separate
/// non intrusive save/load member functions
template<class Archive, class U >
inline void serialize(Archive &ar, QList<U> &t, const uint file_version )
{
boost::serialization::split_free( ar, t, file_version);
}

} // namespace serialization
} // namespace boost

BOOST_SERIALIZATION_COLLECTION_TRAITS(QList)

如果您希望 Boost.Bind 将 QPointer 当作普通指针(如 shared_ptr)处理:

namespace boost {

template<typename T> T * get_pointer(QPointer<T> const& qPointer)
{
return qPointer;
}
}

在需要 std::stream 的地方使用 QIODevice

namespace boost {
namespace iostreams {

class IoDeviceSource
{
public:
typedef char char_type;
typedef source_tag category;

explicit IoDeviceSource(QIODevice& source)
: m_source(source)
{
}

std::streamsize read(char* buffer, std::streamsize n)
{
return return m_source.read(buffer, n);
}
private:
QIODevice& m_source;
};

class IoDeviceSink {

public:
typedef char char_type;
typedef sink_tag category;

explicit IoDeviceSink(QIODevice& sink)
: m_sink(sink)
{
}

std::streamsize write(const char_type* buffer, std::streamsize n)
{
return m_sink.write(buffer, n);
}

private:
QIODevice &m_sink;
};

class IoDeviceDevice {

public:
typedef char char_type;
typedef seekable_device_tag category;

explicit IoDeviceDevice(QIODevice& device)
:m_device(device) {
}

std::streamsize write(const char_type *buffer, std::streamsize n)
{
return m_device.write(buffer, n);
}

std::streamsize read(char* buffer, std::streamsize n)
{
return m_device.read(buffer, n);
}

stream_offset seek(stream_offset off, std::ios_base::seekdir way)
{
using namespace std;
stream_offset next(0);

if(way==ios_base::beg)
{
next = m_device.pos();
}
else if(way==ios_base::cur)
{
next = m_device.pos() + offset;
}
else if(way==ios_base::end)
{
next = m_device.size() -1 + offset;
}
else
{
throw ios_base::failure("bad seek direction");
}

if( !m_device.seek(next) )
{
throw ios_base::failure("bad seek offset");
}
return m_device.pos();
}

private:
QIODevice &m_device;
};
}
}

示例

#include <iostream>
#include <QFile>
#include <boost/iostreams/stream.hpp>
#include "iodevicestream.h"

int main(int argc, char *argv[])
{
namespace io = boost::iostreams;

QVector<int> data;

QFile fl("temp.bin");
fl.open(QIODevice::ReadWrite);
io::stream<io::IoDeviceDevice> inoutput( fl );

std::copy(data.begin(), data.end(), std::ostream_iterator<int>(inoutput, "\n"));
inoutput.flush();
inoutput.seekg(0, std::ios_base::beg);
std::cout << inoutput;
return 0;
}

关于c++ - 将 Qt 与 STL 和 Boost 混合使用 - 是否有任何桥梁可以让它变得简单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/848269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com