有什么方法可以自动生成基于 QSharedData 的结构吗?

发布于 2024-09-05 12:43:45 字数 249 浏览 6 评论 0原文

Qt 有一个内置支持,可以通过 QSharedData 和 QSharedDataPointer 创建具有集成引用计数的对象。一切都很好,但对于每个这样的对象,我需要编写大量代码:基于 QSharedData 的实现类,带有构造函数和复制构造函数,对象类本身带有每个字段的访问器方法。

对于具有 5-10 个字段的简单结构,这确实需要大量几乎相同的代码。是否有一些方法可以自动生成此类类?也许存在一些生成器,它们采用简短的描述并自动生成具有所有访问器的实现类和对象类?

Qt has a build-in supprt for creating objects with integrated reference counting via QSharedData and QSharedDataPointer. All works great, but for each such object I need to write a lot of code: QSharedData-based implementation class with constructor and copy constructor, object class itsef with accessor methods for each filed.

For a simple structures with 5-10 fields this requires really lot of near same code. Is it some ways to automate such classes generation? Maybe it's some generators exists that take a short description and automatically generates implementation class and object class with all accessors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城月光淡如水﹏ 2024-09-12 12:43:45

使用 QSharedData/Pointer 时,通常不必实现复制构造函数或运算符=。默认实现复制/分配 QSharedData 派生成员,这通常会做正确的事情(TM)。
对于公共类,您需要实现创建私有对象的 ctor,如果私有类没有在标头中声明,而是在实现中声明(这更好),则需要一个 dtor(什么也不做,唯一的一点是不是内联并在 .cpp 中定义,在私有声明之后)。
对于私有类,不需要任何方法/ctor/dtor 实现。
对于简单的基于值的类,编写 setter 当然很乏味,但如果使用普通的私有成员变量也是如此。 LOC 的开销不会随着成员数量的增加而增加。

不,据我所知,没有标准的生成器解决方案,尽管编写脚本或 emacs 宏等并不那么困难。将这些东西添加到公开可用的工具箱或 QtCreator 中可能是有意义的......

You usually don't have to implement copy ctor or operator= when using QSharedData/Pointer. The default impls copy/assign the QSharedData-derived member, which usually does the Right Thing (TM).
For the public class, you need to implement the ctor creating the private object, and if the private class is not declared in the header but in the implementation (which is better), a dtor (doing nothing, the only point is that is not inlined and defined in the .cpp, after the private declaration).
For the private class, no method/ctor/dtor implementations are necessary.
For simple value-based classes, writing setters is of course tedious, but the same is true if you use plain private member variables. The overhead in LOC doesn't grow with the number of members.

And no, there is no standard generator solution for that I know of, although writing a script or emacs macro etc. doing it is not that hard. Probably would make sense to add such things to a publicly available toolbox, or QtCreator...

樱娆 2024-09-12 12:43:45

我认为这些事情不会存在生成器,但我建议两件事:

这两个子类都有简单的示例,展示了如何实现看起来的共享性。但我无法进一步帮助你,因为我从来不需要创建自己的。

再想一想,为什么不公开所有数据字段,并使用 QSharedData 派生作为具有引用计数的类似结构的类?也许封装不太好,但如果你小心的话,应该不会发生任何问题。

I don't think generators would exist for these things, but I suggest two things:

The two subclasses have simple examples that show how to implement the shared-ness it seems. I can't help you further though, because I've never had the need to create my own.

On second thought, why not make all data fields public, and use the QSharedData derivative as a struct-like class with reference counting? Maybe not nice on encapsulation, but if you're careful, nothing wrong should happen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文