Commit 528005a1 authored by Davis King's avatar Davis King

Improved the string_assign utility.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403877
parent 6b34d376
......@@ -340,10 +340,17 @@ namespace dlib
class string_assign
{
template <
typename charT,
typename traits,
typename alloc
>
class string_assign_helper
{
public:
string_assign_helper (const std::string& str_) : str(str_) {}
string_assign_helper (
const std::basic_string<charT,traits,alloc>& str_
) : str(str_) {}
template <typename T>
operator T () const
......@@ -353,19 +360,82 @@ namespace dlib
private:
const std::string& str;
const std::basic_string<charT,traits,alloc>& str;
};
// -------------
class char_assign_helper
{
public:
char_assign_helper (
const char* str_
) : str(str_) {}
template <typename T>
operator T () const
{
return string_cast<T>(str);
}
private:
const char* str;
};
// -------------
class wchar_t_assign_helper
{
public:
wchar_t_assign_helper (
const wchar_t* str_
) : str(str_) {}
template <typename T>
operator T () const
{
return string_cast<T>(str);
}
private:
const wchar_t* str;
};
// -------------
public:
string_assign_helper operator=(
const std::string& str
template <
typename charT,
typename traits,
typename alloc
>
string_assign_helper<charT,traits,alloc> operator=(
const std::basic_string<charT,traits,alloc>& str
) const
{
return string_assign_helper(str);
return string_assign_helper<charT,traits,alloc>(str);
}
char_assign_helper operator= (
const char* str
) const
{
return char_assign_helper(str);
}
wchar_t_assign_helper operator= (
const wchar_t* str
) const
{
return wchar_t_assign_helper(str);
}
};
const string_assign sa = string_assign();
// ----------------------------------------------------------------------------------------
template <
......
......@@ -58,9 +58,15 @@ namespace dlib
After executing, val will be equal to 1234 and dval will be 3.141.
Note that you can use string_assign to assign to any type which you could
use with string_cast(). You aren't just limited to ints and doubles.
Additionally, note that there is a global instance of this object, dlib::sa.
So you never have to create a string_assign object yourself. Finally, this
object is totally stateless and threadsafe.
!*/
};
const string_assign sa = string_assign();
// ----------------------------------------------------------------------------------------
class cast_to_string_error : public error
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment