libzypp  17.14.0
LocaleGuard.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
11 #ifndef ZYPP_BASE_LOCALEGUARD_H
12 #define ZYPP_BASE_LOCALEGUARD_H
13 
14 #include <locale.h>
15 #include <string>
16 
17 #include "zypp/base/Easy.h"
18 
20 namespace zypp
21 {
28  {
31 
32  public:
34  LocaleGuard( int category_r, const std::string & value_r = "C" )
35  : _category( -1 )
36  {
37  const char * ovalue = ::setlocale( category_r, nullptr );
38  if ( ovalue && ovalue != value_r )
39  {
40  _category = category_r;
41  _value = ovalue;
42  ::setlocale( _category, value_r.c_str() );
43  }
44  }
45 
48  { restore(); }
49 
51  void restore()
52  {
53  if ( _category != -1 )
54  {
55  ::setlocale( _category, _value.c_str() );
56  _category = -1;
57  }
58  }
59 
60  private:
61  int _category;
62  std::string _value;
63  };
64 } // namespace zypp
66 #endif // ZYPP_BASE_LOCALEGUARD_H
LocaleGuard(int category_r, const std::string &value_r="C")
Ctor saving the current locale category value.
Definition: LocaleGuard.h:34
~LocaleGuard()
Dtor asserts the saved locale category value is restored.
Definition: LocaleGuard.h:47
NON_MOVABLE(LocaleGuard)
NON_COPYABLE(LocaleGuard)
std::string _value
saved category value
Definition: LocaleGuard.h:62
Temorarily change a locale category value.
Definition: LocaleGuard.h:27
int _category
saved category or -1 if no restore needed
Definition: LocaleGuard.h:61
void restore()
immediately restore the saved locale category value.
Definition: LocaleGuard.h:51
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1