-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathObjectIterator.cpp
123 lines (94 loc) · 2.89 KB
/
ObjectIterator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
////////////////////////////////////////////////////////////////////////////////
//! \file ObjectIterator.cpp
//! \brief The ObjectIterator class definition.
//! \author Chris Oldwood
#include "Common.hpp"
#include "ObjectIterator.hpp"
#include "Exception.hpp"
#include <Core/BadLogicException.hpp>
#ifndef _MSC_VER
WCL_DECLARE_IFACETRAITS(IWbemClassObject, IID_IWbemClassObject);
WCL_DECLARE_IFACETRAITS(IEnumWbemClassObject, IID_IEnumWbemClassObject);
#endif
namespace WMI
{
////////////////////////////////////////////////////////////////////////////////
//! Constructor for the End iterator.
ObjectIterator::ObjectIterator()
: m_enumerator()
, m_connection()
, m_value()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Constructor for the Begin iterator.
ObjectIterator::ObjectIterator(IEnumWbemClassObjectPtr enumerator, const Connection& connection)
: m_enumerator(enumerator)
, m_connection(connection)
, m_value()
{
increment();
}
////////////////////////////////////////////////////////////////////////////////
//! Destructor.
ObjectIterator::~ObjectIterator()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Dereference operator.
const Object& ObjectIterator::operator*() const
{
if (m_value.get() == nullptr)
throw Core::BadLogicException(TXT("Attempted to dereference end iterator"));
return *m_value;
}
////////////////////////////////////////////////////////////////////////////////
//! Pointer-to-member operator.
const Object* ObjectIterator::operator->() const
{
if (m_value.get() == nullptr)
throw Core::BadLogicException(TXT("Attempted to dereference end iterator"));
return m_value.get();
}
////////////////////////////////////////////////////////////////////////////////
//! Compare to another iterator for equivalence.
bool ObjectIterator::equals(const ObjectIterator& rhs) const
{
// Comparing End iterators?
if (m_enumerator.get() == nullptr)
return (rhs.m_enumerator.get() == nullptr);
return (m_enumerator.get() == rhs.m_enumerator.get());
}
////////////////////////////////////////////////////////////////////////////////
//! Move the iterator forward.
void ObjectIterator::increment()
{
ASSERT(m_enumerator.get() != nullptr);
// Request the next item.
IWbemClassObjectPtr value;
ULONG avail = 0;
HRESULT result = m_enumerator->Next(WBEM_INFINITE, 1, AttachTo(value), &avail);
if (FAILED(result))
throw Exception(result, m_enumerator, TXT("Failed to advance the WMI object enumerator"));
// Continued enumeration?
if (avail != 0)
{
ASSERT(avail == 1);
m_value.reset(new Object(value, m_connection));
}
// End reached.
else
{
ASSERT(result == S_FALSE);
reset();
}
}
////////////////////////////////////////////////////////////////////////////////
//! Move the iterator to the End.
void ObjectIterator::reset()
{
m_value.reset();
m_enumerator.Release();
}
//namespace WMI
}