63 lines
No EOL
1.5 KiB
C++
63 lines
No EOL
1.5 KiB
C++
#pragma once
|
|
|
|
#include <iostream>
|
|
#include "../util/debug.h"
|
|
|
|
|
|
template <typename Object> struct SmartPointer{
|
|
Object *ref;
|
|
int counter;
|
|
~SmartPointer(){
|
|
print("SmartPointer Desctructor called")
|
|
if(counter==0){
|
|
delete ref;
|
|
}
|
|
}
|
|
};
|
|
|
|
template <typename Object>
|
|
using SPointer = struct SmartPointer;
|
|
|
|
template <typename Object>
|
|
class Box
|
|
{
|
|
public:
|
|
Box(Object *); //constructor
|
|
~Box(); //destructor
|
|
Box(const Box&); //copy constructor
|
|
Object *operator->();
|
|
Object *operator*(); //deref operator
|
|
int getRefCounter(){
|
|
return this->sref->counter;
|
|
};
|
|
friend std::ostream &operator<<(std::ostream &stream, Box const &c)
|
|
{
|
|
return stream << "Boxed[v=" << *c.sref->ref << ",r=" << c.sref->ref << "]";
|
|
}
|
|
|
|
private:
|
|
SPointer<Object>* sref;
|
|
};
|
|
|
|
template <typename Number>
|
|
class Point2D
|
|
{
|
|
private:
|
|
Number _x;
|
|
Number _y;
|
|
|
|
public:
|
|
Point2D(Number x, Number y) : _x(x), _y(y){
|
|
print("Point2D default constructor called: " << this)}; //Default constructor
|
|
Point2D(const Point2D &); //Copy constructor
|
|
Point2D(Point2D &&); //Move constructor
|
|
~Point2D();
|
|
//Destructor
|
|
friend std::ostream &operator<<(std::ostream &stream, Point2D const &c)
|
|
{
|
|
return stream << "(" << c._x << "," << c._y << ")";
|
|
}
|
|
|
|
Number getX() { return _x; }
|
|
Number getY() { return _y; }
|
|
}; |