cpp-utilities/conversion/widen.h

52 lines
1.5 KiB
C
Raw Normal View History

#ifndef CONVERSION_UTILITIES_WIDEN_H
#define CONVERSION_UTILITIES_WIDEN_H
#include "../global.h"
#include <functional>
#include <iostream>
2017-05-01 03:13:11 +02:00
#include <locale>
#include <string>
#include <vector>
2017-05-01 03:13:11 +02:00
namespace ConversionUtilities {
/*!
* \brief Converts a std::string to a wide string using the specified locale.
2017-02-03 00:54:44 +01:00
* \deprecated Might be removed in future release because not used anymore. Use iconv based string converion functions instead.
*/
2017-05-04 22:44:00 +02:00
template <class E, class T = std::char_traits<E>, class A = std::allocator<E>>
class CPP_UTILITIES_EXPORT Widen : public std::unary_function<const std::string &, std::basic_string<E, T, A>> {
public:
/*!
* \brief Constructs a new instance with the specified \a locale.
*/
2017-05-01 03:13:11 +02:00
Widen(const std::locale &locale = std::locale())
: m_loc(locale)
2017-05-04 22:44:00 +02:00
, m_pctype(&std::use_facet<std::ctype<E>>(locale))
2017-05-01 03:13:11 +02:00
{
}
Widen(const Widen &) = delete;
2017-05-01 03:13:11 +02:00
Widen &operator=(const Widen &) = delete;
/*!
* \brief Performs the conversation for the provided \a string.
*/
2017-05-01 03:13:11 +02:00
std::basic_string<E, T, A> operator()(const std::string &string) const
{
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;
2017-05-01 03:13:11 +02:00
const std::ctype<E> *m_pctype;
};
} // namespace ConversionUtilities
#endif // CONVERSION_UTILITIES_WIDEN_H