libzypp: zypp/base/LocaleGuard.h Source File

libzypp 17.38.14

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-core/base/Easy.h>
18
20namespace zypp
21{
27 class LocaleGuard
28 {
29 NON_COPYABLE(LocaleGuard);
30 NON_MOVABLE(LocaleGuard);
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
47 ~LocaleGuard()
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
Easy.h
zypp::LocaleGuard::_category
int _category
saved category or -1 if no restore needed
Definition LocaleGuard.h:61
zypp::LocaleGuard::NON_COPYABLE
NON_COPYABLE(LocaleGuard)
zypp::LocaleGuard::LocaleGuard
LocaleGuard(int category_r, const std::string &value_r="C")
Ctor saving the current locale category value.
Definition LocaleGuard.h:34
zypp::LocaleGuard::restore
void restore()
immediately restore the saved locale category value.
Definition LocaleGuard.h:51
zypp::LocaleGuard::~LocaleGuard
~LocaleGuard()
Dtor asserts the saved locale category value is restored.
Definition LocaleGuard.h:47
zypp::LocaleGuard::NON_MOVABLE
NON_MOVABLE(LocaleGuard)
zypp::LocaleGuard::_value
std::string _value
saved category value
Definition LocaleGuard.h:62
locale.h
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2