[C++] mutable的使用

文章目录

  • mutable
  • 其他
  • Reference

mutable

  • 当你定义一个类,并且在将类的实例当作参数传递过程中,需要用const Class* cls_ptr方式传递时,会存在一种情况,就是虽然cls_ptr为const,但是你还需要调用该类的方法,修改类中一些属性,来满足自己的需求;这个时候就需要将一些需要修改不受类的const修饰词影响的属性定义为mutable属性。
class Context {void set_stream(Stream stream) { stream_ = stream; }// 当Context的实例被const修饰时,只能调用`void Function const{}`的方法。否则会报错误。const Stream& stream() const { return stream_; }mutable Stream stream_;
}

源码参考:https://github.com/google/mediapipe/blob/3e9f86e580ad89519d8e7a228d247ca330d71409/mediapipe/framework/formats/tensor.h#L391

struct MtlResources;
class Tensor {class View {public:// Non-copyable.View(const View&) = delete;View& operator=(const View&) = delete;protected:explicit View(std::unique_ptr<absl::MutexLock>&& lock): lock_(std::move(lock)) {}std::unique_ptr<absl::MutexLock> lock_;};public:// No resources are allocated here.enum class ElementType {kNone,kFloat16,kFloat32,kUInt8,kInt8,kInt32,kChar,kBool};struct Shape {Shape() = default;Shape(std::initializer_list<int> dimensions) : dims(dimensions) {}Shape(const std::vector<int>& dimensions) : dims(dimensions) {}Shape(std::initializer_list<int> dimensions, bool is_dynamic): dims(dimensions), is_dynamic(is_dynamic) {}Shape(const std::vector<int>& dimensions, bool is_dynamic): dims(dimensions), is_dynamic(is_dynamic) {}int num_elements() const {return std::accumulate(dims.begin(), dims.end(), 1,std::multiplies<int>());}std::vector<int> dims;// The Tensor has dynamic rather than static shape so the TFLite interpreter// needs to be reallocated. Only relevant for CPU.bool is_dynamic = false;};// Quantization parameters corresponding to the zero_point and scale value// made available by TfLite quantized (uint8/int8) tensors.struct QuantizationParameters {QuantizationParameters() = default;QuantizationParameters(float scale, int zero_point): scale(scale), zero_point(zero_point) {}float scale = 1.0f;int zero_point = 0;};...private:friend class MtlBufferView;void Move(Tensor*);void Invalidate();ElementType element_type_;Shape shape_;QuantizationParameters quantization_parameters_;// The flags describe the current source of truth resource type.enum {kValidNone = 0,kValidCpu = 1 << 0,kValidMetalBuffer = 1 << 1,kValidOpenGlBuffer = 1 << 2,kValidOpenGlTexture2d = 1 << 3,kValidAHardwareBuffer = 1 << 5,};// A list of resource which are currently allocated and synchronized between// each-other: valid_ = kValidCpu | kValidMetalBuffer;mutable int valid_ = 0;// The mutex is locked by Get*View and is kept by all Views.mutable absl::Mutex view_mutex_;mutable void* cpu_buffer_ = nullptr;void AllocateCpuBuffer() const;// Forward declaration of the MtlResources provides compile-time verification// of ODR if this header includes any actual code that uses MtlResources.mutable std::unique_ptr<MtlResources> mtl_resources_;#ifdef MEDIAPIPE_TENSOR_USE_AHWBmutable std::shared_ptr<HardwareBuffer> ahwb_;// Allocates and pools HardwareBuffer instances. Holding the shared_ptr to the// pool ensures it outlives the internal ahwb_.std::shared_ptr<HardwareBufferPool> hardware_buffer_pool_;// Signals when GPU finished writing into SSBO so AHWB can be used then. Or// signals when writing into AHWB has been finished so GPU can read from SSBO.// Sync and FD are bound together.mutable EGLSyncKHR fence_sync_ = EGL_NO_SYNC_KHR;// This FD signals when the writing into the SSBO has been finished.mutable int ssbo_written_ = -1;// An externally set FD that is wrapped with the EGL sync then to synchronize// AHWB -> OpenGL SSBO.mutable int fence_fd_ = -1;// Reading from SSBO has been finished so SSBO can be released.mutable GLsync ssbo_read_ = 0;// An externally set function that signals when it is safe to release AHWB.// If the input parameter is 'true' then wait for the writing to be finished.mutable FinishingFunc ahwb_written_;mutable std::function<void()> release_callback_;bool AllocateAHardwareBuffer(int size_alignment = 0) const;void CreateEglSyncAndFd() const;
#endif  // MEDIAPIPE_TENSOR_USE_AHWB// Use Ahwb for other views: OpenGL / CPU buffer.mutable bool use_ahwb_ = false;mutable uint64_t ahwb_tracking_key_ = 0;// TODO: Tracks all unique tensors. Can grow to a large number. LRU// (Least Recently Used) can be more predicted.// The value contains the size alignment parameter.static inline absl::flat_hash_map<uint64_t, int> ahwb_usage_track_;// Expects the target SSBO to be already bound.bool AllocateAhwbMapToSsbo() const;bool InsertAhwbToSsboFence() const;void MoveAhwbStuff(Tensor* src);void ReleaseAhwbStuff();void* MapAhwbToCpuRead() const;void* MapAhwbToCpuWrite() const;void MoveCpuOrSsboToAhwb() const;// Set current tracking key, set "use ahwb" if the key is already marked.void TrackAhwbUsage(uint64_t key) const;#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_30mutable std::shared_ptr<mediapipe::GlContext> gl_context_;mutable GLuint opengl_texture2d_ = GL_INVALID_INDEX;mutable GLuint frame_buffer_ = GL_INVALID_INDEX;mutable int texture_width_;mutable int texture_height_;
#ifdef __EMSCRIPTEN__mutable bool texture_is_half_float_ = false;
#endif  // __EMSCRIPTEN__void AllocateOpenGlTexture2d() const;
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31mutable GLuint opengl_buffer_ = GL_INVALID_INDEX;void AllocateOpenGlBuffer() const;
#endif  // MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31bool NeedsHalfFloatRenderTarget() const;
#endif  // MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_30
};

其他

上层用非const,底层传递过程中用const修饰的 一个思考就是:在不同调用层级中会使用不同的方法对同样的属性操作,比如在上层,context是非const的,所以可以调用一些set方法,设置一些属性;当传递到下层时,我们希望他们只使用这些设置好的属性,那么我们就将context的实例修饰成const,这样他只能调用这些void Function const{}方法。

  • mutable 的使用思考, 是不是能够用于管理类的方法,控制哪些可以被const实例使用,哪些可以被非const实例使用,这样管理起来可以控制一些类的使用权限情况–方法层级的使用权限。

Reference

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/644187.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2024.1.23 GNSS 零散知识 学习笔记

1.天线种类 2.接收机 2.四大导航系统的介绍 3.卫星高度与轨道卫星种类 4.GNSS有哪些应用 5.在空间保持静⽌或匀速直线运动(⽆加速度)的坐标系称为惯性坐标系。 6.地⼼惯性坐标系实际上并没有满⾜能成为惯性坐标系的条件&#xff1a; ⾸先&#xff0c;地球及其质⼼都在围绕太阳…

[计算机提升] 切换(域)用户

4.14 切换(域)用户 4.14.1 为什么要切换用户 在Windows系统中&#xff0c;切换用户的主要目的是为了实现多用户共享同一台计算机的便利和安全。当多个人需要使用同一台计算机时&#xff0c;每个人可以登录自己的用户账户&#xff0c;这样可以避免互相干扰和混淆数据。 以下是…

《深入解析Java虚拟机:从JVM体系结构到垃圾回收算法》

文章目录 JVM体系结构JVM的组成 类加载器Class Loader类加载器的作用双亲委派机制JVM自带三个类加载器Bootstrap ClassLoader-根加载器ExtClassLoader-扩展加载器AppClassLoader-应用类加载器 Java历史-沙箱安全机制沙箱概念沙箱的作用本地代码和远程代码沙箱安全机制模型JDK1 …

sql server 修改表前 先判断是否有这个列

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME Users AND COLUMN_NAME userNum) BEGINALTER TABLE UsersADD userNum INT; END 在这个示例中&#xff1a; TABLE_NAME Users 表示我们正在检查Users这张表。COLUMN_NAME userNum 表示我们在查…

寒假刷题13天

PTA甲级 1082 Read Number in Chinese 字符串处理&#xff0c;恶心 #include<iostream> #include<algorithm>using namespace std;string num[10] {"ling" , "yi" , "er" , "san" , "si", "wu" ,…

C语言快速排序(非递归)图文详解

前言&#xff1a; 上一期分析了快速排序的三种写法&#xff0c;这三种写法有一个相同点&#xff0c;都是采用递归形式来实现的&#xff0c;那么有没有非递归的方法实现呢&#xff1f;答案是当然有&#xff0c;用非递归的方法实现快速排序&#xff0c;其实可以借助数据结构中的栈…

【LangChain学习之旅】—(10) 用RouterChain确定客户意图

【【LangChain学习之旅】—&#xff08;10&#xff09; 用RouterChain确定客户意图 任务设定整体框架具体步骤如下&#xff1a; 具体实现构建提示信息的模板构建目标链 Reference&#xff1a;LangChain 实战课 任务设定 首先&#xff0c;还是先看一下今天要完成一个什么样的任…

《鸟哥的Linux私房菜》第1章——总结与简答题回答

目录 一、什么是Linux&#xff0c;有什么作用&#xff1f; 二、Linux发展史 三、Linux发行版 四、简答题部分 一、什么是Linux&#xff0c;有什么作用&#xff1f; Linux包括内核和系统调用两部分&#xff0c;是位于硬件设备与应用程序中间的操作系统。 操作系统其实也是…

54-函数的3种定义,函数的4种调用:函数模式调用,方法模式调用,构造函数模式调用,apply call bind调用

一.函数的3种定义 1.函数的声明定义:具有声明提升 <script>//函数声明定义function fn(){}</script> 2.函数的表达式定义 <script>//匿名式表达式var fn = function(){}//命名式表达式var fn1 = function a(){}</script> 3.构造函数定义 var 变量…

宝塔Linux面板卸载方法

宝塔Linux面板卸载方法 1.你需要先在面板中将通过面板安装的所有软件卸载&#xff0c;如 nginx、mysql、php 等等&#xff0c;然后&#xff0c;进入 SSH 命令行&#xff0c;输入以下命令&#xff1a; /etc/init.d/bt stop && rm -f /etc/init.d/bt && rm -rf…

利用Facebook广告进行品牌宣传的优缺点有哪些

利用Facebook广告进行品牌宣传具有以下优点&#xff1a; 精准定位受众&#xff1a;Facebook广告能够根据用户的人口统计数据、兴趣爱好、行为特征等精准定位目标受众&#xff0c;帮助广告主更好地触达潜在客户&#xff0c;提高广告效果。多样化的广告格式&#xff1a;Facebook…

distinct和group by的功能、使用和底层原理

distinct和group by的功能、使用和底层原理 distinct功能和用法 DISTINCT 是一种用于去除 SELECT 语句返回结果中重复行的关键字。在使用 SELECT 语句查询数据时&#xff0c;如果结果集中包含重复的行&#xff0c;可以使用 SELECT DISTINCT 语句来去除这些重复的行。 需要注…

多维时序 | Matlab实现GWO-TCN-Multihead-Attention灰狼算法优化时间卷积网络结合多头注意力机制多变量时间序列预测

多维时序 | Matlab实现GWO-TCN-Multihead-Attention灰狼算法优化时间卷积网络结合多头注意力机制多变量时间序列预测 目录 多维时序 | Matlab实现GWO-TCN-Multihead-Attention灰狼算法优化时间卷积网络结合多头注意力机制多变量时间序列预测效果一览基本介绍程序设计参考资料 效…

python爬虫代码示例:爬取京东详情页图片

python爬虫代码示例:爬取京东详情页图片 一、Requests安装及示例 爬虫爬取网页内容首先要获取网页的内容&#xff0c;通过requests库进行获取。 GitHub: https://github.com/requests/requests PyPl: https://pypi.python.org/pypi/requests 官方文档:http://wwwpython-requ…

Java可以用于物联网的开发吗?

Java可以用于物联网的开发吗? 在开始前我有一些资料&#xff0c;是我根据网友给的问题精心整理了一份「Java的资料从专业入门到高级教程」&#xff0c; 点个关注在评论区回复“888”之后私信回复“888”&#xff0c;全部无偿共享给大家&#xff01;&#xff01;&#xff01;J…

web安全学习笔记【06】——http\https抓包

思维导图放最后 #知识点&#xff1a; 1、Web常规-系统&中间件&数据库&源码等 2、Web其他-前后端&软件&Docker&分配站等 3、Web拓展-CDN&WAF&OSS&反向&负载均衡等 ----------------------------------- 1、APP架构-封装&原生态&…

SpringBoot集成mybatis时idea控制台中文乱码问题解决

在application.yml中配置好映射文件打印数据库日志文件时&#xff0c;控制台出现乱码的情况解决如下 问题 在执行查询操作的时候&#xff0c;查询时可以查看是没有问题的&#xff0c;但是控制台乱码了 解决 在File-Setting-Editor-File Encodings中设置如图所示就可以了 现在…

软件测试之功能测试详解

一、测试项目启动与研读需求文档 &#xff08;一&#xff09; 组建测试团队 1、测试团队中的角色 2、测试团队的基本责任 尽早地发现软件程序、系统或产品中所有的问题。督促和协助开发人员尽快地解决程序中的缺陷。帮助项目管理人员制定合理的开发和测试计划。对缺陷进行跟…

业余爱好-生物信息学/生物化学/物理/统计学/政治/数学/概率论/AI/AGI/区块链

生物信息学 高等数学—元素和极限-实数的定义高等数学—元素和极限-实数的元素个数高等数学—元素和极限-自然数个数少于实数个数高等数学—元素和极限-无穷大之比较高等数学—元素和极限-级数的收敛高等数学—元素和极限-极限的定义数学分析与概率论人工智能AI数学基础——全套…

http503错误是什么原因

HTTP503错误在站长圈很经常遇到&#xff0c;很多网站站长经常遇到的HTTP503错误经常会不知道怎么去解决它。今天我们就来针对HTTP503错误问题展开说说。HTTP503错误是指服务器暂时无法处理客户端的请求&#xff0c;常常出现在服务器超负荷或维护期间。在这种情况下&#xff0c;…