boost system
boost::system::error_code is the most basic class in Boost.System; it represents operating system-specific errors. Because operating systems typically enumerate errors, boost::system::error_code saves an error code in a variable of type int.
1. error_code
include <boost/system/error_code.hpp>
#include <iostream> using namespace boost::system; void fail(error_code &ec)
{
ec = errc::make_error_code(errc::not_supported);
} int main()
{
error_code ec;
fail(ec);
std::cout << ec.value() << std::endl;
std::cout << ec.category() << std::endl;
return ;
}
boost::system::errc::not_supported is a number and ec is an object of type boost::system::error_code, the function boost::system::errc::make_error_code() is called. This function creates an object of type boost::system::error_code with the respective error code.
value() member function returns the error code stored in the object.
Error codes of type boost::system::error_code belong to a category that can be retrieved with the member function category(). Errors created with boost::system::errc::make_error_code() automatically belong to the generic category. This is the category errors belong to if they aren’t assigned to another category explicitly.
2. creating error category
#include <boost/system/error_code.hpp>
#include <string>
#include <iostream> class application_category :
public boost::system::error_category
{
public:
const char *name() const noexcept { return "my app"; }
std::string message(int ev) const { return "error message"; }
}; application_category cat; int main()
{
boost::system::error_code ec(, cat);
std::cout << ec.value() << std::endl;
std::cout << ec.category().name() << std::endl;
return ;
}
A new error category is defined by creating a class derived from boost::system::error_category. This requires you to define various member functions. At a minimum, the member functions name() and message() must be supplied because they are defined as pure virtual member functions in boost::system::error_category. While name() returns the name of the error category, message() is used to retrieve the error description for a particular error code.
3. error_condition
#include <boost/system/error_code.hpp>
#include <iostream> using namespace boost::system; void fail(error_code &ec)
{
ec = errc::make_error_code(errc::not_supported);
} int main()
{
error_code ec;
fail(ec);
boost::system::error_condition ecnd = ec.default_error_condition();
std::cout << ecnd.value() << std::endl;
std::cout << ecnd.category().name() << std::endl;
return ;
}
boost::system::error_condition is used just like boost::system::error_code.
While the class boost::system::error_code is used for platform-dependent error codes, boost::system::error_condition is used to access platform-independent error codes. The member function default_error_condition() translates a platform-dependent error code into a platform-independent error code of type boost::system::error_condition.
boost system的更多相关文章
- boost中g++ 链接undefined reference to `boost::system::generic_category()问题
编译错误如下: g++ -std=c++11 tcp_session.cpp tcp_server.cpp test.cpp -o test -pthread/tmp/ccv4rZkD.o: In ...
- c++ boost asio库初学习
前些日子研究了一个c++的一个socket库,留下范例代码给以后自己参考. 同步server: // asio_server.cpp : コンソール アプリケーションのエントリ ポイントを定義します. ...
- 如何在多线程leader-follower模式下正确的使用boost::asio。
#include <assert.h> #include <signal.h> #include <unistd.h> #include <iostream& ...
- BOOST.Asio——Tutorial
=================================版权声明================================= 版权声明:原创文章 谢绝转载 啥说的,鄙视那些无视版权随 ...
- BOOST.Asio——Overview
=================================版权声明================================= 版权声明:原创文章 谢绝转载 啥说的,鄙视那些无视版权随 ...
- boost asio sync
Service: #include<boost/asio.hpp> #include<boost/thread.hpp> #include<iostream> #i ...
- 网络库crash以及boost asio strand dispath分析
最近在做服务器的稳定性的相关测试,服务器的网络底层使用的是boost asio,然后自己做的二次封装以更好的满足需求. 服务器昨天晚上发现crash了一次,之前测试了将近半个多月,有一次是莫名的退出了 ...
- boost asio tcp server 拆分
从官方给出的示例中对于 boost::asio::ip::tcp::acceptor 类的使用,是直接使用构造函数进行构造对象,这一种方法用来学习是一个不错的方式. 但是要用它来做项目却是不能够满足我 ...
- ros与下位机通信常用的c++ boost串口应用
一.首先移植c++ boost 库: 1. 先去 Boost官网 下载最新的Boost版本, 我下载的是boost_1_6_0版本, 解压. 2. 进入解压后目录: cd boost_1_6_0, 执 ...
随机推荐
- thread_process_action
import math import random import re import sys import threading from time import ctime, sleep from l ...
- 牛客网编程练习(华为机试在线训练)-----求int型正整数在内存中存储时1的个数
题目描述 输入一个int型的正整数,计算出该int型数据在内存中存储时1的个数. 输入描述: 输入一个整数(int类型) 输出描述: 这个数转换成2进制后,输出1的个数 示例1 输入 5 输出 2 P ...
- day30—使用Flexbox和CSS Grid实现高效布局实践
转行学开发,代码100天——2018-04-15 (今天是代码开发的第30天,但是代码记录有些滞后,尽快补上.日后要严格执行,避免学习进度和内容相对滞后.) 今天,记录“前端大全”里分享的一篇关于利用 ...
- shell脚本一一项目3
主题:批量创建100个用户并设置密码 脚本内容 user_list=$@user_file=./user.infofor USER in ${user_list};do if ! id $USER & ...
- Git - 暂存区及撤销修改
1. 暂存区 每个 Git 仓库中,都有一个隐藏目录 .git 用于存放 Git 仓库的相关信息,包括暂存区(称为 stage).自动创建的 master 分支以及指向 master 分支的 HEAD ...
- nmon内存分析
可参考: MemTotal:显示当前服务器物理内存大小,本服务器有8063180 KB≍7874 MB左右. MemFree:显示当前服务器的空闲内存大小,本服务器有5052336 KB≍4934 M ...
- mysql练习题目试水50题,附建库sql代码
如果你没试过水的话,那一题一题地每一题都敲一遍吧.不管它们对你看来有多么简单. 建库代码 部分题目答案在末尾,可用ctrl f 搜索题号. 作业练习——学生-选课 表结构 学生表: Student ...
- aspnet core in docker
1 创建一个文件夹(app), 将项目发布后的文件放入该文件夹中 并且创建Dockerfile文件 2 打开Dockerfile文件,编辑一下内容 #基于 `microsoft/dotnet:-cor ...
- [Codeforces 364D]Ghd(随机算法+gcd)
[Codeforces 364D]Ghd(随机算法) 题面 给出n个正整数,在其中选出n/2(向上取整)个数,要求这些数的最大公约数最大,求最大公约数的最大值 分析 每个数被选到的概率\(\geq \ ...
- codeforces hack
对某一题,首先你自己要先过TEST. 然后回到比赛的PROBLEM列表,把这题后面的锁锁上(锁上了就不能再提交了,所以没把握就别锁了), 然后到ROOM里面,你就可以看别人代码了,下面有HACK按钮, ...