当前位置: 首页 > news >正文

6.4 Case Studies - A Simple Logging Archive Class

下面这段内容介绍了一个示例,目的是帮助澄清"归档概念(Archive Concept)"的用法,以便用户可以实现自己的归档类。simple_log_archive.hpp 实现了一个简单但实用的归档类,用于将任何可序列化类型以可读的格式发送到输出文本流中。使用这个归档类非常简单,示例如下:

#include "simple_log_archive.hpp"
...
// 显示完整的时间表
simple_log_archive log(std::cout);
log << schedule;

上述代码将时间表 schedule 以可读的格式输出到标准输出流,并且产生了以下输出:

schedule count 6item first driver bobhour 6minute 24second -> stops count 3item -> latitude degrees 34minutes 135seconds 52.56longitude degrees 134minutes 22seconds 78.3
...

这个示例的完整代码可以在 demo_simple_log.cpp 中找到。要更好地理解它的工作原理,可以查看 “Trivial Archive”。此外,请注意以下内容:

  • 仅包含了160行代码。
  • 仅包含头文件 - 无需链接到序列化库。
  • 能够显示所有可序列化类型。
  • 缺少一些功能。例如,它不会显示作为二进制数据序列化的信息。
  • 当给定指向多态基类的指针时,它不会显示派生类型的数据,仅显示基类的信息。
  • 如果需要添加这种功能,可以查看下一个示例。

simple_log_archive.hpp

