我正在尝试使用boost序列化库将类序列化为字符串,并且在我的类中包含了几个双成员变量.
下面是我用来序列化的代码:
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>
std::stringstream ss;
boost::archive::text_oarchive oa(ss);
oa << mPoint;
Run Code Online (Sandbox Code Playgroud)
这是我的Point类中的序列方法:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
if (version > 0)
{
ar & mLatitude;
ar & mLongitude;
}
}
Run Code Online (Sandbox Code Playgroud)
当我序列化为字符串时,boost似乎没有像我期望的那样处理双字符串到字符串的转换(似乎存在舍入问题).研究一下,看起来其他人报告了相同的行为.我也理解与将double转换为字符串相关的精度相关问题,反之亦然,以及这可能导致问题.
有什么奇怪的,我不明白,虽然当我使用stringstream本身并将double重定向到流时,或者当我使用boost的lexical_cast函数从stringstream.str()返回时,这似乎不会发生一双.在发现boost有自己的序列化/反序列化类之前,我实际上已经使用stringstream和lexical_cast调用编写了自己的函数,并且它没有问题.我真的希望我不必放弃序列化库并回到之前的状态.希望只有一些设置/特征/等.我迷路了.
如何在 不添加任何其他库的情况下仅使用Asio of Boost Library?那可能吗?
在Boost中,没有带有*.a文件的名为" lib " 的文件夹?
我有一个boost :: asio的服务器工作正常,除了我正在尝试添加一个检查,没有其他任何接受同一端口上的连接.如果我创建了两个服务器,其中一个始终接受连接,但另一个不报告任何错误(两个中的哪一个接受所有连接似乎是随机的).
服务器类的相关位(它是使用具有Accepted()和typedef的基类的模板,用于创建连接类型):
MessageServer ( boost::asio::io_service &io, unsigned short port_num )
: BaseServerType ( io ), acceptor_ ( io, boost::asio::ip::tcp::endpoint ( boost::asio::ip::tcp::v4(), port_num ) ) {
Listen();
}
void Listen () {
boost::system::error_code ec;
acceptor_.listen ( boost::asio::socket_base::max_connections, ec );
if ( !ec ) {
start_accept();
} else {
// not reached even if a separate process
// is already listening to that port
connection_pointer new_connection;
BaseServerType::Accepted ( new_connection );
}
}
private:
void start_accept() {
connection_pointer new_connection ( CreateConnection …Run Code Online (Sandbox Code Playgroud) 我想返回一个boost::system::error_code指示是否可以解析主机/服务.主机/服务查找失败可能有多种原因(例如,网络连接问题或无效参数).
应该归还什么?
我正在使用Boost asio 1.48.是否可以为boost::asio::io_service同时打开的多个侦听套接字重用单个实例?
文档说它的共享安全,但我不确定这是否已经是一个肯定的答案.
我有一个类,它有一个boost::asio::io_service对象.我希望这个对象存储在一个boost::shared_ptr.
所以我的标题看起来像这样(我摆脱了任何不必要的代码,所以它不会分散注意力)
class CommandDispatcher
{
private:
boost::shared_ptr<boost::asio::io_service> m_ioservice;
public:
CommandDispatcher();
};
Run Code Online (Sandbox Code Playgroud)
当我现在创建一个对象时,CommandDispatcher我想要io_service为指针初始化一个对象.现在我不太清楚如何做到这一点.我查了两个不同的解决方案,但只有一个工作,我不太确定它是不是很好.但请亲自看看:
CommandDispatcher::CommandDispatcher()
{
m_ioservice.reset(new boost::asio::io_service); // this actually works
//m_ioservice = boost::make_shared<boost::asio::io_service>
// (new boost::asio::io_service); // this doesn't work
}
Run Code Online (Sandbox Code Playgroud)
所以reset调用正在运行,但我认为这个实际上是重新分配指针.所以使用它并没有错,但它似乎不是我最好的解决方案.make_shared我在另一个问题中找到的电话建议.但是这个对我来说不起作用(我按官方提升示例中的描述实现了它).我明白了
/usr/local/include/boost/smart_ptr/make_shared.hpp:189: error: invalid conversion from ‘boost::asio::io_service*’ to ‘size_t’
/usr/local/include/boost/smart_ptr/make_shared.hpp:189: error: initializing argument 1 of ‘boost::asio::io_service::io_service(size_t)’
我现在不太清楚如何做到这一点,这将是最好的方式(也许还有一个完整的其他选择).或者也许我做得对,但我得io_service错了.
希望这个问题还没有以这种方式出现在这里(我查了一些老问题,但似乎没有答案适合我).
我想使用boost :: program_options创建一个可执行文件,可以调用如下:
./example --nmax=0,10 # nmax is chosen randomly between 0 and 10
./example --nmax=9 # nmax is set to 9
./example # nmax is set to the default value of 10
Run Code Online (Sandbox Code Playgroud)
以最小的代码以类型安全的方式实现这一目标的最佳方法是什么?
我boost::asio::io_service用来管理一些异步TCP通信.这意味着我创造了一个boost::asio::ip::tcp::socket并给予io_service它.当我开始沟通时,它会像这样示意:
Async Resolve -> Callback -> Async Connect -> Callback -> Async Write -> Callback -> Async Read
我省略了像resolve和bind这样的部分.假设Socket已绑定到端口并且主机名已解析(因此连接意味着建立与端点的真实连接)
现在重点是我可以使用相同的io_service对象启动多个异步连接.这意味着,例如,虽然在我的io_service线程中程序是关于Async Write某些数据,主线程将Async Resolve在Socket上调用(但具有相同的io_service).这意味着我io_service现在有一些平行的工作要做 - 我想知道的是它将如何确定工作的优先顺序?
例如,它是这样的
Main Thread | io_service Thread
-------------------------+-----------------------------------------------
SocketA->Async Connect |
//Some other Stuff | SocketA->Callback from Async Connect
| SocketA->Async Write
SocketB->Async Connect |
| --> ?
Run Code Online (Sandbox Code Playgroud)
现在,在这一点上,我不得不承认我不太确定它是如何io_service工作的.在第四行中,现在有两个不同的异步函数需要执行.
是io_service能够做的Async Connect和Async Write …
我见过的提升实现了一个漂亮的帽子戏法,他们以某种方式使用()运算符重载的类boost ::系统:: ERROR_CODE的实例计算为一个布尔值
class error_code
{
...
typedef void (*unspecified_bool_type)();
static void unspecified_bool_true() {}
operator unspecified_bool_type() const // true if error
{
return m_val == 0 ? 0 : unspecified_bool_true;
}
...
}
Run Code Online (Sandbox Code Playgroud)
这导致可以检查这样的错误
...
boost::system::error_code err
some_boost_func(err);
if(err)
{
//handle error
}
....
Run Code Online (Sandbox Code Playgroud)
所以我一直在问自己..那里发生了什么?这似乎在某种程度上与函数指针的使用有关......如果我调用会发生什么,这会err评估函数本身还是函数指针?但是void (*unspecified_bool_type)();函数如何返回一个值
return m_val == 0 ? 0 : unspecified_bool_true;
Run Code Online (Sandbox Code Playgroud) 我正在摆弄c ++中的一些代码,由于某种原因我不想工作,我把它缩小到这种情况:
#include <thread>
#include <atomic>
#include <chrono>
#include <mutex>
#include <iostream>
using namespace std;
void test()
{
timed_mutex m;
m.lock();
std::cout << "Can i have the lock? " << m.try_lock() << std::endl;
std::cout << "in test(), should block for 10 seconds" << std::endl;
bool got_lock = m.try_lock_for(std::chrono::seconds(10));
std::cout << "Now i've blocked, got the lock: " << got_lock << std::endl;
m.unlock();
}
int main()
{
thread t = thread(&test);
t.join();
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
问题是test()根本不会阻塞,即使try_lock返回false.有没有我忽略的东西,或者这是gcc中的一个错误,或者我应该去哪里找出什么是错的?感谢任何建议和帮助!
我编译这个小程序是这样的:g++ -pthread …
c++ ×10
boost ×9
boost-asio ×6
c++11 ×1
include ×1
locking ×1
mutex ×1
shared-ptr ×1
sockets ×1