Skip to content

Commit

Permalink
Added LogCommon, from Instalog 8168f6f7cc90f0b057834a1e9ae2457b27868c0b
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyONeal committed Jul 13, 2013
1 parent 62ba015 commit 6a8f89a
Show file tree
Hide file tree
Showing 61 changed files with 31,084 additions and 0 deletions.
157 changes: 157 additions & 0 deletions LogCommon/Com.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#include "pch.hpp"
#include "Com.hpp"

namespace Instalog { namespace SystemFacades {

Com::Com(DWORD threadingType /* = COINIT_APARTMENTTHREADED */)
{
ThrowIfFailed(::CoInitializeEx(nullptr, threadingType));
ThrowIfFailed(::CoInitializeSecurity(
NULL,
-1, // COM negotiates service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
));
}

Com::~Com()
{
::CoUninitialize();
}

UniqueBstr::UniqueBstr() : wrapped(nullptr)
{ }

UniqueBstr::UniqueBstr( std::wstring const& source )
{
if (source.empty())
{
this->wrapped = nullptr;
return;
}

assert(source.size() <= std::numeric_limits<UINT>::max());
UINT lengthPrefix = static_cast<UINT>(source.size());
this->wrapped = ::SysAllocStringLen(source.data(), lengthPrefix);
if (this->wrapped == nullptr)
{
throw std::bad_alloc();
}
}

UniqueBstr::UniqueBstr( UniqueBstr && other ) : wrapped(other.wrapped)
{
other.wrapped = nullptr;
}

BSTR UniqueBstr::AsInput() const
{
return this->wrapped;
}

BSTR* UniqueBstr::AsTarget()
{
::SysFreeString(this->wrapped);
this->wrapped = nullptr;
return &this->wrapped;
}

uint32_t UniqueBstr::Length()
{
return ::SysStringLen(this->wrapped);
}

std::wstring UniqueBstr::AsString()
{
return std::wstring(this->wrapped, this->Length());
}

UniqueBstr::~UniqueBstr()
{
::SysFreeString(this->wrapped);
}

#pragma warning(push)
#pragma warning(disable: 4189) // local variable is initialized but not referenced
// (The assert gets compiled out in release mode leading to the spurrious warning)
void UniqueVariant::Destroy()
{
HRESULT result = ::VariantClear(&wrappedVariant);
assert(result == S_OK);
}
#pragma warning(pop)

UniqueVariant::UniqueVariant()
{
::VariantInit(&this->wrappedVariant);
}

VARIANT* UniqueVariant::PassAsOutParameter()
{
this->Destroy();
::VariantInit(&this->wrappedVariant);
return &this->wrappedVariant;
}

VARIANT& UniqueVariant::Get()
{
return this->wrappedVariant;
}

VARIANT const& UniqueVariant::Get() const
{
return this->wrappedVariant;
}

std::wstring UniqueVariant::AsString() const
{
UniqueVariant bstrVariant;
ThrowIfFailed(::VariantChangeType(bstrVariant.PassAsOutParameter(), &this->wrappedVariant, 0, VT_BSTR));
BSTR asBstr = bstrVariant.Get().bstrVal;
return std::wstring(asBstr, ::SysStringLen(asBstr));
}

UINT UniqueVariant::AsUint() const
{
UniqueVariant uintVariant;
ThrowIfFailed(::VariantChangeType(uintVariant.PassAsOutParameter(), &this->wrappedVariant, 0, VT_UINT));
return uintVariant.Get().uintVal;
}

ULONG UniqueVariant::AsUlong() const
{
UniqueVariant ulongVariant;
ThrowIfFailed(::VariantChangeType(ulongVariant.PassAsOutParameter(), &this->wrappedVariant, 0, VT_UI4));
return ulongVariant.Get().ulVal;
}

ULONGLONG UniqueVariant::AsUlonglong() const
{
UniqueVariant ulongVariant;
ThrowIfFailed(::VariantChangeType(ulongVariant.PassAsOutParameter(), &this->wrappedVariant, 0, VT_UI8));
return ulongVariant.Get().ullVal;
}

bool UniqueVariant::AsBool() const
{
UniqueVariant booleanVariant;
ThrowIfFailed(::VariantChangeType(booleanVariant.PassAsOutParameter(), &this->wrappedVariant, 0, VT_BOOL));
return booleanVariant.Get().boolVal != 0;
}

bool UniqueVariant::IsNull() const
{
return this->wrappedVariant.vt == VT_NULL;
}

UniqueVariant::~UniqueVariant()
{
this->Destroy();
}

}} // namespace Instalog::SystemFacades
123 changes: 123 additions & 0 deletions LogCommon/Com.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright © 2012 Jacob Snyder, Billy O'Neal III
// This is under the 2 clause BSD license.
// See the included LICENSE.TXT file for more details.
#pragma once
#include <cstring>
#include <boost/noncopyable.hpp>
#include <Windows.h>
#include "Win32Exception.hpp"

