📄 resource_string.hpp
字号:
}
/// Constructs an around the string loaded from the given \c id and \c hinst
basic_resource_string(HINSTANCE hinst, ws_int_t id) stlsoft_throw_1(ss_typename_type_k exception_policy_type::thrown_type)
{
this->load_(hinst, id, NULL);
}
/// Constructs an around the string loaded from the given \c id, or uses the given default
ss_explicit_k basic_resource_string(ws_int_t id, value_type const* defaultValue)
{
this->load_(::GetModuleHandle(NULL), id, defaultValue);
}
/// Constructs an around the string loaded from the given \c id and \c hinst
basic_resource_string(HINSTANCE hinst, ws_int_t id, value_type const* defaultValue)
{
this->load_(hinst, id, defaultValue);
}
/// Copy constructor
basic_resource_string(class_type const& rhs)
: parent_class_type(rhs)
{}
/// Copy constructor
basic_resource_string(string_type const& rhs)
: parent_class_type(rhs)
{}
/// Copy assignment operator
class_type& operator =(class_type const& rhs)
{
parent_class_type::operator =(rhs);
return *this;
}
/// Copy assignment operator
class_type& operator =(string_type const& rhs)
{
parent_class_type::operator =(rhs);
return *this;
}
/// @}
// Implementation
private:
ws_int_t load_string_(HINSTANCE hinst, int uID, ws_char_a_t *buffer, ws_size_t cchBuffer)
{
return ::LoadStringA(hinst, static_cast<UINT>(uID), buffer, static_cast<int>(cchBuffer));
}
ws_int_t load_string_(HINSTANCE hinst, int uID, ws_char_w_t *buffer, ws_size_t cchBuffer)
{
if(::GetVersion() & 0x80000000)
{
// This block of code kindly provided by Ryan Ginstrom
int block = (uID >> 4) + 1; // Compute block number.
int num = uID & 0xf; // Compute offset into block.
HRSRC hRC = ::FindResourceEx( hinst
, RT_STRING
, MAKEINTRESOURCE(block)
, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
if(NULL != hRC)
{
HGLOBAL hgl = ::LoadResource(hinst, hRC);
if(NULL != hgl)
{
LPWSTR res_str = (LPWSTR)::LockResource(hgl);
if(NULL != res_str)
{
for(int i = 0; i < num; ++i)
{
res_str += *res_str + 1;
}
const LPCWSTR ptr = res_str + 1;
const ws_size_t cch = static_cast<ws_size_t>(*res_str);
if(cch < cchBuffer)
{
cchBuffer = cch + 1; // This is +1, since lstrcpyn 'uses' a character for the nul character
buffer[cch] = L'\0';
}
::lstrcpynW(buffer, ptr, static_cast<int>(cchBuffer));
return static_cast<ws_int_t>(cchBuffer);
}
}
}
return 0;
}
return ::LoadStringW(hinst, static_cast<UINT>(uID), buffer, static_cast<int>(cchBuffer));
}
void load_(HINSTANCE hinst, ws_int_t id, value_type const* defaultValue) stlsoft_throw_1(ss_typename_type_k exception_policy_type::thrown_type)
{
// TODO: Verify that it's not possible to load string resources of >256. If that's
// wrong, then need to fix this to use auto_buffer
value_type sz[1024];
if(0 == this->load_string_(hinst, id, sz, STLSOFT_NUM_ELEMENTS(sz)))
{
if(NULL != defaultValue)
{
parent_class_type::operator =(defaultValue);
}
else
{
exception_policy_type()("string did not load", ::GetLastError(), MAKEINTRESOURCE(id), RT_STRING);
parent_class_type::operator =(string_type());
}
}
else
{
parent_class_type::operator =(sz);
}
}
};
/* /////////////////////////////////////////////////////////////////////////
* Creator Functions
*/
//inline make_resource_string
////////////////////////////////////////////////////////////////////////////
// Unit-testing
#ifdef STLSOFT_UNITTEST
# include "./unittest/resource_string_unittest_.h"
#endif /* STLSOFT_UNITTEST */
/* /////////////////////////////////////////////////////////////////////////
* String access shims
*/
#if 0
/* c_str_ptr_null */
/** \brief Returns the corresponding C-string pointer of \c s, or a null pointer
*
* \ingroup group__library__string
*/
template< ss_typename_param_k S
, ss_typename_param_k X
>
inline C const* c_str_ptr_null(basic_resource_string<S, X> const& s)
{
return (s.length() == 0) ? 0 : s.c_str();
}
/* c_str_ptr */
/** \brief Returns the corresponding C-string pointer of \c s
*
* \ingroup group__library__string
*/
template< ss_typename_param_k S
, ss_typename_param_k X
>
inline C const* c_str_ptr(basic_resource_string<S, X> const& s)
{
return s.c_str();
}
/* c_str_ptr */
/** \brief Returns the corresponding C-string pointer of \c s
*
* \ingroup group__library__string
*/
template< ss_typename_param_k S
, ss_typename_param_k X
>
inline C const* c_str_data(basic_resource_string<S, X> const& s)
{
return s.c_str();
}
#endif /* 0 */
/* c_str_ptr_len */
/** \brief Returns the length (in characters) of \c s, <b><i>not</i></b> including the null-terminating character
*
* \ingroup group__library__string
*/
template< ss_typename_param_k S
, ss_typename_param_k X
>
inline ss_size_t c_str_len(basic_resource_string<S, X> const& s)
{
return s.length();
}
/* ////////////////////////////////////////////////////////////////////// */
#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace winstl
# else
} // namespace winstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* !WINSTL_INCL_WINSTL_STRING_HPP_RESOURCE_STRING */
/* ////////////////////////////////////////////////////////////////////// */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -