1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/Env.h
11 #ifndef ZYPP_BASE_ENV_H
12 #define ZYPP_BASE_ENV_H
18 ///////////////////////////////////////////////////////////////////
21 ///////////////////////////////////////////////////////////////////
24 ///////////////////////////////////////////////////////////////////
26 /// \brief Temporarily set/unset an environment variable
30 ScopedSet( const ScopedSet & ) = delete;
31 ScopedSet & operator=( const ScopedSet & ) = delete;
33 ScopedSet( ScopedSet && ) = default;
34 ScopedSet & operator=( ScopedSet && ) = default;
37 /** Default ctor (NOOP). */
41 /** Set \a var_r to \a val_r (unsets \a var_r if \a val_r is a \c nullptr). */
42 ScopedSet( std::string var_r, const char * val_r )
43 : _var { std::move(var_r) }
47 if ( const char * orig = ::getenv( _var.c_str() ) )
48 _val.reset( new std::string( orig ) );
53 /** Restore the original setting. */
57 setval( _val ? _val->c_str() : nullptr );
61 void setval( const char * val_r )
64 ::setenv( _var.c_str(), val_r, 1 );
66 ::unsetenv( _var.c_str() );
71 std::unique_ptr<std::string> _val;
75 ///////////////////////////////////////////////////////////////////
77 ///////////////////////////////////////////////////////////////////
78 #endif // ZYPP_BASE_ENV_H