Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

DPtr Magic #74

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libGitWrap/Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ namespace Git
*/
bool BasePrivate::isValid(Result& r, const BasePrivate* d)
{
if ( Q_LIKELY(d) ) {
return d->isValidObject( r );
if (Q_LIKELY(d)) {
return d->isValidObject(r);
}
r.setInvalidObject();
return false;
Expand Down
67 changes: 66 additions & 1 deletion libGitWrap/Base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#pragma once

#include "libGitWrap/GitWrap.hpp"
#include "libGitWrap/Result.hpp"

namespace Git
{
Expand Down Expand Up @@ -113,6 +113,71 @@ namespace Git
// because QExplicitlySharedDataPointer and GitPtr actually do give a shit about const
// correctness.
}

template<typename T>
struct DPtrT
{
typedef typename T::Private DPtr;

DPtrT(T* that)
: d(static_cast<DPtr*>(that->mData))
{
}

DPtrT(T* that, Result& r)
: d(static_cast<DPtr*>(that->mData))
{
if (Q_LIKELY(d)) {
if (d->isValidObject(r)) {
return;
}
}
r.setInvalidObject();
d = nullptr;
}

DPtr* operator->() { return d; }
const DPtr* operator->() const { return d; }
operator DPtr*() { return d; }
operator const DPtr*() const { return d; }

private:
DPtr* d;
};

template<typename T>
struct DPtrT<const T>
{
typedef typename T::Private DPtr;

DPtrT(const T* that)
: d(static_cast<const DPtr*>(that->mData))
{
}

DPtrT(const T* that, Result& r)
: d(static_cast<const DPtr*>(that->mData))
{
if (Q_LIKELY(d)) {
if (d->isValidObject(r)) {
return;
}
}
r.setInvalidObject();
d = nullptr;
}

const DPtr* operator->() const { return d; }
operator const DPtr*() const { return d; }

DPtr* unConst() const {
return const_cast<DPtr*>(d);
}

private:
const DPtr* d;
};

};

}
Loading