如何在运行时以最小的开销共享全局常量? [英] How to share global constants with minimum overhead at runtime?

查看:47
本文介绍了如何在运行时以最小的开销共享全局常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++ 11。我不允许使用boost等外部库。只能使用STL。

I am using C++11. I am not allowed to use external libraries like boost etc. I must use STL only.

我有很多事件,必须将其标识为字符串常量。我不允许使用枚举或整数或任何其他数据类型。例如:

I have a number of events, which must be identified as string constants. I am not allowed to use enums or ints or any other data type. For example:


event_name1

"event_name1"

event_name2

"event_name2"

some_other_event_name3

"some_other_event_name3"

a_different_event_name12

"a_different_event_name12"

然后我有一些需要使用这些字符串的类,但不知道其他类是否存在(它们之间没有任何关系)。

Then I have some classes which need to use these strings, but don't know the other classes exist (they don't have anything to do with each other).

class Panel{

    void postEvent(){
        SomeSingleton::postEvent("event_name");
    }
}

另一类::

class SomeClass{

    SomeClass(){
        SomeSingleton::listenForEvent("event_name");
    }

    void receiveEvent(){
         //This function is triggered when "event_name" occurs.
         //Do stuff
    }
}

所有这些事件是常量,用于标识正在发生的事情。

All these events are constants, and are used to identify things that are happening.

这是我尝试过的事情:

如何存储字符串常量,这些常量将被许多不同的类访问?

那里的某些人建议我提供如何解决具体问题的具体细节,所以我创建了这个新问题。

Some of the persons there suggested I provide specific details of how to solve a concrete problem, so I have created this new question.

如何将字符串存储在公共文件中,以便使用这些字符串的所有其他类都可以引用


  • 我不想在应用程序的生命周期内浪费内存或泄漏内存(它是移动设备app)

  • 编译时间对我来说并不重要,因为该项目规模不大

  • 有预计可能会发生50个不同的事件。

  • 将所有字符串保存在一个文件中,并在情况发生变化时仅编辑该文件似乎更加可维护。

  • 任何类都可以随时监听任何事件,在编译之前我不会知道

  • I do not want to waste memory or leak memory during my app's lifetime (it is a mobile app)
  • compilation times are not a big deal to me, since the project isn't so big
  • there are expected to be maybe 50 different events.
  • It seems it would be more maintainable to keep all the strings in one file, and edit only this file as and when things change.
  • Any class can listen for any event, at any time, and I won't know prior to compilation

推荐答案

最简单的方法是使用 char const * 常量,因为它更易于优化并且不使用动态分配。

The easiest way would be to use a char const* constant, as it's way more optimizable and don't use dynamic allocations.

也可以在 postEvent std :: string_view c>函数,避免动态分配。此步骤是可选的。如果您无法使用字符串视图,但仍希望避免动态分配,请参考实现的SSO最大容量,并将事件名称保持在该大小以下。

Also you can use std::string_view in the postEvent function, avoiding dynamic allocations. This step is optional. If you cannot have string views and still want to avoid dynamic allocations, then refer to your implementation's SSO max capacity and keep event names below that size.

也请考虑 nonstd :: string_view 可以作为C ++ 11库提供,并且很可能是您需要的抽象。诸如 cpp17_headers string-view-lite 仅出于此目的而存在。

Also consider that nonstd::string_view can be shipped as a C++11 library and most likely the abstraction you need. Library such as cpp17_headers and string-view-lite exist solely for that purpose.

它看起来像这样:

constexpr auto event_name1 = "event_name1";

在作为静态成员的类中,其工作方式相同:

In a class as a static member it works the same way:

struct Type {
    static constexpr auto event_name1 = "event_name1";
};

这最多会占用可执行文件的只读静态数据中的空间。

This will at most take space in the read-only static data of your executable.

这篇关于如何在运行时以最小的开销共享全局常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