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  

dotnetstl_string_accessor.h

Go to the documentation of this file.
00001 /* 
00002  * File:        dotnetstl_string_accessor.h
00003  *
00004  * Purpose:     A useful tool for accessing a String object's content as a c-string.
00005  *
00006  * Created:     24th June 2003
00007  * Updated:     11th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 2003-2004, Matthew Wilson and Synesis Software
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *
00017  * - Redistributions of source code must retain the above copyright notice, this
00018  *   list of conditions and the following disclaimer.
00019  * - Redistributions in binary form must reproduce the above copyright notice,
00020  *   this list of conditions and the following disclaimer in the documentation
00021  *   and/or other materials provided with the distribution.
00022  * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
00023  *   any contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  *
00038  * 
00039 
00040 
00044 
00045 #ifndef DOTNETSTL_INCL_H_DOTNETSTL_STRING_ACCESSOR
00046 #define DOTNETSTL_INCL_H_DOTNETSTL_STRING_ACCESSOR
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define DOTNETSTL_VER_H_DOTNETSTL_STRING_ACCESSOR_MAJOR    2
00050 # define DOTNETSTL_VER_H_DOTNETSTL_STRING_ACCESSOR_MINOR    0
00051 # define DOTNETSTL_VER_H_DOTNETSTL_STRING_ACCESSOR_REVISION 1
00052 # define DOTNETSTL_VER_H_DOTNETSTL_STRING_ACCESSOR_EDIT     20
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Includes
00057  */
00058 
00059 #ifndef DOTNETSTL_INCL_H_DOTNETSTL
00060 # include "dotnetstl.h"                     // Include the .netSTL root header
00061 #endif /* !DOTNETSTL_INCL_H_DOTNETSTL */
00062 #ifndef STLSOFT_INCL_H_STLSOFT_SAP_CAST
00063 # include "stlsoft_sap_cast.h"
00064 #endif /* !STLSOFT_INCL_H_STLSOFT_SAP_CAST */
00065 #ifdef STLSOFT_UNITTEST
00066 # include <wchar.h>
00067 #endif /* STLSOFT_UNITTEST */
00068 
00069 /* 
00070 
00071 #ifdef _STLSOFT_NO_NAMESPACE
00072 /* There is no stlsoft namespace, so must define ::dotnetstl */
00073 namespace dotnetstl
00074 {
00075 #else
00076 /* Define stlsoft::dotnet_project */
00077 
00078 namespace stlsoft
00079 {
00080 
00081 namespace dotnetstl_project
00082 {
00083 
00084 #endif /* _STLSOFT_NO_NAMESPACE */
00085 
00086 /* 
00087  * Classes
00088  */
00089 
00091 template <ss_typename_param_k C>
00092 class c_string_accessor
00093 {
00094 public:
00095     typedef C           char_type;
00096     typedef C           *pointer;
00097     typedef C const     *const_pointer;
00098 
00099 // Construction
00100 public:
00104     ss_explicit_k c_string_accessor(System::String *s)
00105         : m_s(get_cstring_(s))
00106     {}
00108     ~c_string_accessor()
00109     {
00110         System::Runtime::InteropServices::Marshal::FreeHGlobal(m_s);
00111     }
00112 
00113 // Accessors
00114 public:
00116     operator const_pointer() const
00117     {
00118         return m_s;
00119     }
00120 
00121 // Implementation
00122 private:
00123     pointer get_cstring_(System::String *s);
00124 
00125 // Members
00126 private:
00127     pointer m_s;
00128 
00129 // Not to be implemented
00130 private:
00131     c_string_accessor(c_string_accessor const &);
00132     c_string_accessor &operator =(c_string_accessor const &);
00133 };
00134 
00136 // Implementation
00137 
00138 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00139 
00140 template <>
00141 inline c_string_accessor<ds_char_a_t>::pointer c_string_accessor<ds_char_a_t>::get_cstring_(System::String *s)
00142 {
00143     return sap_cast<ds_char_a_t*>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer());
00144 }
00145 
00146 template <>
00147 inline c_string_accessor<ds_char_w_t>::pointer c_string_accessor<ds_char_w_t>::get_cstring_(System::String *s)
00148 {
00149     return sap_cast<ds_char_w_t*>(System::Runtime::InteropServices::Marshal::StringToHGlobalUni(s).ToPointer());
00150 }
00151 
00152 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00153 
00155 // Unit-testing
00156 
00157 #ifdef STLSOFT_UNITTEST
00158 
00159 namespace unittest
00160 {
00161     ss_bool_t test_dotnetstl_string_accessor(unittest_reporter *r)
00162     {
00163         using stlsoft::unittest::unittest_initialiser;
00164 
00165         ss_bool_t               bSuccess    =   true;
00166 
00167         unittest_initialiser    init(r, ".netSTL", "string_accessor", __FILE__);
00168 
00169         System::String  *s1 =   new System::String("String #1");
00170         System::String  *s2 =   new System::String("String #2");
00171 
00172         if(0 != strcmp(c_string_accessor<char>(s1), "String #1"))
00173         {
00174             r->report("c_string_accessor<char>() failed ", __LINE__);
00175             bSuccess = false;
00176         }
00177 
00178         if(0 != wcscmp(c_string_accessor<wchar_t>(s2), L"String #2"))
00179         {
00180             r->report("c_string_accessor<char>() failed ", __LINE__);
00181             bSuccess = false;
00182         }
00183 
00184         return bSuccess;
00185     }
00186 
00187     unittest_registrar    unittest_dotnetstl_string_accessor(test_dotnetstl_string_accessor);
00188 
00189 } // namespace unittest
00190 
00191 #endif /* STLSOFT_UNITTEST */
00192 
00193 /* 
00194 
00195 #ifdef _STLSOFT_NO_NAMESPACE
00196 } // namespace dotnetstl
00197 #else
00198 } // namespace dotnetstl_project
00199 } // namespace stlsoft
00200 #endif /* _STLSOFT_NO_NAMESPACE */
00201 
00202 /* 
00203 
00204 #endif /* DOTNETSTL_INCL_H_DOTNETSTL_STRING_ACCESSOR */
00205 
00206 /* 

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