C++ Utilities  4.17.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
widen.h
Go to the documentation of this file.
1 #ifndef CONVERSION_UTILITIES_WIDEN_H
2 #define CONVERSION_UTILITIES_WIDEN_H
3 
4 #include "../global.h"
5 
6 #include <functional>
7 #include <iostream>
8 #include <locale>
9 #include <string>
10 #include <vector>
11 
12 namespace ConversionUtilities {
13 
18 template <class E, class T = std::char_traits<E>, class A = std::allocator<E>>
19 class CPP_UTILITIES_EXPORT Widen : public std::unary_function<const std::string &, std::basic_string<E, T, A>> {
20 public:
24  Widen(const std::locale &locale = std::locale())
25  : m_loc(locale)
26  , m_pctype(&std::use_facet<std::ctype<E>>(locale))
27  {
28  }
29 
30  Widen(const Widen &) = delete;
31  Widen &operator=(const Widen &) = delete;
32 
36  std::basic_string<E, T, A> operator()(const std::string &string) const
37  {
38  typename std::basic_string<E, T, A>::size_type srcLen = string.length();
39  const char *srcBeg = string.c_str();
40  std::vector<E> tmp(srcLen);
41  m_pctype->widen(srcBeg, srcBeg + srcLen, &tmp[0]);
42  return std::basic_string<E, T, A>(&tmp[0], srcLen);
43  }
44 
45 private:
46  std::locale m_loc;
47  const std::ctype<E> *m_pctype;
48 };
49 } // namespace ConversionUtilities
50 
51 #endif // CONVERSION_UTILITIES_WIDEN_H
#define CPP_UTILITIES_EXPORT
std::basic_string< E, T, A > operator()(const std::string &string) const
Performs the conversation for the provided string.
Definition: widen.h:36
Converts a std::string to a wide string using the specified locale.
Definition: widen.h:19
Contains several functions providing conversions between different data types.
Widen(const std::locale &locale=std::locale())
Constructs a new instance with the specified locale.
Definition: widen.h:24