C++ Utilities  4.6.1
Common C++ classes and routines used by my applications 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 <string>
7 #include <vector>
8 #include <locale>
9 #include <functional>
10 #include <iostream>
11 
12 namespace ConversionUtilities
13 {
14 
19 template<class E, class T = std::char_traits<E>, class A = std::allocator<E> >
20 class CPP_UTILITIES_EXPORT Widen : public std::unary_function<const std::string &, std::basic_string<E, T, A> >
21 {
22 public:
26  Widen(const std::locale &locale = std::locale()) :
27  m_loc(locale),
28  m_pctype(&std::use_facet<std::ctype<E> >(locale))
29  {}
30 
31  Widen(const Widen &) = delete;
32  Widen& operator= (const Widen &) = delete;
33 
37  std::basic_string<E, T, A> operator() (const std::string &string) const
38  {
39  typename std::basic_string<E, T, A>::size_type srcLen = string.length();
40  const char *srcBeg = string.c_str();
41  std::vector<E> tmp(srcLen);
42  m_pctype->widen(srcBeg, srcBeg + srcLen, &tmp[0]);
43  return std::basic_string<E, T, A>(&tmp[0], srcLen);
44  }
45 
46 private:
47  std::locale m_loc;
48  const std::ctype<E>* m_pctype;
49 };
50 
51 }
52 
53 #endif // CONVERSION_UTILITIES_WIDEN_H
STL namespace.
Converts a std::string to a wide string using the specified locale.
Definition: widen.h:20
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:26
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.