#ifndef BOOST_SIMPLE_LOG_ARCHIVE_HPP
#define BOOST_SIMPLE_LOG_ARCHIVE_HPP// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif/1/2/3/4/5/6/7/8
// simple_log_archive.hpp// (C) Copyright 2010 Robert Ramey - http://www.rrsd.com . 
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)//  See http://www.boost.org for updates, documentation, and revision history.#include <ostream>
#include <cstddef> // std::size_t#include <boost/config.hpp>
#if defined(BOOST_NO_STDC_NAMESPACE)
namespace std{ using ::size_t; 
} // namespace std
#endif#include <boost/type_traits/is_enum.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/array.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/access.hpp>/
// log data to an output stream.  This illustrates a simpler implemenation
// of text output which is useful for getting a formatted display of
// any serializable class.  Intended to be useful as a debugging aid.
class simple_log_archive {std::ostream & m_os;unsigned int m_depth;template<class Archive>struct save_enum_type {template<class T>static void invoke(Archive &ar, const T &t){ar.m_os << static_cast<int>(t);}};template<class Archive>struct save_primitive {template<class T>static void invoke(Archive & ar, const T & t){ar.m_os << t;}};template<class Archive>struct save_only {template<class T>static void invoke(Archive & ar, const T & t){// make sure call is routed through the highest interface that might// be specialized by the user.boost::serialization::serialize_adl(ar, const_cast<T &>(t), ::boost::serialization::version< T >::value);}};template<class T>void save(const T &t){typedef BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<boost::is_enum< T >,boost::mpl::identity<save_enum_type<simple_log_archive> >,//elseBOOST_DEDUCED_TYPENAME boost::mpl::eval_if<// if its primitiveboost::mpl::equal_to<boost::serialization::implementation_level< T >,boost::mpl::int_<boost::serialization::primitive_type>>,boost::mpl::identity<save_primitive<simple_log_archive> >,// elseboost::mpl::identity<save_only<simple_log_archive> >> >::type typex;typex::invoke(*this, t);}    #ifndef BOOST_NO_STD_WSTRINGvoid save(const std::wstring &ws){m_os << "wide string types not suported in log archive";}#endifpublic:///// Implement requirements for archive concepttypedef boost::mpl::bool_<false> is_loading;typedef boost::mpl::bool_<true> is_saving;// this can be a no-op since we ignore pointer polymorphismtemplate<class T>void register_type(const T * = NULL){}unsigned int get_library_version(){return 0;}void save_binary(const void *address, std::size_t count){m_os << "save_binary not implemented";}// the << operators template<class T>simple_log_archive & operator<<(T const & t){m_os << ' ';save(t);return * this;}template<class T>simple_log_archive & operator<<(T * const t){m_os << " ->";if(NULL == t)m_os << " null";else*this << * t;return * this;}template<class T, int N>simple_log_archive & operator<<(const T (&t)[N]){return *this << boost::serialization::make_array(static_cast<const T *>(&t[0]),N);}template<class T>simple_log_archive & operator<<(const boost::serialization::nvp< T > & t){m_os << '\n'; // start line with each named object// indent according to object depthfor(unsigned int i = 0; i < m_depth; ++i)m_os << ' ';++m_depth;m_os << t.name(); // output the name of the object* this << t.const_value();--m_depth;return * this;}// the & operator template<class T>simple_log_archive & operator&(const T & t){return * this << t;}///simple_log_archive(std::ostream & os) :m_os(os),m_depth(0){}
};#endif // BOOST_SIMPLE_LOG_ARCHIVE_HPP

demo_log.cpp

/1/2/3/4/5/6/7/8
//
// demo_log.cpp
//
// (C) Copyright 2009 Robert Ramey - http://www.rrsd.com .
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)#include <iostream>
#include <cstdio>#include "demo_gps.hpp"
#include "simple_log_archive.hpp"int main(int argc, char *argv[])
{   // make the schedulebus_schedule schedule;// fill in the data// make a few stopsbus_stop *bs0 = new bus_stop_corner(gps_position(34, 135, 52.560f),gps_position(134, 22, 78.30f),"24th Street", "10th Avenue");bus_stop *bs1 = new bus_stop_corner(gps_position(35, 137, 23.456f),gps_position(133, 35, 54.12f),"State street", "Cathedral Vista Lane");bus_stop *bs2 = new bus_stop_destination(gps_position(35, 136, 15.456f),gps_position(133, 32, 15.300f),"White House");bus_stop *bs3 = new bus_stop_destination(gps_position(35, 134, 48.789f),gps_position(133, 32, 16.230f),"Lincoln Memorial");// make a  routesbus_route route0;route0.append(bs0);route0.append(bs1);route0.append(bs2);// add trips to scheduleschedule.append("bob", 6, 24, &route0);schedule.append("bob", 9, 57, &route0);schedule.append("alice", 11, 02, &route0);// make aother routesbus_route route1;route1.append(bs3);route1.append(bs2);route1.append(bs1);// add trips to scheduleschedule.append("ted", 7, 17, &route1);schedule.append("ted", 9, 38, &route1);schedule.append("alice", 11, 47, &route1);// display the complete schedulesimple_log_archive log(std::cout);log << schedule;delete bs0;delete bs1;delete bs2;delete bs3;return 0;
}

相关文章:

6.4 Case Studies - A Simple Logging Archive Class

下面这段内容介绍了一个示例&#xff0c;目的是帮助澄清"归档概念&#xff08;Archive Concept&#xff09;"的用法&#xff0c;以便用户可以实现自己的归档类。simple_log_archive.hpp 实现了一个简单但实用的归档类&#xff0c;用于将任何可序列化类型以可读的格式…...

【深度学习实验】前馈神经网络(九):整合训练、评估、预测过程(Runner)

目录 一、实验介绍 二、实验环境 1. 配置虚拟环境 2. 库版本介绍 三、实验内容 0. 导入必要的工具包 1. __init__(初始化) 2. train(训练) 3. evaluate(评估) 4. predict(预测) 5. save_model 6. load_model 7. 代码整合 一、实验介绍 二、实验环境 本系列实验使用…...

002-第一代硬件系统架构确立及产品选型

第一代硬件系统架构确立及产品选型 文章目录 第一代硬件系统架构确立及产品选型项目介绍摘要硬件架构硬件结构选型及设计单片机选型上位机选型扯点别的 关键字&#xff1a; Qt、 Qml、 信号采集机、 数据处理、 上位机 项目介绍 欢迎来到我们的 QML & C 项目&#xff…...

Go基础语法:指针和make和new

8 指针、make、new 8.1 指针&#xff08;pointer&#xff09; Go 语言中没有指针操作&#xff0c;只需要记住两个符号即可&#xff1a; & 取内存地址* 根据地址取值 package mainimport "fmt"func main() {a : 18// 获取 a 的地址值并复制给 pp : &a// …...

039_小驰私房菜_Camera perfermance debug

全网最具价值的Android Camera开发学习系列资料~ 作者:8年Android Camera开发,从Camera app一直做到Hal和驱动~ 欢迎订阅,相信能扩展你的知识面,提升个人能力~ 一、抓取trace 1. adb shell "echo vendor.debug.trace.perf=1 >> /system/build.prop" 2. …...

Caché for Windows安装及配置

本文介绍在Windows上安装Cach的操作步骤。本文假设用户熟悉Windows目录结构、实用程序和命令。本文包含如下主要部分:​​​​​​ 1)Cach安装...

代码随想录算法训练营20期|第四十六天|动态规划part08|● 139.单词拆分 ● 关于多重背包,你该了解这些! ● 背包问题总结篇!

139.单词拆分 感觉这个板块要重新刷&#xff0c;完全没有印象 class Solution {public boolean wordBreak(String s, List<String> wordDict) {Set<String> set new HashSet<>(wordDict);boolean[] dp new boolean[s.length() 1];dp[0] true;for (int i…...

