|
| 1 | +/* Copyright (C) 2003-2013 Runtime Revolution Ltd. |
| 2 | +
|
| 3 | +This file is part of LiveCode. |
| 4 | +
|
| 5 | +LiveCode is free software; you can redistribute it and/or modify it under |
| 6 | +the terms of the GNU General Public License v3 as published by the Free |
| 7 | +Software Foundation. |
| 8 | +
|
| 9 | +LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | +WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | +for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU General Public License |
| 15 | +along with LiveCode. If not see <http://www.gnu.org/licenses/>. */ |
| 16 | + |
| 17 | +#ifndef __MINI_ATL_H |
| 18 | +#define __MINI_ATL_H |
| 19 | +#define __ATLBASE_H__ |
| 20 | + |
| 21 | +#include <malloc.h> |
| 22 | + |
| 23 | +//////////////////////////////////////////////////////////////////////////////// |
| 24 | +// String Conversion |
| 25 | + |
| 26 | +inline char *_ConvW2A(char *p_ansi, const WCHAR *p_wide, int p_ansi_chars) |
| 27 | +{ |
| 28 | + int t_conv; |
| 29 | + t_conv = WideCharToMultiByte(CP_ACP, 0, p_wide, -1, p_ansi, p_ansi_chars, NULL, NULL); |
| 30 | + if (t_conv == 0) |
| 31 | + return NULL; |
| 32 | + return p_ansi; |
| 33 | +} |
| 34 | + |
| 35 | +inline WCHAR *_ConvA2W(WCHAR *p_wide, const char *p_ansi, int p_wide_chars) |
| 36 | +{ |
| 37 | + int t_conv; |
| 38 | + t_conv = MultiByteToWideChar(CP_ACP, 0, p_ansi, -1, p_wide, p_wide_chars); |
| 39 | + if (t_conv == 0) |
| 40 | + return NULL; |
| 41 | + return p_wide; |
| 42 | +} |
| 43 | + |
| 44 | +#define USES_CONVERSION int _conv_chars; char *_conv_ansi_str; WCHAR *_conv_wide_str; (_conv_chars,_conv_ansi_str,_conv_wide_str); |
| 45 | + |
| 46 | +#define W2A(w) (w == NULL ? NULL : (_conv_chars = WideCharToMultiByte(CP_ACP, 0, w, -1, NULL, 0, NULL, NULL), _conv_ansi_str = (char*)alloca(_conv_chars), _ConvW2A(_conv_ansi_str, w, _conv_chars))) |
| 47 | +#define A2W(a) (a == NULL ? NULL : (_conv_chars = MultiByteToWideChar(CP_ACP, 0, a, -1, NULL, 0), _conv_wide_str = (WCHAR*)alloca(_conv_chars * sizeof(WCHAR)), _ConvA2W(_conv_wide_str, a, _conv_chars))) |
| 48 | + |
| 49 | +#if defined(_UNICODE) |
| 50 | +#define W2T(a) (a) |
| 51 | +#define T2W(a) (a) |
| 52 | +#else |
| 53 | +#define W2T W2A |
| 54 | +#define T2W A2W |
| 55 | +#endif |
| 56 | + |
| 57 | +//////////////////////////////////////////////////////////////////////////////// |
| 58 | +// CComPtr |
| 59 | + |
| 60 | +template<class T> |
| 61 | +class CComPtr |
| 62 | +{ |
| 63 | +public: |
| 64 | + T** operator & (void) |
| 65 | + { |
| 66 | + return &m_ptr; |
| 67 | + } |
| 68 | + |
| 69 | + bool operator ! (void) |
| 70 | + { |
| 71 | + return m_ptr == NULL; |
| 72 | + } |
| 73 | + |
| 74 | + T* operator -> (void) |
| 75 | + { |
| 76 | + return m_ptr; |
| 77 | + } |
| 78 | + |
| 79 | + operator T* (void) |
| 80 | + { |
| 81 | + return m_ptr; |
| 82 | + } |
| 83 | + |
| 84 | + T* operator = (const CComPtr<T> &p_comptr) |
| 85 | + { |
| 86 | + if (m_ptr != p_comptr.m_ptr) |
| 87 | + { |
| 88 | + Release(); |
| 89 | + m_ptr = p_comptr.m_ptr; |
| 90 | + |
| 91 | + if (m_ptr != NULL) |
| 92 | + m_ptr->AddRef(); |
| 93 | + } |
| 94 | + |
| 95 | + return m_ptr; |
| 96 | + } |
| 97 | + |
| 98 | + CComPtr() |
| 99 | + { |
| 100 | + m_ptr = NULL; |
| 101 | + } |
| 102 | + |
| 103 | + //CComPtr(T*); |
| 104 | + //CComPtr(const CComPtr<T>&); |
| 105 | + |
| 106 | + //template<class S> |
| 107 | + //CComPtr(const CComPtr<S>&); |
| 108 | + |
| 109 | + CComPtr(int p_null) |
| 110 | + { |
| 111 | + // initialise to NULL |
| 112 | + m_ptr = NULL; |
| 113 | + } |
| 114 | + |
| 115 | + ~CComPtr() |
| 116 | + { |
| 117 | + Release(); |
| 118 | + } |
| 119 | + |
| 120 | + HRESULT CoCreateInstance(GUID p_id, IUnknown *p_outer = NULL, DWORD p_cls_context = CLSCTX_ALL) |
| 121 | + { |
| 122 | + HRESULT t_result; |
| 123 | + t_result = ::CoCreateInstance(p_id, p_outer, p_cls_context, __uuidof(T), (void**)&m_ptr); |
| 124 | + |
| 125 | + return t_result; |
| 126 | + } |
| 127 | + |
| 128 | + void Release(void) |
| 129 | + { |
| 130 | + if (m_ptr != NULL) |
| 131 | + m_ptr->Release(); |
| 132 | + m_ptr = NULL; |
| 133 | + } |
| 134 | + |
| 135 | + T* Detach(void) |
| 136 | + { |
| 137 | + T* t_ptr = m_ptr; |
| 138 | + m_ptr = NULL; |
| 139 | + return t_ptr; |
| 140 | + } |
| 141 | + |
| 142 | +protected: |
| 143 | + T *m_ptr; |
| 144 | +}; |
| 145 | + |
| 146 | +template<class T, const GUID *I> |
| 147 | +class CComQIPtr : public CComPtr<T> |
| 148 | +{ |
| 149 | +public: |
| 150 | + T* operator = (IUnknown *p_unknown) |
| 151 | + { |
| 152 | + Query(p_unknown); |
| 153 | + return m_ptr; |
| 154 | + } |
| 155 | + |
| 156 | + CComQIPtr() : CComPtr() |
| 157 | + { |
| 158 | + } |
| 159 | + |
| 160 | + //CComQIPtr(T*); |
| 161 | + CComQIPtr(IUnknown *p_unknown) |
| 162 | + { |
| 163 | + Query(p_unknown); |
| 164 | + } |
| 165 | + |
| 166 | +protected: |
| 167 | + void Query(IUnknown *p_unknown) |
| 168 | + { |
| 169 | + IUnknown *t_queried = NULL; |
| 170 | + if (p_unknown != NULL) |
| 171 | + p_unknown->QueryInterface(*I, (void**)&t_queried); |
| 172 | + |
| 173 | + Release(); |
| 174 | + m_ptr = (T*)t_queried; |
| 175 | + } |
| 176 | +}; |
| 177 | + |
| 178 | +//////////////////////////////////////////////////////////////////////////////// |
| 179 | + |
| 180 | +#endif |
0 commit comments