159 lines
4.1 KiB
C++
159 lines
4.1 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/Environment.h
|
|
*
|
|
* @brief Environment variable functions.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_ENVIRONMENT_H)
|
|
#define ACTIV_BASE_ENVIRONMENT_H
|
|
|
|
#include "ActivMiddleware/ActivBase/External.h"
|
|
#include "ActivMiddleware/Misc/Optional.h"
|
|
#include "ActivMiddleware/CxxYZ/map.h"
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
//! Environment namespace.
|
|
namespace Environment
|
|
{
|
|
|
|
typedef CxxYZ::map<String, String> EnvironmentMap;
|
|
|
|
/**
|
|
* @brief Get an environment variable as a string.
|
|
*
|
|
* @param name the name of the variable.
|
|
*
|
|
* @return A Boost::optional<String>; Boost::none if the variable is not set, otherwise the contents of the variable.
|
|
*/
|
|
ACTIV_BASE_API Boost::optional<String> Get(const String &name);
|
|
|
|
/**
|
|
* @brief Get an environment variable as an arbitrary type.
|
|
*
|
|
* @tparam T the type to convert the variable to.
|
|
*
|
|
* @param name the name of the variable.
|
|
*
|
|
* @return A Boost::optional<T>; Boost::none if the variable is not set or cannot be converted,
|
|
* otherwise the contents of the variable.
|
|
*/
|
|
template<typename T>
|
|
Boost::optional<T> Get(const String &name);
|
|
|
|
/**
|
|
* @brief Get an environment variable as an arbitrary type.
|
|
*
|
|
* @tparam T the type to convert the variable to.
|
|
*
|
|
* @param name the name of the variable.
|
|
* @param defaultValue the value to return if the variable is not found or is invalid.
|
|
*
|
|
* @return The environment value or @a defaultValue if not found or invalid.
|
|
*/
|
|
template<typename T>
|
|
T Get(const String &name, const T &defaultValue);
|
|
|
|
#if (!defined ACTIV_PLATFORM_WASM)
|
|
|
|
/**
|
|
* @brief Get all environment variables.
|
|
*
|
|
* @return A map of keys/values for all environment variables.
|
|
*/
|
|
ACTIV_BASE_API EnvironmentMap GetAll();
|
|
|
|
#endif
|
|
|
|
/**
|
|
* @brief Set an environment variable from a String.
|
|
*
|
|
* @param name the name of the variable.
|
|
* @param value the value to set.
|
|
* @param shouldOverwrite whether to overwrite an existing value.
|
|
*
|
|
* @return true if variable is set. Note true is returned if !shouldOverwrite and the variable already exists.
|
|
*/
|
|
ACTIV_BASE_API bool Set(const String &name, const String &value, const bool shouldOverwrite);
|
|
|
|
/**
|
|
* @brief Set an environment variable.
|
|
*
|
|
* @tparam T type of the value; must be Boost::lexical_cast<>able to String.
|
|
*
|
|
* @param name the name of the variable.
|
|
* @param value the value to set.
|
|
* @param shouldOverwrite whether to overwrite an existing value.
|
|
*
|
|
* @return true if variable is set. Note true is returned if !shouldOverwrite and the variable already exists.
|
|
*/
|
|
template<typename T>
|
|
bool Set(const String &name, const T &value, const bool shouldOverwrite);
|
|
|
|
//! Remove an environment variable.
|
|
ACTIV_BASE_API bool Remove(const String &name);
|
|
|
|
/**
|
|
* @brief Replace any environment variables in a string with the value of the environment variable.
|
|
*
|
|
* @param source source string.
|
|
* @param target reference to a string to receive the expanded string.
|
|
*
|
|
* @return true if conversion succeeded.
|
|
*/
|
|
ACTIV_BASE_API bool Expand(const String &source, String &target);
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
inline Boost::optional<T> Get(const String &name)
|
|
{
|
|
try
|
|
{
|
|
Boost::optional<String> value(Get(name));
|
|
if (!value)
|
|
return Boost::none;
|
|
|
|
return Boost::lexical_cast<T>(*value);
|
|
}
|
|
catch (const Boost::bad_lexical_cast &)
|
|
{
|
|
return Boost::none;
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
inline T Get(const String &name, const T &defaultValue)
|
|
{
|
|
Boost::optional<T> envVar = Get<T>(name);
|
|
if (envVar)
|
|
return *envVar;
|
|
|
|
return defaultValue;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
inline bool Set(const String &name, const T &value, const bool shouldOverwrite)
|
|
{
|
|
try
|
|
{
|
|
return Set(name, Boost::lexical_cast<String>(value), shouldOverwrite);
|
|
}
|
|
catch (const Boost::bad_lexical_cast &)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
} // namespace Environment
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_ENVIRONMENT_H)
|