locale_context.h 1.11 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*!
 * Copyright (c) 2020 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
#ifndef LIGHTGBM_LOCALE_CONTEXT_H_
#define LIGHTGBM_LOCALE_CONTEXT_H_

#include <clocale>
#include <locale>

/*!
 * Class to override the program global locale during this object lifetime.
 * After the object is destroyed, the locale is returned to its original state.
 *
 * @warn This is not thread-safe.
 */
class LocaleContext {
 public:
  /*!
   * Override the current program global locale during this object lifetime.
   *
   * @param target_locale override the locale to this locale setting.
   * @warn This is not thread-safe.
   * @note This doesn't override cout, cerr, etc.
   */
  explicit LocaleContext(const char* target_locale = "C") {
    std::locale::global(std::locale(target_locale));
  }

  /*!
   * Restores the old global locale.
   */
  ~LocaleContext() {
    std::locale::global(_saved_global_locale);
  }
 private:
  std::locale _saved_global_locale;  //!< Stores global locale at initialization.
};

#endif  // LIGHTGBM_LOCALE_CONTEXT_H_