与STL和升压混合的Qt - 是否有任何桥梁,以方便? [英] Mixing Qt with STL and Boost - are there any bridges to make it easy?
问题描述
是否有任何的桥梁,使与STL混合的Qt和Boost无缝和容易尽可能?
Are there any bridges to make mixing Qt with STL and Boost as seamless and easy as possible?
这是一个后续到混合的Qt和Boost ,在没有具体的答案如何做到这一点给予。
This is a followup to Mixing Qt and Boost, where no specific answers how to accomplish this were given.
推荐答案
你需要什么桥?
您可以使用所有的Qt的容器类性病的算法。
大部分时间我preFER Qt的容器类,因为我敢肯定,他们使用copy-on-write的成语(恒定时间操作)。 Qt的的foreach函数创建因此它的漂亮,你肯定知道这是一个常数时间的操作容器的副本。
You can use all the Qt container classes with std algorithms. Most of the time I prefer the Qt container classes because I'm sure they use the copy-on-write idiom (constant time operation). Qt's foreach function creates a copy of the container so its nice that you know for sure it is a constant time operation.
如果Qt的信号槽机制是慢,你可以切换到升压选择。
有关Qt信号/槽伟大的事情是两个线程之间的信号/槽连接。
If the Qt signal slot mechanism is to slow you can switch to the boost alternative. The great thing about Qt signal/slot is the signal/slot connection between two threads.
QtConcurrent的伟大工程与BOOST.Lambda
QtConcurrent works great with BOOST.Lambda
有关共享父子关系我用这个辅助函数。
For "shared" child-parent relationship I use this helper function.
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 ) );
}
Qt容器不被支持Boost.serialize,你必须自己写的连载功能。
我喜欢Qt的流类和Boost.archive之间的桥梁。
Qt containers are not supported by Boost.serialize, you'll have to write the serialize functions yourself. I would love a bridge between the Qt streaming classes and Boost.archive.
下面是我的QList序列模板你可以计算出他们的休息......
Here is my QList serialization template you can figure out the rest of them ...
///\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的):
If you want Boost.Bind to handle QPointer as a normal pointer (like shared_ptr):
namespace boost {
template<typename T> T * get_pointer(QPointer<T> const& qPointer)
{
return qPointer;
}
}
使用的QIODevice
凡的std ::流
需要
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;
}
这篇关于与STL和升压混合的Qt - 是否有任何桥梁,以方便?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!