STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ... ATLSTL - Template Software for the Active Template Library COMSTL - The Standard Template Library meets the Component Object Model .netSTL - Standard Template Library meets the Microsoft.NET Common Language Runtime InetSTL - The Standard Template Library meets WinInet MFCSTL - Template Software for the Microsoft Foundation Classes UNIXSTL - Template Software for the UNIX Operating System WinSTL - where the Standard Template Library meets the Win32 API

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

winstl_module_filename.h

Go to the documentation of this file.
00001 /* 
00002  * File:        winstl_module_filename.h (originally MWModFNm.h; ::SynesisWin)
00003  *
00004  * Purpose:     Preposterously simple class that gets, and makes accessible, the
00005  *              module filename.
00006  *
00007  * Created:     31st March 2002
00008  * Updated:     11th September 2004
00009  *
00010  * Home:        http://stlsoft.org/
00011  *
00012  * Copyright (c) 2002-2004, Matthew Wilson and Synesis Software
00013  * All rights reserved.
00014  *
00015  * Redistribution and use in source and binary forms, with or without
00016  * modification, are permitted provided that the following conditions are met:
00017  *
00018  * - Redistributions of source code must retain the above copyright notice, this
00019  *   list of conditions and the following disclaimer.
00020  * - Redistributions in binary form must reproduce the above copyright notice,
00021  *   this list of conditions and the following disclaimer in the documentation
00022  *   and/or other materials provided with the distribution.
00023  * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
00024  *   any contributors may be used to endorse or promote products derived from
00025  *   this software without specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00037  * POSSIBILITY OF SUCH DAMAGE.
00038  *
00039  * 
00040 
00041 
00045 
00046 #ifndef WINSTL_INCL_H_WINSTL_MODULE_FILENAME
00047 #define WINSTL_INCL_H_WINSTL_MODULE_FILENAME
00048 
00049 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00050 # define WINSTL_VER_H_WINSTL_MODULE_FILENAME_MAJOR      2
00051 # define WINSTL_VER_H_WINSTL_MODULE_FILENAME_MINOR      0
00052 # define WINSTL_VER_H_WINSTL_MODULE_FILENAME_REVISION   1
00053 # define WINSTL_VER_H_WINSTL_MODULE_FILENAME_EDIT       40
00054 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00055 
00056 /* 
00057  * Compatibility
00058  */
00059 
00060 /*
00061 [Incompatibilies-start]
00062 __STLSOFT_COMPILER_IS_MSVC: _MSC_VER<1200
00063 [Incompatibilies-end]
00064  */
00065 
00066 /* 
00067  * Includes
00068  */
00069 
00070 #ifndef WINSTL_INCL_H_WINSTL
00071 # include "winstl.h"                // Include the WinSTL root header
00072 #endif /* !WINSTL_INCL_H_WINSTL */
00073 
00074 #if defined(__STLSOFT_COMPILER_IS_MSVC) && \
00075     _MSC_VER < 1200
00076 # error winstl_module_filename.h is not compatible with Visual C++ 5.0 or earlier
00077 #endif /* _MSC_VER < 1200 */
00078 
00079 #ifndef WINSTL_INCL_H_WINSTL_FILESYSTEM_TRAITS
00080 # include "winstl_filesystem_traits.h"  // filesystem_traits
00081 #endif /* !WINSTL_INCL_H_WINSTL_FILESYSTEM_TRAITS */
00082 #ifndef WINSTL_INCL_H_WINSTL_FILE_PATH_BUFFER
00083 # include "winstl_file_path_buffer.h"   // basic_file_path_buffer
00084 #endif /* !WINSTL_INCL_H_WINSTL_FILE_PATH_BUFFER */
00085 
00086 /* 
00087  * Namespace
00088  */
00089 
00090 #ifndef _WINSTL_NO_NAMESPACE
00091 # if defined(_STLSOFT_NO_NAMESPACE) || \
00092      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00093 /* There is no stlsoft namespace, so must define ::winstl */
00094 namespace winstl
00095 {
00096 # else
00097 /* Define stlsoft::winstl_project */
00098 
00099 namespace stlsoft
00100 {
00101 
00102 namespace winstl_project
00103 {
00104 
00105 # endif /* _STLSOFT_NO_NAMESPACE */
00106 #endif /* !_WINSTL_NO_NAMESPACE */
00107 
00108 /* 
00109 
00112 
00116 
00121 
00122 /* 
00123  * basic_module_filename
00124  *
00125  * This class wraps the GetModuleFilename() API function, and effectively acts
00126  * as an adaptor between HINSTANCE and a C-string to the name.
00127  */
00128 
00133 template<   ss_typename_param_k C
00134 #ifdef __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
00135         ,   ss_typename_param_k T = filesystem_traits<C>
00136 #else
00137         ,   ss_typename_param_k T /* = filesystem_traits<C> */
00138 #endif /* __STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
00139         >
00140 class basic_module_filename
00141 {
00142 public:
00144     typedef C                           char_type;
00146     typedef T                           traits_type;
00148     typedef basic_module_filename<C, T> class_type;
00150     typedef ws_size_t                   size_type;
00151 
00152 // Construction
00153 public:
00155     basic_module_filename();
00157     ss_explicit_k basic_module_filename(HINSTANCE hinst);
00158 
00159 // Operations
00160 public:
00162     size_type           get_filename(ws_char_a_t *buffer, size_type cchBuffer) const;
00164     size_type           get_filename(ws_char_w_t *buffer, size_type cchBuffer) const;
00166     static size_type    get_filename(HINSTANCE hinst, ws_char_a_t *buffer, size_type cchBuffer);
00168     static size_type    get_filename(HINSTANCE hinst, ws_char_w_t *buffer, size_type cchBuffer);
00169 
00170 // Attributes
00171 public:
00173     char_type const *get_filename() const;
00175     size_type       length() const;
00176 
00177 // Conversions
00178 public:
00180     operator char_type const *() const
00181     {
00182         return get_filename();
00183     }
00184 
00185 // Members
00186 private:
00187     basic_file_path_buffer<char_type>   m_path;
00188     HINSTANCE const                     m_hinst;
00189     size_type const                     m_len;
00190 
00191 // Not to be implemented
00192 private:
00193     basic_module_filename(class_type const &);
00194     basic_module_filename &operator =(class_type const &);
00195 };
00196 
00197 /* 
00198  * Typedefs for commonly encountered types
00199  */
00200 
00202 typedef basic_module_filename<ws_char_a_t, filesystem_traits<ws_char_a_t> >     module_filename_a;
00204 typedef basic_module_filename<ws_char_w_t, filesystem_traits<ws_char_w_t> >     module_filename_w;
00206 typedef basic_module_filename<TCHAR, filesystem_traits<TCHAR> >                 module_filename;
00207 
00208 /* 
00209  * Unit-testing
00210  */
00211 
00212 #ifdef STLSOFT_UNITTEST
00213 
00214 namespace unittest
00215 {
00216     ss_bool_t test_winstl_module_filename(unittest_reporter *r)
00217     {
00218         using stlsoft::unittest::unittest_initialiser;
00219 
00220         ss_bool_t               bSuccess    =   true;
00221 
00222         unittest_initialiser    init(r, "WinSTL", "module_filename", __FILE__);
00223 
00224         TCHAR   mfn[1 + _MAX_PATH];
00225 
00226         ::GetModuleFileName(NULL, &mfn[0], stlsoft_num_elements(mfn));
00227 
00228         if(0 != lstrcmp(mfn, module_filename()))
00229         {
00230             r->report("module filename failed ", __LINE__);
00231             bSuccess = false;
00232         }
00233 
00234         return bSuccess;
00235     }
00236 
00237     unittest_registrar    unittest_winstl_module_filename(test_winstl_module_filename);
00238 
00239 } // namespace unittest
00240 
00241 #endif /* STLSOFT_UNITTEST */
00242 
00243 /* 
00244  * Implementation
00245  */
00246 
00247 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00248 
00249 template<   ss_typename_param_k C
00250         ,   ss_typename_param_k T
00251         >
00252 inline basic_module_filename<C, T>::basic_module_filename()
00253     : m_hinst(::GetModuleHandle(NULL))
00254     , m_len(get_filename(&m_path[0], m_path.size()))
00255 {
00256     stlsoft_static_assert(stlsoft_raw_offsetof(class_type, m_hinst) < stlsoft_raw_offsetof(class_type, m_len));
00257 }
00258 
00259 template<   ss_typename_param_k C
00260         ,   ss_typename_param_k T
00261         >
00262 inline basic_module_filename<C, T>::basic_module_filename(HINSTANCE hinst)
00263     : m_hinst(hinst)
00264     , m_len(get_filename(hinst, &m_path[0], m_path.size()))
00265 {
00266     stlsoft_static_assert(stlsoft_raw_offsetof(class_type, m_hinst) < stlsoft_raw_offsetof(class_type, m_len));
00267 }
00268 
00269 template<   ss_typename_param_k C
00270         ,   ss_typename_param_k T
00271         >
00272 inline ss_typename_type_k basic_module_filename<C, T>::size_type basic_module_filename<C, T>::get_filename(ws_char_a_t *buffer, ss_typename_type_k basic_module_filename<C, T>::size_type cchBuffer) const
00273 {
00274     return static_cast<size_type>(::GetModuleFileNameA(m_hinst, buffer, cchBuffer));
00275 }
00276 
00277 template<   ss_typename_param_k C
00278         ,   ss_typename_param_k T
00279         >
00280 inline ss_typename_type_k basic_module_filename<C, T>::size_type basic_module_filename<C, T>::get_filename(ws_char_w_t *buffer, ss_typename_type_k basic_module_filename<C, T>::size_type cchBuffer) const
00281 {
00282     return static_cast<size_type>(::GetModuleFileNameW(m_hinst, buffer, cchBuffer));
00283 }
00284 
00285 template<   ss_typename_param_k C
00286         ,   ss_typename_param_k T
00287         >
00288 inline /* static */ ss_typename_type_k basic_module_filename<C, T>::size_type basic_module_filename<C, T>::get_filename(HINSTANCE hinst, ws_char_a_t *buffer, ss_typename_type_k basic_module_filename<C, T>::size_type cchBuffer)
00289 {
00290     return static_cast<size_type>(::GetModuleFileNameA(hinst, buffer, cchBuffer));
00291 }
00292 
00293 template<   ss_typename_param_k C
00294         ,   ss_typename_param_k T
00295         >
00296 inline /* static */ ss_typename_type_k basic_module_filename<C, T>::size_type basic_module_filename<C, T>::get_filename(HINSTANCE hinst, ws_char_w_t *buffer, ss_typename_type_k basic_module_filename<C, T>::size_type cchBuffer)
00297 {
00298     return static_cast<size_type>(::GetModuleFileNameW(hinst, buffer, cchBuffer));
00299 }
00300 
00301 template<   ss_typename_param_k C
00302         ,   ss_typename_param_k T
00303         >
00304 inline ss_typename_type_k basic_module_filename<C, T>::char_type const *basic_module_filename<C, T>::get_filename() const
00305 {
00306     return stlsoft_ns_qual(c_str_ptr)(m_path);
00307 }
00308 
00309 template<   ss_typename_param_k C
00310         ,   ss_typename_param_k T
00311         >
00312 inline ss_typename_type_k basic_module_filename<C, T>::size_type basic_module_filename<C, T>::length() const
00313 {
00314     return m_len;
00315 }
00316 
00317 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00318 
00319 /* 
00320  * String access shims
00321  */
00322 
00323 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00324 
00325 template<   ss_typename_param_k C
00326         ,   ss_typename_param_k T
00327         >
00328 inline ws_char_a_t const *c_str_ptr_a_null(basic_module_filename<C, T> const &mfn)
00329 {
00330     return mfn; // Can never be empty, so no need to cater for NULL return
00331 }
00332 
00333 template<   ss_typename_param_k C
00334         ,   ss_typename_param_k T
00335         >
00336 inline ws_char_w_t const *c_str_ptr_w_null(basic_module_filename<C, T> const &mfn)
00337 {
00338     return mfn; // Can never be empty, so no need to cater for NULL return
00339 }
00340 
00341 template<   ss_typename_param_k C
00342         ,   ss_typename_param_k T
00343         >
00344 inline ws_char_a_t const *c_str_ptr_a(basic_module_filename<C, T> const &mfn)
00345 {
00346     return mfn;
00347 }
00348 
00349 template<   ss_typename_param_k C
00350         ,   ss_typename_param_k T
00351         >
00352 inline ws_char_w_t const *c_str_ptr_w(basic_module_filename<C, T> const &mfn)
00353 {
00354     return mfn;
00355 }
00356 
00357 template<   ss_typename_param_k C
00358         ,   ss_typename_param_k T
00359         >
00360 inline ss_typename_type_k basic_module_filename<C, T>::size_type c_str_len_a(basic_module_filename<C, T> const &mfn)
00361 {
00362     return mfn.length();
00363 }
00364 
00365 template<   ss_typename_param_k C
00366         ,   ss_typename_param_k T
00367         >
00368 inline ss_typename_type_k basic_module_filename<C, T>::size_type c_str_size(basic_module_filename<C, T> const &mfn)
00369 {
00370     return mfn.length() * sizeof(ss_typename_type_k basic_module_filename<C, T>::char_type);
00371 }
00372 
00373 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00374 
00375 /* 
00376 
00378 
00379 /* 
00380 
00381 #ifndef _WINSTL_NO_NAMESPACE
00382 # if defined(_STLSOFT_NO_NAMESPACE) || \
00383      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00384 } // namespace winstl
00385 # else
00386 } // namespace winstl_project
00387 } // namespace stlsoft
00388 # endif /* _STLSOFT_NO_NAMESPACE */
00389 #endif /* !_WINSTL_NO_NAMESPACE */
00390 
00391 /* 
00392 
00393 #endif /* WINSTL_INCL_H_WINSTL_MODULE_FILENAME */
00394 
00395 /* 

STLSoft Libraries documentation © Synesis Software Pty Ltd, 2001-2004