系统安装(一)CentOS 7 本地安装

CentOS与Ubuntu并称为Linux最著名的两个发行版&#xff0c;但由于笔者主要从事深度学习图像算法工作&#xff0c;Ubuntu作为谷歌和多数依赖库的亲儿子占据着最高生态位。但最近接手的一个项目里&#xff0c;甲方指定需要在CentOS7上运行项目代码&#xff0c;笔者被迫小小cos了一…...

obsidian使用指南

插入代码块快捷键设置 插入代码块 用英文搜索快捷键名字 英文搜索的【Insert code block】对应的是 (6个点) 中文搜索的【代码块】对应的是 &#xff08;2个点&#xff09; 查看word、excel等非md文件设置 电脑端obsidian->设置->文件与链接->检测所有类型文件->…...

【ardunio】青少年机器人四级实操代码(2023年9月)

目录 一、题目 二、示意图 三、流程图 四、硬件连接 1、舵机 2、超声波 3、LED灯 五、程序 一、题目 实操考题(共1题&#xff0c;共100分) 1. 主题&#xff1a; 迎宾机器人 器件&#xff1a;Atmega328P主控板1块&#xff0c;舵机1个&#xff0c;超声波传感器1个&…...

MYSQL的存储过程

存储过程 存储过程是事先经过编译并存储在数据库中的一段 SQL 语句的集合&#xff0c;调用存储过程可以简化应用开发人员的很多工作&#xff0c;减少数据在数据库和应用服务器之间的传输&#xff0c;对于提高数据处理的效率是有好处的。存储过程思想上很简单&#xff0c;就是…...

[kubernetes/docker] failed to resolve reference ...:latest: not found

问题描述: pod一直pending, kubectl describe pod ... 显示: Warning Failed 9s (x3 over 63s) kubelet Failed to pull image "mathemagics/my-kube-scheduler": rpc error: code NotFound desc failed to pull and unpack image "docker…...

彻底解决win11系统0x80070032

经过各种尝试&#xff0c;终于找到原因。第一个是电脑加密软件&#xff0c;第二个是需要的部分功能没有开启&#xff0c;第三个BIOS设置。个人觉得第三个不重要。 解决方法 笔记本型号 笔记本型号是Thinkpad T14 gen2。进入BIOS的按键是按住Enter键。 1、关闭山丽防水墙服务…...

解决因为修改SELINUX配置文件出错导致Faild to load SELinux poilcy无法进入CentOS7系统的问题

一、问题 最近学习Kubernetes&#xff0c;需要设置永久关闭SELINUX,结果修改错了一个SELINUX配置参数&#xff0c;关机重新启动后导致无法进入CentOS7系统&#xff0c;卡在启动进度条界面。 二、解决 多次重启后&#xff0c;在启动日志中发现 Faild to load SELinux poilcy…...

flask中的跨域处理-方法二不使用第三方库

方法1(第三方库) pip install flask-cors from flask import Flask from flask_cors import CORSapp = Flask(__name__) CORS(app, resources={r"/api/*": {"origins": ["http://localhost:63342", "http://localhost:63345"]}})方…...

矿山定位系统-矿井人员定位系统在矿山自动化安全监控过程中的应用

一&#xff0c;矿井人员定位系统现阶段使用的必要性 1&#xff0c;煤矿开采是一项非常特殊的工作&#xff0c;现场属于非常复杂多变的环境&#xff0c;井下信号极差&#xff0c;数据传输非常不稳定&#xff0c;人员安全难以保证&#xff0c;煤矿企业一直在研究如何使用更合适的…...

JS-ECharts-前端图表 多层级联合饼图、柱状堆叠图、柱/线组合图、趋势图、自定义中线、平均线、气泡备注点

本篇博客背景为JavaScript。在ECharts在线编码快速上手&#xff0c;绘制相关前端可视化图表。 ECharts官网&#xff1a;https://echarts.apache.org/zh/index.html 其他的一些推荐&#xff1a; AntV&#xff1a;https://antv.vision/zh chartcube&#xff1a;https://chartcub…...

【eslint】屏蔽语言提醒

在 JavaScript 中&#xff0c;ESLint 是一种常用的静态代码分析工具&#xff0c;它用于检测和提醒代码中的潜在问题和风格问题。有时候&#xff0c;在某些特定情况下&#xff0c;你可能希望临时屏蔽或禁用某些 ESLint 的提醒信息&#xff0c;以便消除不必要的警告或避免不符合项…...

【python】入门第一课:了解基本语法(数据类型)

目录 一、介绍 1、什么是python&#xff1f; 2、python的几个特点 二、实例 1、注释 2、数据类型 2.1、字符串 str 2.2、整数 int 2.3、浮点数 float 2.4、布尔 bool 2.5、列表 list 2.6、元组 tuple 2.7、集合 set 2.8、字典 dict 一、介绍 1、什么是python&…...

csa从初阶到大牛(练习题2-查询)

新建2个文件d1.txt d2.txt ,使用vim打开d1.txt 输入“Hello World”字符串,将b1.txt 硬链接到b2.txt &#xff0c;查看2个文件的硬连接数 # 新建文件d1.txt和d2.txt touch d1.txt d2.txt# 使用vim编辑d1.txt并输入文本"Hello World" vim d1.txt# 创建硬链接b2.…...

挑战杯推荐项目

“人工智能”创意赛 - 智能艺术创作助手&#xff1a;借助大模型技术&#xff0c;开发能根据用户输入的主题、风格等要求&#xff0c;生成绘画、音乐、文学作品等多种形式艺术创作灵感或初稿的应用&#xff0c;帮助艺术家和创意爱好者激发创意、提高创作效率。 ​ - 个性化梦境…...

React hook之useRef

React useRef 详解 useRef 是 React 提供的一个 Hook&#xff0c;用于在函数组件中创建可变的引用对象。它在 React 开发中有多种重要用途&#xff0c;下面我将全面详细地介绍它的特性和用法。 基本概念 1. 创建 ref const refContainer useRef(initialValue);initialValu…...

【论文笔记】若干矿井粉尘检测算法概述

总的来说&#xff0c;传统机器学习、传统机器学习与深度学习的结合、LSTM等算法所需要的数据集来源于矿井传感器测量的粉尘浓度&#xff0c;通过建立回归模型来预测未来矿井的粉尘浓度。传统机器学习算法性能易受数据中极端值的影响。YOLO等计算机视觉算法所需要的数据集来源于…...

C++ 基础特性深度解析

目录 引言 一、命名空间&#xff08;namespace&#xff09; C 中的命名空间​ 与 C 语言的对比​ 二、缺省参数​ C 中的缺省参数​ 与 C 语言的对比​ 三、引用&#xff08;reference&#xff09;​ C 中的引用​ 与 C 语言的对比​ 四、inline&#xff08;内联函数…...

QT: `long long` 类型转换为 `QString` 2025.6.5

在 Qt 中&#xff0c;将 long long 类型转换为 QString 可以通过以下两种常用方法实现&#xff1a; 方法 1&#xff1a;使用 QString::number() 直接调用 QString 的静态方法 number()&#xff0c;将数值转换为字符串&#xff1a; long long value 1234567890123456789LL; …...

QT3D学习笔记——圆台、圆锥

类名作用Qt3DWindow3D渲染窗口容器QEntity场景中的实体&#xff08;对象或容器&#xff09;QCamera控制观察视角QPointLight点光源QConeMesh圆锥几何网格QTransform控制实体的位置/旋转/缩放QPhongMaterialPhong光照材质&#xff08;定义颜色、反光等&#xff09;QFirstPersonC…...

08. C#入门系列【类的基本概念】:开启编程世界的奇妙冒险

C#入门系列【类的基本概念】&#xff1a;开启编程世界的奇妙冒险 嘿&#xff0c;各位编程小白探险家&#xff01;欢迎来到 C# 的奇幻大陆&#xff01;今天咱们要深入探索这片大陆上至关重要的 “建筑”—— 类&#xff01;别害怕&#xff0c;跟着我&#xff0c;保准让你轻松搞…...

Linux部署私有文件管理系统MinIO

最近需要用到一个文件管理服务&#xff0c;但是又不想花钱&#xff0c;所以就想着自己搭建一个&#xff0c;刚好我们用的一个开源框架已经集成了MinIO&#xff0c;所以就选了这个 我这边对文件服务性能要求不是太高&#xff0c;单机版就可以 安装非常简单&#xff0c;几个命令就…...

图解JavaScript原型:原型链及其分析 | JavaScript图解

​​ 忽略该图的细节&#xff08;如内存地址值没有用二进制&#xff09; 以下是对该图进一步的理解和总结 1. JS 对象概念的辨析 对象是什么&#xff1a;保存在堆中一块区域&#xff0c;同时在栈中有一块区域保存其在堆中的地址&#xff08;也就是我们通常说的该变量指向谁&…...

车载诊断架构 --- ZEVonUDS(J1979-3)简介第一篇

我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 做到欲望极简,了解自己的真实欲望,不受外在潮流的影响,不盲从,不跟风。把自己的精力全部用在自己。一是去掉多余,凡事找规律,基础是诚信;二是…...