#ifndef LLVM_TOOLS_LLVM_PDBDUMP_COMEXTRAS_H
#define LLVM_TOOLS_LLVM_PDBDUMP_COMEXTRAS_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/ConvertUTF.h"
#include <tuple>
namespace llvm {
template <typename F> struct function_traits;
#if LLVM_HAS_VARIADIC_TEMPLATES
template <typename R, typename... Args>
struct function_traits<R (*)(Args...)> : public function_traits<R(Args...)> {};
template <typename C, typename R, typename... Args>
struct function_traits<R (__stdcall C::*)(Args...)> {
using args_tuple = std::tuple<Args...>;
};
#else
template <typename C, typename R, typename A1, typename A2, typename A3>
struct function_traits<R (__stdcall C::*)(A1, A2, A3)> {
typedef std::tuple<A1, A2, A3> args_tuple;
};
template <typename C, typename R, typename A1, typename A2, typename A3,
typename A4, typename A5>
struct function_traits<R (__stdcall C::*)(A1, A2, A3, A4, A5)> {
typedef std::tuple<A1, A2, A3, A4, A5> args_tuple;
};
#endif
template <class FuncTraits, std::size_t arg> struct function_arg {
typedef
typename std::tuple_element<arg, typename FuncTraits::args_tuple>::type
type;
};
template <class T> struct remove_double_pointer {};
template <class T> struct remove_double_pointer<T **> { typedef T type; };
namespace sys {
namespace windows {
template <class EnumeratorType, std::size_t ArgIndex> class com_iterator {
using FunctionTraits = function_traits<decltype(&EnumeratorType::Next)>;
typedef typename function_arg<FunctionTraits, ArgIndex>::type FuncArgType;
typedef typename remove_double_pointer<FuncArgType>::type EnumDataType;
CComPtr<EnumeratorType> EnumeratorObject;
CComPtr<EnumDataType> CurrentItem;
public:
typedef CComPtr<EnumDataType> value_type;
typedef std::ptrdiff_t difference_type;
typedef value_type *pointer_type;
typedef value_type &reference_type;
typedef std::forward_iterator_tag iterator_category;
explicit com_iterator(CComPtr<EnumeratorType> Enumerator,
CComPtr<EnumDataType> Current)
: EnumeratorObject(Enumerator), CurrentItem(Current) {}
com_iterator() {}
com_iterator &operator++() {
CurrentItem.Release();
ULONG Count = 0;
HRESULT hr = EnumeratorObject->Next(1, &CurrentItem, &Count);
if (FAILED(hr) || Count == 0)
*this = com_iterator();
return *this;
}
value_type operator*() { return CurrentItem; }
bool operator==(const com_iterator &other) const {
return (EnumeratorObject == other.EnumeratorObject) &&
(CurrentItem == other.CurrentItem);
}
bool operator!=(const com_iterator &other) const { return !(*this == other); }
com_iterator &operator=(const com_iterator &other) {
EnumeratorObject = other.EnumeratorObject;
CurrentItem = other.CurrentItem;
return *this;
}
};
template <class EnumeratorType, std::size_t ArgIndex> class com_enumerator {
typedef function_traits<decltype(&EnumeratorType::Next)> FunctionTraits;
typedef typename function_arg<FunctionTraits, ArgIndex>::type FuncArgType;
typedef typename remove_double_pointer<FuncArgType>::type EnumDataType;
CComPtr<EnumeratorType> EnumeratorObject;
public:
com_enumerator(CComPtr<EnumeratorType> Enumerator)
: EnumeratorObject(Enumerator) {}
com_iterator<EnumeratorType, ArgIndex> begin() {
if (!EnumeratorObject)
return end();
EnumeratorObject->Reset();
ULONG Count = 0;
CComPtr<EnumDataType> FirstItem;
HRESULT hr = EnumeratorObject->Next(1, &FirstItem, &Count);
return (FAILED(hr) || Count == 0) ? end()
: com_iterator<EnumeratorType, ArgIndex>(
EnumeratorObject, FirstItem);
}
com_iterator<EnumeratorType, ArgIndex> end() {
return com_iterator<EnumeratorType, ArgIndex>();
}
};
template <class EnumeratorType> class com_data_record_iterator {
public:
typedef llvm::ArrayRef<uint8_t> value_type;
typedef std::ptrdiff_t difference_type;
typedef value_type *pointer_type;
typedef value_type &reference_type;
typedef std::forward_iterator_tag iterator_category;
explicit com_data_record_iterator(CComPtr<EnumeratorType> enumerator)
: Enumerator(enumerator), CurrentRecord(0) {
if (enumerator)
enumerator->Reset();
if (!ReadNextRecord())
*this = com_data_record_iterator();
}
com_data_record_iterator() {}
com_data_record_iterator &operator++() {
++CurrentRecord;
if (!ReadNextRecord())
*this = com_data_record_iterator();
return *this;
}
value_type operator*() {
return llvm::ArrayRef<uint8_t>(RecordData.begin(), RecordData.end());
}
bool operator==(const com_data_record_iterator &other) const {
return (Enumerator == other.Enumerator) &&
(CurrentRecord == other.CurrentRecord);
}
bool operator!=(const com_data_record_iterator &other) const {
return !(*this == other);
}
private:
bool ReadNextRecord() {
RecordData.clear();
ULONG Count = 0;
DWORD RequiredBufferSize;
HRESULT hr = Enumerator->Next(1, 0, &RequiredBufferSize, nullptr, &Count);
if (hr == S_OK) {
RecordData.resize(RequiredBufferSize);
DWORD BytesRead = 0;
hr = Enumerator->Next(1, RequiredBufferSize, &BytesRead,
RecordData.data(), &Count);
}
if (hr != S_OK)
RecordData.clear();
return (hr == S_OK);
}
CComPtr<EnumeratorType> Enumerator;
uint32_t CurrentRecord;
llvm::SmallVector<uint8_t, 32> RecordData;
};
template <class EnumeratorType> class com_data_record_enumerator {
public:
com_data_record_enumerator(CComPtr<EnumeratorType> enumerator)
: Enumerator(enumerator) {}
com_data_record_iterator<EnumeratorType> begin() {
return com_data_record_iterator<EnumeratorType>(Enumerator);
}
com_data_record_iterator<EnumeratorType> end() {
LONG NumElts = 0;
HRESULT hr = Enumerator->get_Count(&NumElts);
return (FAILED(hr)) ? com_data_record_iterator<EnumeratorType>(Enumerator)
: com_data_record_iterator<EnumeratorType>();
}
private:
CComPtr<EnumeratorType> Enumerator;
};
template <class EnumeratorType>
com_enumerator<EnumeratorType, 1>
make_com_enumerator(CComPtr<EnumeratorType> Enumerator) {
return com_enumerator<EnumeratorType, 1>(Enumerator);
}
template <class EnumeratorType>
com_data_record_enumerator<EnumeratorType>
make_com_data_record_enumerator(CComPtr<EnumeratorType> Enumerator) {
return com_data_record_enumerator<EnumeratorType>(Enumerator);
}
inline bool BSTRToUTF8(BSTR String16, std::string &String8) {
UINT ByteLength = ::SysStringByteLen(String16);
char *Bytes = reinterpret_cast<char *>(String16);
String8.clear();
return llvm::convertUTF16ToUTF8String(ArrayRef<char>(Bytes, ByteLength),
String8);
}
} } }
#endif