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  

atlstl_property_method_helpers.h

Go to the documentation of this file.
00001 /* 
00002  * File:        atlstl_property_method_helpers.h
00003  *
00004  * Purpose:     Contains functions for assisting in the implementation of
00005  *              property methods of ATL COM server classes.
00006  *
00007  * Created:
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 ATLSTL_INCL_H_ATLSTL_PROPERTY_METHOD_HELPERS
00047 #define ATLSTL_INCL_H_ATLSTL_PROPERTY_METHOD_HELPERS
00048 
00049 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00050 # define ATLSTL_VER_H_ATLSTL_PROPERTY_METHOD_HELPERS_MAJOR      2
00051 # define ATLSTL_VER_H_ATLSTL_PROPERTY_METHOD_HELPERS_MINOR      0
00052 # define ATLSTL_VER_H_ATLSTL_PROPERTY_METHOD_HELPERS_REVISION   1
00053 # define ATLSTL_VER_H_ATLSTL_PROPERTY_METHOD_HELPERS_EDIT       41
00054 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00055 
00056 /* 
00057  * Compatibility
00058  */
00059 
00060 /*
00061 [Incompatibilies-start]
00062 __STLSOFT_COMPILER_IS_BORLAND: __BORLANDC__=*
00063 [Incompatibilies-end]
00064  */
00065 
00066 /* 
00067  * Includes
00068  */
00069 
00070 #ifndef ATLSTL_INCL_H_ATLSTL
00071 # include "atlstl.h"    // Include the ATLSTL root header
00072 #endif /* !ATLSTL_INCL_H_ATLSTL */
00073 
00074 #if defined(__STLSOFT_COMPILER_IS_BORLAND)
00075 # error No recognised Borland compiler generates correct code when used with these functions
00076 #endif /* __STLSOFT_COMPILER_IS_BORLAND */
00077 
00078 /* 
00079  * Namespace
00080  *
00081  * The ATLSTL components are contained within the atlstl namespace. This is
00082  * actually an alias for stlsoft::atlstl_project,
00083  *
00084  * The definition matrix is as follows:
00085  *
00086  * _STLSOFT_NO_NAMESPACE    _ATLSTL_NO_NAMESPACE    atlstl definition
00087  * ---------------------    --------------------    -----------------
00088  *  not defined              not defined             = stlsoft::atlstl_project
00089  *  not defined              defined                 not defined
00090  *  defined                  not defined             atlstl
00091  *  defined                  defined                 not defined
00092  *
00093  */
00094 
00095 #ifndef _ATLSTL_NO_NAMESPACE
00096 # if defined(_STLSOFT_NO_NAMESPACE) || \
00097      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00098 /* There is no stlsoft namespace, so must define ::atlstl */
00099 namespace atlstl
00100 {
00101 # else
00102 /* Define stlsoft::atlstl_project */
00103 
00104 namespace stlsoft
00105 {
00106 
00107 namespace atlstl_project
00108 {
00109 
00110 # endif /* _STLSOFT_NO_NAMESPACE */
00111 #endif /* !_ATLSTL_NO_NAMESPACE */
00112 
00113 /* 
00114 
00119 
00120 /* 
00121  * get_MemberValue
00122  *
00123  * This can be used to get a member variable value. The function has the
00124  * general usage:
00125  *
00126  *   HRESULT Class::get_Member(MemberType *ret)
00127  *   {
00128  *       return get_MemberValue(this, ret, &Class::m_memberVariable);
00129  *   }
00130  */
00131 
00146 template <ss_typename_param_k C, ss_typename_param_k T>
00147 inline HRESULT get_MemberValue(C *const cls, T *ret, T C::*mem)
00148 {
00149     return (ret == 0) ? E_POINTER : (*ret = cls->*mem, S_OK);
00150 }
00151 
00163 template <ss_typename_param_k C, class T>
00164 inline HRESULT get_MemberValue(C *const cls, T **ret, T *C::*mem);
00165 
00179 template <ss_typename_param_k C>
00180 inline HRESULT get_MemberValue(C *const cls, BSTR *ret, CComBSTR C::*mem)
00181 {
00182     return (ret == 0) ? E_POINTER : (*ret = (cls->*mem).Copy(), (*ret != 0 ? S_OK :  E_OUTOFMEMORY));
00183 }
00184 
00201 template <ss_typename_param_k C>
00202 inline HRESULT get_MemberValue(C *const cls, VARIANT *ret, CComVariant C::*mem)
00203 {
00204     return (ret == 0) ? E_POINTER : ::VariantCopy(ret, &(cls->*mem));
00205 }
00206 
00207 /* 
00208  * put_MemberValue
00209  *
00210  * This can be used to put a member variable value. The function has the
00211  * general usage:
00212  *
00213  *   HRESULT Class::put_Member(MemberType newValue)
00214  *   {
00215  *       return put_MemberValue(this, newValue, &Class::m_memberVariable);
00216  *   }
00217  */
00218 
00230 template <ss_typename_param_k C, ss_typename_param_k T>
00231 inline HRESULT put_MemberValue(C *const cls, T const &newVal, T C::*mem)
00232 {
00233     return (cls->*mem = newVal, S_OK);
00234 }
00235 
00246 template <ss_typename_param_k C>
00247 inline HRESULT put_MemberValue(C *const cls, BSTR newVal, CComBSTR C::*mem)
00248 {
00249     return (cls->*mem = newVal, S_OK);
00250 }
00251 
00262 template <ss_typename_param_k C>
00263 inline HRESULT put_MemberValue(C *const cls, CComBSTR const &newVal, CComBSTR C::*mem)
00264 {
00265     return put_MemberValue(cls, (BSTR)newVal, mem);
00266 }
00267 
00278 template <ss_typename_param_k C>
00279 inline HRESULT put_MemberValue(C *const cls, CComVariant const &newVal, CComVariant C::*mem)
00280 {
00281     return put_MemberValue(cls, (VARIANT const &)newVal, mem);
00282 }
00283 
00285 // Unit-testing
00286 
00287 #ifdef STLSOFT_UNITTEST
00288 
00289 namespace unittest
00290 {
00291     class SimpleServer
00292     {
00293     public:
00294         SimpleServer()
00295             : m_bstrUserName("[No username]")
00296             , m_logonKey(-1)
00297         {}
00298 
00299     // Property accessors
00300     public:
00301         STDMETHODIMP SimpleServer::get_UserName(/* [retval][out] */ BSTR *pVal)
00302         {
00303             return get_MemberValue(this, pVal, &SimpleServer::m_bstrUserName);
00304         }
00305 
00306         STDMETHODIMP SimpleServer::put_UserName(/* [in] */ BSTR newVal)
00307         {
00308             return put_MemberValue(this, newVal, &SimpleServer::m_bstrUserName);
00309         }
00310 
00311         STDMETHODIMP SimpleServer::get_LogonKey(/* [retval][out] */ short *pVal)
00312         {
00313             return get_MemberValue(this, pVal, &SimpleServer::m_logonKey);
00314         }
00315 
00316         STDMETHODIMP SimpleServer::put_LogonKey(/* [in] */ short newVal)
00317         {
00318             return put_MemberValue(this, newVal, &SimpleServer::m_logonKey);
00319         }
00320 
00321     // Members
00322     protected:
00323         friend ss_bool_t test_atlstl_property_method_helpers(unittest_reporter *r);
00324 
00325         CComBSTR    m_bstrUserName;
00326         short       m_logonKey;
00327     };
00328 
00329     ss_bool_t test_atlstl_property_method_helpers(unittest_reporter *r)
00330     {
00331         using stlsoft::unittest::unittest_initialiser;
00332 
00333         ss_bool_t               bSuccess    =   true;
00334         HRESULT                 hr;
00335 
00336         unittest_initialiser    init(r, "ATLSTL", "property_method_helpers", __FILE__);
00337 
00338         SimpleServer    simpleserver;
00339         short           logonKey(-1);
00340         CComBSTR        bstrUserName;
00341 
00342         hr = simpleserver.put_LogonKey(12);
00343         if(FAILED(hr))
00344         {
00345             r->report("call to put_LogonKey() failed ", __LINE__);
00346             bSuccess = false;
00347         }
00348 
00349         hr = simpleserver.get_LogonKey(&logonKey);
00350         if(FAILED(hr))
00351         {
00352             r->report("call to get_LogonKey() failed ", __LINE__);
00353             bSuccess = false;
00354         }
00355         else
00356         {
00357             if(12 != logonKey)
00358             {
00359                 r->report("put/get_MemberValue failed ", __LINE__);
00360                 bSuccess = false;
00361             }
00362         }
00363 
00364         simpleserver.put_UserName(L"Matty");
00365         hr = simpleserver.get_UserName(&bstrUserName);
00366         if(SUCCEEDED(hr))
00367         {
00368             if(!(bstrUserName == L"Matty"))
00369             {
00370                 r->report("put/get_MemberValue failed ", __LINE__);
00371                 bSuccess = false;
00372             }
00373         }
00374 
00375         return bSuccess;
00376     }
00377 
00378     unittest_registrar    unittest_atlstl_property_method_helpers(test_atlstl_property_method_helpers);
00379 
00380 } // namespace unittest
00381 
00382 #endif /* STLSOFT_UNITTEST */
00383 
00384 /* 
00385 
00387 
00388 /* 
00389 
00390 #ifndef _ATLSTL_NO_NAMESPACE
00391 # if defined(_STLSOFT_NO_NAMESPACE) || \
00392      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00393 } // namespace atlstl
00394 # else
00395 } // namespace atlstl_project
00396 } // namespace stlsoft
00397 # endif /* _STLSOFT_NO_NAMESPACE */
00398 #endif /* !_ATLSTL_NO_NAMESPACE */
00399 
00400 /* 
00401 
00402 #endif /* !ATLSTL_INCL_H_ATLSTL_PROPERTY_METHOD_HELPERS */
00403 
00404 /* 

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