📄 resource_string.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: winstl/string/resource_string.hpp (was winstl_resource_string.h; originally MWResStr.h: ::SynesisWin)
*
* Purpose: basic_resource_string class.
*
* Created: 1st November 1994
* Updated: 22nd March 2007
*
* Thanks to: Ryan Ginstrom for suggesting the implementation for handling
* Unicode strings on Win9x.
*
* Home: http://stlsoft.org/
*
* Copyright (c) 1994-2007, Matthew Wilson and Synesis Software
* Copyright (c) 2004-2005, Ryan Ginstrom
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
* any contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/** \file winstl/string/resource_string.hpp
*
* \brief [C++ only] Definition of the winstl::basic_resource_string class
* template
* (\ref group__library__string "String" Library).
*/
#ifndef WINSTL_INCL_WINSTL_STRING_HPP_RESOURCE_STRING
#define WINSTL_INCL_WINSTL_STRING_HPP_RESOURCE_STRING
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define WINSTL_VER_WINSTL_STRING_HPP_RESOURCE_STRING_MAJOR 4
# define WINSTL_VER_WINSTL_STRING_HPP_RESOURCE_STRING_MINOR 2
# define WINSTL_VER_WINSTL_STRING_HPP_RESOURCE_STRING_REVISION 2
# define WINSTL_VER_WINSTL_STRING_HPP_RESOURCE_STRING_EDIT 79
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Compatibility
*/
/*
[Incompatibilies-start]
STLSOFT_COMPILER_IS_MSVC: _MSC_VER<1200
[Incompatibilies-end]
*/
/* /////////////////////////////////////////////////////////////////////////
* Includes
*/
#ifndef WINSTL_INCL_WINSTL_H_WINSTL
# include <winstl/winstl.h>
#endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
//#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_TRAITS
//# include <stlsoft/string/string_traits.hpp>
//#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_TRAITS */
#ifndef STLSOFT_INCL_STLSOFT_ERROR_HPP_EXCEPTIONS
# include <stlsoft/error/exceptions.hpp> // for null_exception_policy
#endif /* !STLSOFT_INCL_STLSOFT_ERROR_HPP_EXCEPTIONS */
#ifndef WINSTL_INCL_WINSTL_ERROR_HPP_WINDOWS_EXCEPTIONS
# include <winstl/error/exceptions.hpp>
#endif /* !WINSTL_INCL_WINSTL_ERROR_HPP_WINDOWS_EXCEPTIONS */
#ifdef STLSOFT_UNITTEST
# include <iostream> // for std::cout, std::endl
# include <string> // for std::string, std::wstring
#endif /* STLSOFT_UNITTEST */
#include <exception>
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::winstl */
namespace winstl
{
# else
/* Define stlsoft::winstl_project */
namespace stlsoft
{
namespace winstl_project
{
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Classes
*/
/** \brief Instances of this class represent Windows string resources, and are
* constructed from instance identifiers.
*
* \ingroup group__library__string
*
* It is an adaptor template, so is parameterised with the underlying string
* type. For example, <code>winstl::basic_resource_string<std::string></code>
* is parameterised from <code>std::string</code>, and can therefore use its methods
* and is compatible with its client code:
*
\code
winstl::basic_resource_string<std::string> str(1024);
std::cout << "String with id 1024: " << str << std::endl;
fprintf(stdout, "String with id 1024: %.*s\n", str.size(), str.data());
\endcode
*
* The second template parameter is the exception policy, which determines
* how the string reacts to a failure to load a string resource corresponding
* to the given Id. It is defaulted to stlsoft::null_exception_policy, which
* means that, when a corresponding string resource is not loaded, the
* resource string instance will be correctly constructed but will contain
* the empty string, i.e.:
*
\code
// Assuming 9999999 is not a valid string resource identifier in the
// module whose instance handle is in hinst ...
winstl::basic_resource_string<std::string> str(hinst, 9999999);
assert(0 == str.size());
assert(str == "");
\endcode
*
* If you want your parameterisation to throw an exception when the string
* resource is not found, simply specify a policy that throws an exception
* to the parameterisation, as in:
*
\code
// Assuming 9999999 is not a valid string resource identifier in the
// module whose instance handle is in hinst ...
try
{
winstl::basic_resource_string<std::string, throw_MyX_policy> str(hinst, 9999999);
std::cerr << "Should never get here!!" << std::endl;
}
catch(MyX &x)
{
std::cerr << "This is what's expected" << std::endl;
}
\endcode
*
* \note The handling of Unicode strings under Windows 9x family operating
* systems eschews the use of LoadStringW(), instead manipulating the resource
* information via FindResourceEx() / LoadResource() / LockResource(). This
* code kindly provided by Ryan Ginstrom.
*
* \param S The string class, e.g. std::string, stlsoft::simple_string, etc.
* \param X The exception class
*/
template< ss_typename_param_k S
#ifdef STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
# ifdef STLSOFT_CF_EXCEPTION_SUPPORT
, ss_typename_param_k X = resource_exception_policy
# else /* ? STLSOFT_CF_EXCEPTION_SUPPORT */
, ss_typename_param_k X = stlsoft_ns_qual(null_exception_policy)
# endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
#else /* ? STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
, ss_typename_param_k X /* = stlsoft_ns_qual(null_exception_policy) */
#endif /* STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
>
// class basic_resource_string
class basic_resource_string
: public S
, protected X
{
private:
typedef S parent_class_type;
public:
/// The type of the underlying string
typedef S string_type;
/// The type of the current parameterisation
typedef basic_resource_string<S, X> class_type;
/// The exception policy type
typedef X exception_policy_type;
/// The exception policy type
///
/// \deprecated
typedef exception_policy_type exception_type;
// typedef stlsoft_ns_qual(string_traits)<S> string_traits_type;
typedef ss_typename_type_k string_type::value_type value_type;
/// \name Construction
/// @{
public:
/// Constructs an around the string loaded from the given \c id
ss_explicit_k basic_resource_string(ws_int_t id) stlsoft_throw_1(ss_typename_type_k exception_policy_type::thrown_type)
{
this->load_(::GetModuleHandle(NULL), id, NULL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -