00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00052 #ifndef WINSTL_INCL_WINSTL_TOOLHELP_HPP_SEQUENCE_BASE
00053 #define WINSTL_INCL_WINSTL_TOOLHELP_HPP_SEQUENCE_BASE
00054
00055 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00056 # define WINSTL_VER_WINSTL_TOOLHELP_HPP_SEQUENCE_BASE_MAJOR 1
00057 # define WINSTL_VER_WINSTL_TOOLHELP_HPP_SEQUENCE_BASE_MINOR 1
00058 # define WINSTL_VER_WINSTL_TOOLHELP_HPP_SEQUENCE_BASE_REVISION 4
00059 # define WINSTL_VER_WINSTL_TOOLHELP_HPP_SEQUENCE_BASE_EDIT 14
00060 #endif
00061
00062
00063
00064
00065
00066 #ifndef WINSTL_INCL_WINSTL_H_WINSTL
00067 # include <winstl/winstl.h>
00068 #endif
00069 #ifdef STLSOFT_CF_PRAGMA_ONCE_SUPPORT
00070 # pragma once
00071 #endif
00072 #ifndef WINSTL_INCL_WINSTL_TOOLHELP_ERROR_HPP_SEQUENCE_EXCEPTION
00073 # include <winstl/toolhelp/error/exceptions.hpp>
00074 #endif
00075 #ifndef STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER
00076 # include <stlsoft/util/std/iterator_helper.hpp>
00077 #endif
00078
00079 #ifndef STLSOFT_INCL_H_TLHELP32
00080 # define STLSOFT_INCL_H_TLHELP32
00081 # include <tlhelp32.h>
00082 #endif
00083
00084
00085
00086
00087
00088 #ifndef _WINSTL_NO_NAMESPACE
00089 # if defined(_STLSOFT_NO_NAMESPACE) || \
00090 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00091
00092 namespace winstl
00093 {
00094 # else
00095
00096
00097 namespace stlsoft
00098 {
00099
00100 namespace winstl_project
00101 {
00102
00103 # endif
00104 #endif
00105
00106
00107
00108
00109
00110 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00111 template<ss_typename_param_k V>
00112 struct th_sequence_value_traits;
00113 #endif
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00133 template<ss_typename_param_k V>
00134 class th_sequence_const_iterator
00135 : public stlsoft_ns_qual(iterator_base)<winstl_ns_qual_std(input_iterator_tag)
00136 , V
00137 , ws_ptrdiff_t
00138 , V const*
00139 , V const&
00140 >
00141 {
00144 public:
00145 typedef th_sequence_const_iterator<V> class_type;
00146 typedef V value_type;
00147 typedef value_type const& const_reference;
00148 typedef HANDLE handle_type;
00149 typedef th_sequence_value_traits<value_type> traits_type;
00151
00154 public:
00156 th_sequence_const_iterator()
00157 : m_snapshot(traits_type::null_handle())
00158 {
00159
00160 }
00162 th_sequence_const_iterator(handle_type snapshot)
00163 : m_snapshot(snapshot)
00164 {
00165 WINSTL_ASSERT(m_snapshot != traits_type::invalid_handle());
00166
00167 ::memset(&m_value, 0, sizeof(m_value));
00168 m_value.dwSize = sizeof(m_value);
00169
00170 bool ok = traits_type::first(m_snapshot, m_value);
00171 if( !ok )
00172 {
00173 m_snapshot = traits_type::null_handle();
00174 }
00175 }
00177 th_sequence_const_iterator(class_type const& rhs)
00178 : m_snapshot(rhs.m_snapshot)
00179 , m_value (rhs.m_value)
00180 {
00181 WINSTL_ASSERT(m_snapshot != traits_type::invalid_handle());
00182 }
00183
00184
00185
00187
00190 public:
00191 const_reference operator *() const
00192 {
00193 return m_value;
00194 }
00195
00196 class_type& operator ++()
00197 {
00198 bool ok = traits_type::next(m_snapshot, m_value);
00199 if( !ok )
00200 m_snapshot = traits_type::null_handle();
00201
00202 return *this;
00203 }
00204
00205 class_type operator ++(int)
00206 {
00207 class_type rhs(*this);
00208 ++*this;
00209 return rhs;
00210 }
00211
00212 bool equal(class_type const& rhs) const
00213 {
00214 WINSTL_MESSAGE_ASSERT("Attempting to compare two non-endpoint iterators from different ranges", (NULL == m_snapshot || NULL == rhs.m_snapshot || m_snapshot == rhs.m_snapshot));
00215
00216 return m_snapshot == rhs.m_snapshot;
00217 }
00218
00219 bool operator ==(class_type const& rhs) const
00220 {
00221 return equal(rhs);
00222 }
00223
00224 bool operator !=(class_type const& rhs) const
00225 {
00226 return !equal(rhs);
00227 }
00229
00232 private:
00233 handle_type m_snapshot;
00234 value_type m_value;
00236 };
00237
00243 template<typename V>
00244 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00245 struct sequence_doesnt_use_pid;
00246 #else
00247 struct sequence_doesnt_use_pid {};
00248 #endif
00249
00254 template< ss_typename_param_k V
00255 , ss_typename_param_k E = throw_exception_policy<toolhelp_exception>
00256 >
00257 class th_sequence_base
00258 {
00261 public:
00263 typedef th_sequence_base<V> class_type;
00265 typedef th_sequence_const_iterator<V> const_iterator;
00267 typedef ss_typename_type_k const_iterator::value_type value_type;
00269 typedef ss_typename_type_k const_iterator::reference reference;
00271 typedef ss_typename_type_k const_iterator::const_reference const_reference;
00273 typedef ss_typename_type_k const_iterator::handle_type handle_type;
00275 typedef ss_typename_type_k const_iterator::traits_type traits_type;
00277 typedef E exception_policy;
00279 typedef ws_size_t size_type;
00281
00284 public:
00286 th_sequence_base()
00287 : m_snapshot(::CreateToolhelp32Snapshot(
00288 traits_type::flag()
00289 , 0
00290 ))
00291 {
00292 verify_construction();
00293 }
00294
00296 #if !defined(STLSOFT_COMPILER_IS_MSVC) || \
00297 _MSC_VER > 1200
00298 th_sequence_base(DWORD process_id, DWORD = sizeof(sequence_doesnt_use_pid<value_type>))
00299 #else
00300 th_sequence_base(DWORD process_id)
00301 #endif
00302 : m_snapshot(::CreateToolhelp32Snapshot(
00303 traits_type::flag()
00304 , process_id
00305 ))
00306 {
00307 verify_construction();
00308 }
00309
00311 ~th_sequence_base()
00312 {
00313 if(traits_type::invalid_handle() != m_snapshot)
00314 {
00315 ::CloseHandle(m_snapshot);
00316 }
00317 }
00319
00322 public:
00326 const_iterator begin() const
00327 {
00328 return const_iterator(m_snapshot);
00329 }
00333 const_iterator end() const
00334 {
00335 return const_iterator();
00336 }
00338
00341 public:
00343 bool empty() const
00344 {
00345
00346
00347
00348 return (begin() == end());
00349 }
00351
00354 private:
00355 handle_type m_snapshot;
00357
00360 private:
00361 th_sequence_base(class_type const&);
00362 class_type& operator =(class_type const&);
00364
00365 void verify_construction()
00366 {
00367 if(traits_type::invalid_handle() == m_snapshot)
00368 {
00369 exception_policy xp;
00370 DWORD dwErr = ::GetLastError();
00371
00372 xp(traits_type::create_snapshot_fail_message(), dwErr);
00373 }
00374 }
00375 };
00376
00377
00378
00379 #ifndef _WINSTL_NO_NAMESPACE
00380 # if defined(_STLSOFT_NO_NAMESPACE) || \
00381 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00382 }
00383 # else
00384 }
00385 }
00386 # endif
00387 #endif
00388
00389
00390
00391 #endif
00392
00393