cpp-utilities/conversion/widen.h

53 lines
1.4 KiB
C
Raw Normal View History

#ifndef CONVERSION_UTILITIES_WIDEN_H
#define CONVERSION_UTILITIES_WIDEN_H
2015-04-22 18:36:40 +02:00
2015-09-06 20:19:09 +02:00
#include "../application/global.h"
2015-04-22 18:36:40 +02:00
#include <string>
#include <vector>
#include <locale>
#include <functional>
#include <iostream>
namespace ConversionUtilities
{
/*!
2016-01-18 23:41:30 +01:00
* \brief Converts a std::string to a wide string using the specified locale.
2015-04-22 18:36:40 +02:00
*/
template<class E, class T = std::char_traits<E>, class A = std::allocator<E> >
2016-01-18 23:41:30 +01:00
class LIB_EXPORT Widen : public std::unary_function<const std::string &, std::basic_string<E, T, A> >
2015-04-22 18:36:40 +02:00
{
public:
/*!
2016-01-18 23:41:30 +01:00
* \brief Constructs a new instance with the specified \a locale.
2015-04-22 18:36:40 +02:00
*/
2016-01-18 23:41:30 +01:00
Widen(const std::locale &locale = std::locale()) :
m_loc(locale),
m_pctype(&std::use_facet<std::ctype<E> >(locale))
{}
2015-04-22 18:36:40 +02:00
Widen(const Widen &) = delete;
Widen& operator= (const Widen &) = delete;
/*!
2016-01-18 23:41:30 +01:00
* \brief Performs the conversation for the provided \a string.
2015-04-22 18:36:40 +02:00
*/
2016-01-18 23:41:30 +01:00
std::basic_string<E, T, A> operator() (const std::string &string) const
2015-04-22 18:36:40 +02:00
{
typename std::basic_string<E, T, A>::size_type srcLen = string.length();
const char *srcBeg = string.c_str();
std::vector<E> tmp(srcLen);
m_pctype->widen(srcBeg, srcBeg + srcLen, &tmp[0]);
return std::basic_string<E, T, A>(&tmp[0], srcLen);
}
private:
std::locale m_loc;
const std::ctype<E>* m_pctype;
};
}
#endif // CONVERSION_UTILITIES_WIDEN_H