Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef __OPTIMIZE__
- void __null_pointer_error() __attribute__((error ("Unsafe access to pointer")));
- #else
- #warning "You should turn on optimization!"
- #include <stdexcept>
- class NullPointerException : public std::exception {
- public:
- inline const char *what() throw() { return "NullPointerException"; }
- inline ~NullPointerException() throw() {}
- };
- void __null_pointer_error() __attribute__((warning ("Pointer access without optimization!")));
- inline void __null_pointer_error() { throw NullPointerException(); }
- #endif
- template <typename T>
- class Ptr {
- public:
- inline Ptr(T *ptr = NULL) : mPtr(ptr) {}
- inline operator bool() const __attribute__((const)) {
- return !!mPtr;
- }
- inline T *operator->() const __attribute__((const)) {
- if(!*this) throw __null_pointer_error();
- return mPtr;
- }
- inline T &operator*() const __attribute__((const)) {
- if(!*this) __null_pointer_error();
- return *mPtr;
- }
- inline Ptr<T> &operator=(const Ptr &other) __attribute__((const)) {
- mPtr = other.mPtr;
- return *this;
- }
- inline Ptr<T> &operator=(T *other) __attribute__((const)) {
- mPtr = other;
- return *this;
- }
- private:
- T *mPtr;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement