📄 pid_sequence.hpp
字号:
size_type size() const;
/// @}
/// \name System Traits
/// @{
public:
/// \brief The process identifier of the Idle process
///
/// \note The Idle process is a pseudo-process. You should not attempt to
/// manipulate it using the process control functions
static value_type idleProcessId()
{
return 0;
}
/// \brief The process identifier of the System process
///
/// \note The System process is a pseudo-process. You should not attempt to
/// manipulate it using the process control functions
static value_type systemProcessId()
{
ws_uint32_t major = system_version::major();
ws_uint32_t minor = system_version::minor();
if(4 == major)
{
return 2; // NT 4
}
else
{
if( 5 == major &&
0 == minor)
{
return 8; // Win2K
}
else if(5 == major &&
1 == minor)
{
return 4; // WinXP
}
else
{
return 4; // Longhorn and above - this value is a guess!!
}
}
}
/// @}
/// \name Members
/// @{
private:
typedef stlsoft_ns_qual(auto_buffer_old)< value_type
, allocator_type
, 64
> buffer_type_;
buffer_type_ m_pids;
/// @}
/// \name Not to be implemented
/// @{
private:
class_type& operator =(class_type const&);
/// @}
};
////////////////////////////////////////////////////////////////////////////
// Unit-testing
#ifdef STLSOFT_UNITTEST
# include "./unittest/pid_sequence_unittest_.h"
#endif /* STLSOFT_UNITTEST */
/* /////////////////////////////////////////////////////////////////////////
* Implementation
*/
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
inline pid_sequence::pid_sequence(ws_uint32_t flags)
: m_pids(buffer_type_::internal_size())
{
DWORD cbReturned;
for(;;)
{
#if defined(_PSAPI_H_) || \
defined(_PSAPI_H)
if(!::EnumProcesses(&m_pids[0], sizeof(value_type) * m_pids.size(), &cbReturned))
#else /* ? psapi */
if(!dl_call<BOOL>("PSAPI.DLL", "stdcall:EnumProcesses", &m_pids[0], sizeof(value_type) * m_pids.size(), &cbReturned))
#endif /* psapi */
{
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
STLSOFT_THROW_X(windows_exception("Failed to enumerate processes", ::GetLastError()));
#else /* ? STLSOFT_CF_EXCEPTION_SUPPORT */
m_pids.resize(0);
break;
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}
else
{
const size_type n = cbReturned / sizeof(value_type);
if(n < m_pids.size())
{
m_pids.resize(n);
break;
}
else
{
const size_type size = m_pids.size();
m_pids.resize(1); // Read "Extended STL, volume 1" to find out what this is for
if(!m_pids.resize(2 * size))
{
// This will only ever be executed when compiled in the
// absence of throwing bad_alloc on memory exhaustion
m_pids.resize(0);
break;
}
}
}
}
if(flags & (elideIdle | elideSystem))
{
value_type *begin = &*m_pids.begin();
value_type *end = &*m_pids.end();
value_type *pIdle = (flags & elideIdle) ? stlsoft_ns_qual_std(find)(begin, end, idleProcessId()) : end;
value_type *pSystem = (flags & elideSystem) ? stlsoft_ns_qual_std(find)(begin, end, systemProcessId()) : end;
// Optimise for the special case where idle is [0] and system is [1]
if( end != pIdle &&
end != pSystem &&
pSystem == pIdle + 1)
{
pod_move(pSystem + 1, end, begin);
m_pids.resize(m_pids.size() - 2);
}
else
{
if(end != pIdle)
{
pod_move(pIdle + 1, end, pIdle);
m_pids.resize(m_pids.size() - 1);
}
if(end != pSystem)
{
pod_move(pSystem + 1, end, pSystem);
m_pids.resize(m_pids.size() - 1);
}
}
}
if(flags & sort)
{
stlsoft_ns_qual_std(sort)(m_pids.begin(), m_pids.end());
}
}
inline pid_sequence::pid_sequence(pid_sequence const& rhs)
: m_pids(rhs.m_pids.size())
{
stlsoft_ns_qual_std(copy)(rhs.m_pids.begin(), rhs.m_pids.end(), m_pids.begin());
}
inline pid_sequence::~pid_sequence() stlsoft_throw_0()
{}
inline pid_sequence::const_iterator pid_sequence::begin() const
{
return &*m_pids.begin();
}
inline pid_sequence::const_iterator pid_sequence::end() const
{
return &*m_pids.end();
}
#if defined(STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT)
inline pid_sequence::const_reverse_iterator pid_sequence::rbegin() const
{
return const_reverse_iterator(end());
}
inline pid_sequence::const_reverse_iterator pid_sequence::rend() const
{
return const_reverse_iterator(begin());
}
#endif /* STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT */
inline pid_sequence::const_reference pid_sequence::operator [](pid_sequence::size_type index) const
{
WINSTL_MESSAGE_ASSERT("Index out of range", index < size());
return m_pids[index];
}
inline ws_bool_t pid_sequence::empty() const
{
return m_pids.empty();
}
inline pid_sequence::size_type pid_sequence::size() const
{
return m_pids.size();
}
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace winstl
# else
} // namespace winstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* !WINSTL_INCL_WINSTL_SYSTEM_HPP_PID_SEQUENCE */
/* ////////////////////////////////////////////////////////////////////// */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -