Commit 8c9e395b authored by Ilya Prokhorov's avatar Ilya Prokhorov

Not null implementation

parent cd419d46
......@@ -14,6 +14,9 @@ template <typename T>
class SharedNotNullPointer {
public:
SharedNotNullPointer() {
}
SharedNotNullPointer(shared_ptr<T> rawSharedPointer) {
if (rawSharedPointer.get() == nullptr) {
throwRuntimeException("Excepted not null, but got null shared_ptr at SharedNotNullPointer constructor");
......@@ -21,11 +24,15 @@ public:
this->rawSharedPointer = rawSharedPointer;
};
shared_ptr<T> operator->() {
T* operator->() const {
return get();
}
T* get() const {
if (rawSharedPointer.get() == nullptr) {
throwRuntimeException("Excepted not null, but got null shared_ptr at SharedNotNullPointer getter");
}
return rawSharedPointer;
return rawSharedPointer.get();
}
private:
......@@ -34,10 +41,10 @@ private:
};
template <typename T, typename... Args>
shared_ptr<SharedNotNullPointer<T> >
SharedNotNullPointer<T>
MakeSharedNotNullPointer(Args&&... args) {
auto rawSharedPointer = make_shared<T>(forward<Args>(args)...);
auto sharedNotNullPointer = make_shared<SharedNotNullPointer<T> >(rawSharedPointer);
auto sharedNotNullPointer = SharedNotNullPointer<T>(rawSharedPointer);
return sharedNotNullPointer;
};
......@@ -45,14 +52,20 @@ MakeSharedNotNullPointer(Args&&... args) {
namespace Shortcuts {
template <typename T>
using NotNull = shared_ptr<SharedNotNullPointer<T> >;
using NotNull = SharedNotNullPointer<T>;
template <typename T, typename... Args>
shared_ptr<SharedNotNullPointer<T> >
SharedNotNullPointer<T>
make(Args&&... args) {
return MakeSharedNotNullPointer<T>(forward<Args>(args)...);
};
template <typename T, typename... Args>
SharedNotNullPointer<T>
toNotNull(shared_ptr<T> rawSharedPointer) {
return SharedNotNullPointer(rawSharedPointer);
};
};
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment