#ifndef ARENA_PINMUX_POINTER_H
#define ARENA_PINMUX_POINTER_H
//----------------------------------------------------------------------------
///
/// \file   pointer.h
///
/// \brief  Pointer to reference and peripheries
///
/// \date   20180129
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Includes -----------------------------------------------------------------

//#include <peripheryTrivia.h>

//---General--------------------------


//---Declaration --------------------------------------------------------------

class CPeriphery;
class CReference;
class CConstructor;


template <class T>
class CPointer
{
      T *ptr;
   public:
      CPointer()
      {
         ptr=nullptr;
      }
      CPointer(T *p)
      {
         ptr=p;
      }
      CPointer(const CPointer<T> &r)
      {
         ptr=r.ptr;
      }
      void check() const
      {
         if(ptr==nullptr)
         {
            qFatal("Empty pointer");
         }
      }
      T* data() const
      {
         check();
         return(ptr);
      }
      T* dataNoCheck() const
      {
         return(ptr);
      }
      T *operator->() const
      {
         check();
         return(ptr);
      }
      CPointer<T> &operator=(const CPointer<T> &r)
      {
         ptr=r.ptr;
         return(*this);
      }
      CPointer<T> &operator=(T *pr)
      {
         ptr=pr;
         return(*this);
      }
      CPointer<T> &operator=(std::nullptr_t ptr2)
      {
         ptr=ptr2;
         return(*this);
      }
      operator CPointer<const T>()
      {
         CPointer<const T> var(ptr);
         return(var);
      }
      bool operator!=(std::nullptr_t ptr2) const
      {
         return(ptr!=ptr2);
      }
      bool operator!=(const T *ptr2) const
      {
         return(ptr!=ptr2);
      }
      bool operator==(std::nullptr_t ptr2) const
      {
         return(ptr==ptr2);
      }
      bool operator==(const CPointer<T> &ptr2) const
      {
         return(ptr==ptr2.ptr);
      }
      operator bool() const
      {
         return(ptr!=nullptr);
      }
      T &operator *() const
      {
         check();
         return(*ptr);
      }
      void clear()
      {
         ptr=nullptr;
      }
};

typedef CPointer<CPeriphery> CPeripheryPtr;
typedef CPointer<CReference> CReferencePtr;
typedef CPointer<const CPeriphery> CPeripheryConstPtr;
typedef CPointer<const CReference> CReferenceConstPtr;
typedef CPeriphery &CPeripheryRef;
typedef CReference &CReferenceRef;


//---fin----------------------------------------------------------------------
#endif // ? ! ARENA_PINMUX_POINTER_H
