-
-
Notifications
You must be signed in to change notification settings - Fork 849
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
65 changed files
with
473 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
#pragma once | ||
#include <memory> | ||
|
||
#include "../macros.hpp" | ||
class CWorkspace; | ||
|
||
typedef std::shared_ptr<CWorkspace> PHLWORKSPACE; | ||
typedef SP<CWorkspace> PHLWORKSPACE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
#pragma once | ||
|
||
#include <typeinfo> | ||
|
||
namespace CSharedPointer_ { | ||
struct impl { | ||
/* strong refcount */ | ||
unsigned int ref = 0; | ||
/* weak refcount */ | ||
unsigned int weak = 0; | ||
|
||
/* the actual stored data */ | ||
void* data = nullptr; | ||
/* if the destructor was called, | ||
creating shared_ptrs is no longer valid */ | ||
bool destroying = false; | ||
}; | ||
}; | ||
|
||
template <typename T> | ||
class CSharedPointer { | ||
public: | ||
template <typename X> | ||
using validHierarchy = typename std::enable_if<std::is_assignable<T*, X*>::value>; | ||
|
||
/* creates a new shared pointer managing a resource | ||
avoid calling. Could duplicate ownership. Prefer makeShared */ | ||
explicit CSharedPointer(T* object) { | ||
impl_ = new CSharedPointer_::impl(); | ||
impl_->data = object; | ||
increment(); | ||
} | ||
|
||
/* creates a shared pointer from a reference */ | ||
template <typename U, typename = validHierarchy<U>> | ||
explicit CSharedPointer(const CSharedPointer<U>& ref) { | ||
impl_ = ref.impl_; | ||
increment(); | ||
} | ||
|
||
/* allows weakPointer to create from an impl */ | ||
explicit CSharedPointer(CSharedPointer_::impl* implementation) { | ||
impl_ = implementation; | ||
increment(); | ||
} | ||
|
||
/* creates an empty shared pointer with no implementation */ | ||
CSharedPointer() { | ||
; // empty | ||
} | ||
|
||
/* creates an empty shared pointer with no implementation */ | ||
CSharedPointer(std::nullptr_t) { | ||
; // empty | ||
} | ||
|
||
~CSharedPointer() { | ||
// we do not decrement here, | ||
// because we want to preserve the pointer | ||
// in case this is the last owner. | ||
if (impl_ && impl_->ref == 1) | ||
destroyImpl(); | ||
else | ||
decrement(); | ||
} | ||
|
||
template <typename U, typename = validHierarchy<U>> | ||
CSharedPointer<T>& operator=(const CSharedPointer<U>& rhs) { | ||
if (impl_ == rhs.impl_) | ||
return *this; | ||
|
||
decrement(); | ||
impl_ = rhs.impl_; | ||
increment(); | ||
} | ||
|
||
operator bool() const { | ||
return impl_ && impl_->data; | ||
} | ||
|
||
bool operator==(const CSharedPointer& rhs) const { | ||
return impl_ == rhs.impl_; | ||
} | ||
|
||
T* operator->() const { | ||
return get(); | ||
} | ||
|
||
T& operator*() const { | ||
return *get(); | ||
} | ||
|
||
void reset() { | ||
decrement(); | ||
impl_ = nullptr; | ||
} | ||
|
||
T* get() const { | ||
return (T*)(impl_ ? impl_->data : nullptr); | ||
} | ||
|
||
unsigned int strongRef() const { | ||
return impl_ ? impl_->ref : 0; | ||
} | ||
|
||
CSharedPointer_::impl* impl_ = nullptr; | ||
|
||
private: | ||
/* | ||
no-op if there is no impl_ | ||
may delete the stored object if ref == 0 | ||
may delete and reset impl_ if ref == 0 and weak == 0 | ||
*/ | ||
void decrement() { | ||
if (!impl_) | ||
return; | ||
|
||
impl_->ref--; | ||
|
||
// if ref == 0, we can destroy impl | ||
if (impl_->ref == 0) | ||
destroyImpl(); | ||
} | ||
/* no-op if there is no impl_ */ | ||
void increment() { | ||
if (!impl_) | ||
return; | ||
|
||
impl_->ref++; | ||
} | ||
|
||
/* destroy the pointed-to object | ||
if able, will also destroy impl */ | ||
void destroyImpl() { | ||
// first, we destroy the data, but keep the pointer. | ||
// this way, weak pointers will still be able to | ||
// reference and use, but no longer create shared ones. | ||
impl_->destroying = true; | ||
delete (T*)impl_->data; | ||
// now, we can reset the data and call it a day. | ||
impl_->data = nullptr; | ||
impl_->destroying = false; | ||
|
||
// check for weak refs, if zero, we can also delete impl_ | ||
if (impl_->weak == 0) | ||
delete impl_; | ||
} | ||
}; | ||
|
||
template <typename U, typename... Args> | ||
static CSharedPointer<U> makeShared(Args&&... args) { | ||
auto p = CSharedPointer<U>(new U(std::forward<Args>(args)...)); | ||
return p; | ||
} |
Oops, something went wrong.