C++将 const 分配给环境变量或默认值

发布于 2024-09-27 04:29:55 字数 381 浏览 8 评论 0原文

对于使用多个环境变量的应用程序,在获取环境变量并将其放入结构体或一堆 const 时是否存在某种约定或“最佳实践”?是?显然,我想为每个环境变量回退到默认值。现在,使用以下方法似乎是一种非常混乱的方法:

char* x;
const SOME_VARIABLE;
if (NULL == (x = getenv("SOME_VARIABLE")))
    SOME_VARIABLE = 5; // default value
else
    SOME_VARIABLE = x;

我还可以编写一个包装 getenv 的函数,以在环境变量为空时返回默认值,但我不确定如果这是最好的方法的话。我也可以放弃使用 const,但这似乎也不是一件好事。

有什么想法吗?

For an application that uses a number of environment variables, is there some kind of a convention or "best practice" when it comes to grabbing environment variables and putting them into either a struct or a bunch of const's? Obviously, I want to fallback to a default value for each and every environment variable. Right now, using the following seems like a very messy way of doing it:

char* x;
const SOME_VARIABLE;
if (NULL == (x = getenv("SOME_VARIABLE")))
    SOME_VARIABLE = 5; // default value
else
    SOME_VARIABLE = x;

I could also write a function that wraps getenv to return a default value if an environment variable is empty, but I'm not sure if that's even the best way to do it. I could also do away with using const, but that doesn't seem like a good thing to do, either.

Any thoughts?

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

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

发布评论

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

评论(3

心清如水 2024-10-04 04:29:56

怎么样:

std::string GetEnvironmentVariableOrDefault(const std::string& variable_name, 
                                            const std::string& default_value)
{
    const char* value = getenv(variable_name.c_str());
    return value ? value : default_value;
}

用作:

const std::string some_variable = GetEnvironmentVariableOrDefault("SOME_VARIABLE", "5");

How about:

std::string GetEnvironmentVariableOrDefault(const std::string& variable_name, 
                                            const std::string& default_value)
{
    const char* value = getenv(variable_name.c_str());
    return value ? value : default_value;
}

Used as:

const std::string some_variable = GetEnvironmentVariableOrDefault("SOME_VARIABLE", "5");
╰ゝ天使的微笑 2024-10-04 04:29:56

James McNellis 提供了很好的答案,但这里有一个替代意见:

我认为 getenv() 返回值程序启动时的环境变量。如果程序启动后环境变量发生更改,getenv() 仍将返回旧值。基于此,另一种方法可能是使用一个类来捕获所有必需的环境变量作为属性。该类在程序开始时填充,并且仅提供(const 限定的)访问器方法。这样,每个环境变量都会恰好调用一次 getenv()。不利的一面是,班级占用了一些空间。

在不使用任何空间的替代方法中,每当需要任何环境变量的值时都会调用 getenv() 。这并不是说 getenv() 调用很昂贵(实际上我不知道),而是它给程序员带来了一个错误的假设,即可能返回最新值。

如果使用环境变量存在任何依赖性,那么拥有一个类也可能有助于抽象,例如

if $OS = "SunOs" 
then 
    GCC="/bin/gcc" 
else 
    GCC="/usr/bin/gcc" 

(this is just an example though)

,现在,一个不关心 $OS 但只需要 $GCC,可以参考envClass->get("GCC");

只是一个想法。

James McNellis has provided a great answer, but here is an alternate opinion:

I think getenv() returns the value of the environment variable at the time the program was started. If the environment variable gets changed after the program is started, getenv() would still return the old value. Based on that, an alternate approach may be to have a class to capture all the required environment variables as attributes. The class is populated at the start of the program, and provides only (const-qualified) accessor methods. That way the getenv() is called exactly once for each environment variable. On the negative side, the class takes up some space.

In the alternate approach, which does not use any space, getenv() is called whenever the value of any environment variable is required. It’s not that the getenv() call is expensive (actually I don’t’ know), but it carries a false assumption to the programmer that the latest value might be returned.

Also having a class may be useful to abstract if there is any dependency in using the environement variables, e.g.

if $OS = "SunOs" 
then 
    GCC="/bin/gcc" 
else 
    GCC="/usr/bin/gcc" 

(this is just an example though)

Now, a function who don’t care about $OS but needs only $GCC, can just refer to envClass->get("GCC");

Just a thought.

情深缘浅 2024-10-04 04:29:56

我通常使用加载配置设置(从环境或配置文件)的类来执行此操作。我在启动时实例化它的单个全局实例。
程序设置是配置类对象的方法或属性。
这很简单,当你编写代码时,你的意图就很清楚了:

if ( configuration.ExitOnFailure && IsError() )
   exit();

I usually do this with a class that loads configuration settings (either from the environment or a configuration file). I instantiate a single global instance of it at startup.
The program settings are a method or property of the configuration class object.
It's simple and when you write code it's pretty clear what you intended:

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