/** * @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 EnvironmentMap; /** * @brief Get an environment variable as a string. * * @param name the name of the variable. * * @return A Boost::optional; Boost::none if the variable is not set, otherwise the contents of the variable. */ ACTIV_BASE_API Boost::optional 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; Boost::none if the variable is not set or cannot be converted, * otherwise the contents of the variable. */ template Boost::optional 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 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 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 inline Boost::optional Get(const String &name) { try { Boost::optional value(Get(name)); if (!value) return Boost::none; return Boost::lexical_cast(*value); } catch (const Boost::bad_lexical_cast &) { return Boost::none; } } // --------------------------------------------------------------------------------------------------------------------------------- template inline T Get(const String &name, const T &defaultValue) { Boost::optional envVar = Get(name); if (envVar) return *envVar; return defaultValue; } // --------------------------------------------------------------------------------------------------------------------------------- template inline bool Set(const String &name, const T &value, const bool shouldOverwrite) { try { return Set(name, Boost::lexical_cast(value), shouldOverwrite); } catch (const Boost::bad_lexical_cast &) { return false; } } } // namespace Environment } // namespace Activ #endif // (!defined ACTIV_BASE_ENVIRONMENT_H)