namespace Instalog { namespace SystemFacades {

struct Com : boost::noncopyable
{
/// <summary>Initializes a new instance of the Com class. Initializes the Component Object Model.</summary>
Com(DWORD threadingType = COINIT_APARTMENTTHREADED);

/// <summary>Finalizes an instance of the Com class. Shuts down Component Object Model.</summary>
~Com();
};

/// <summary>
/// Unique com pointer. Similar to ATL's CComPtr, except allows only a unique reference with move
/// semantics, and plays nicer with auto.
/// </summary>
/// <typeparam name="T">The COM interface wrapped by this UniqueComPtr.</typeparam>
template <typename T>
class UniqueComPtr
{
T* pointer;
UniqueComPtr(UniqueComPtr const&);
UniqueComPtr& operator=(UniqueComPtr const&);
public:
T* operator->()
{
return this->pointer;
}

UniqueComPtr()
: pointer(nullptr)
{ }

UniqueComPtr(T* wrap)
: pointer(wrap)
{ }

UniqueComPtr(UniqueComPtr && other)
: pointer(other.pointer)
{
other.pointer = nullptr;
}

~UniqueComPtr()
{
if (this->pointer != nullptr)
{
this->pointer->Release();
}
}

static UniqueComPtr<T> Create(REFCLSID clsid, DWORD clsCtx)
{
T* resultPtr;
ThrowIfFailed(::CoCreateInstance(clsid, nullptr, clsCtx, __uuidof(T), reinterpret_cast<void**>(&resultPtr)));
return UniqueComPtr<T>(resultPtr);
}

T* Get()
{
return this->pointer;
}

T const* Get() const
{
return this->pointer;
}

T** PassAsOutParameter()
{
if (this->pointer != nullptr)
{
this->pointer->Release();
this->pointer = nullptr;
}
return &this->pointer;
}
};

class UniqueBstr
{
BSTR wrapped;
UniqueBstr(UniqueBstr const&);
UniqueBstr& operator=(UniqueBstr const&);
public:
UniqueBstr();
UniqueBstr(std::wstring const& source);
UniqueBstr(UniqueBstr && other);
BSTR AsInput() const;
BSTR* AsTarget();
uint32_t Length();
std::wstring AsString();
~UniqueBstr();
};

class UniqueVariant
{
VARIANT wrappedVariant;
UniqueVariant(UniqueVariant const&);
UniqueVariant& operator=(UniqueVariant const&);
void Destroy();
public:
UniqueVariant();
VARIANT* PassAsOutParameter();
VARIANT& Get();
VARIANT const& Get() const;
std::wstring AsString() const;
UINT AsUint() const;
ULONG AsUlong() const;
ULONGLONG AsUlonglong() const;
bool AsBool() const;
bool IsNull() const;
~UniqueVariant();
};
}}
Binary file added LogCommon/CommonWhitelists.rc
Binary file not shown.
Loading

0 comments on commit 6a8f89a

Please sign in to comment.