imlpement vector2 +-*/ operations, add CowPtr implementation
This commit is contained in:
parent
dbdb15e9af
commit
b6f00051ed
@ -1,8 +1,24 @@
|
||||
#include "App.hh"
|
||||
#include "Math.hh"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
App::App* app = new App::App();
|
||||
return app->getReturnCode();
|
||||
// App::App* app = new App::App();
|
||||
// return app->getReturnCode();
|
||||
Vector2 v1(1, 1.5);
|
||||
Vector2 v2(3, -2);
|
||||
Vector2 res = v1 + v2;
|
||||
cout << res.x << ", " << res.y << endl;
|
||||
res = v1 - v2;
|
||||
cout << res.x << ", " << res.y << endl;
|
||||
res = v1 * v2;
|
||||
cout << res.x << ", " << res.y << endl;
|
||||
res = v1 / v2;
|
||||
cout << res.x << ", " << res.y << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
47
App/AppLib/include/AppLib/CowPtr.hh
Normal file
47
App/AppLib/include/AppLib/CowPtr.hh
Normal file
@ -0,0 +1,47 @@
|
||||
// Implementation from https://www.youtube.com/watch?v=9b9hWIH8-hE
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace Core {
|
||||
template<class T>
|
||||
class CowPtr
|
||||
{
|
||||
public:
|
||||
explicit CowPtr(T* t) : content(t)
|
||||
{
|
||||
}
|
||||
|
||||
T& operator*()
|
||||
{
|
||||
detach();
|
||||
return *content;
|
||||
}
|
||||
const T& operator*() const
|
||||
{
|
||||
return *content;
|
||||
}
|
||||
|
||||
T* operator->()
|
||||
{
|
||||
detach();
|
||||
return content.operator->();
|
||||
}
|
||||
|
||||
const T* operator->() const
|
||||
{
|
||||
return content.operator->();
|
||||
}
|
||||
|
||||
std::shared_ptr<T> content;
|
||||
|
||||
private:
|
||||
void detach()
|
||||
{
|
||||
T* tmp = content.get();
|
||||
if(!(tmp == 0 || content.unique()))
|
||||
{
|
||||
content = std::shared_ptr<T>(new T(*tmp));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -8,6 +8,23 @@ namespace Core
|
||||
class Vector2
|
||||
{
|
||||
public:
|
||||
Vector2();
|
||||
Vector2(float x, float y);
|
||||
|
||||
Vector2 operator+(Vector2 const& other);
|
||||
Vector2 operator-(Vector2 const& other);
|
||||
Vector2 operator*(Vector2 const& other);
|
||||
Vector2 operator/(Vector2 const& other);
|
||||
Vector2 operator+=(Vector2 const& other);
|
||||
Vector2 operator-=(Vector2 const& other);
|
||||
Vector2 operator*=(Vector2 const& other);
|
||||
Vector2 operator/=(Vector2 const& other);
|
||||
|
||||
bool operator==(Vector2 const& other);
|
||||
bool operator!=(Vector2 const& other);
|
||||
|
||||
Vector2 operator*(float const& scalar);
|
||||
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
@ -18,7 +35,21 @@ namespace Core
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
float w = 1;
|
||||
float w;
|
||||
|
||||
Vector3 operator+(Vector3 const& other);
|
||||
Vector3 operator-(Vector3 const& other);
|
||||
Vector3 operator*(Vector3 const& other);
|
||||
Vector3 operator/(Vector3 const& other);
|
||||
Vector3 operator+=(Vector3 const& other);
|
||||
Vector3 operator-=(Vector3 const& other);
|
||||
Vector3 operator*=(Vector3 const& other);
|
||||
Vector3 operator/=(Vector3 const& other);
|
||||
|
||||
bool operator==(Vector3 const& other);
|
||||
bool operator!=(Vector3 const& other);
|
||||
|
||||
Vector3 operator*(float const& scalar);
|
||||
};
|
||||
|
||||
} // namespace Core
|
||||
|
||||
@ -2,4 +2,32 @@
|
||||
|
||||
namespace Core
|
||||
{
|
||||
Vector2::Vector2() : x(0), y(0)
|
||||
{
|
||||
}
|
||||
|
||||
Vector2::Vector2(float x, float y) : x(x), y(y)
|
||||
{
|
||||
}
|
||||
|
||||
Vector2 Vector2::operator+(Vector2 const &other)
|
||||
{
|
||||
return Vector2(this->x+other.x, this->y+other.y);
|
||||
}
|
||||
|
||||
Vector2 Vector2::operator-(Vector2 const &other)
|
||||
{
|
||||
return Vector2(this->x-other.x, this->y-other.y);
|
||||
}
|
||||
|
||||
Vector2 Vector2::operator*(Vector2 const &other)
|
||||
{
|
||||
return Vector2(this->x*other.x, this->y*other.y);
|
||||
}
|
||||
|
||||
Vector2 Vector2::operator/(Vector2 const &other)
|
||||
{
|
||||
return Vector2(this->x/other.x, this->y/other.y);
|
||||
}
|
||||
|
||||
} // namespace Core
|
||||
Loading…
x
Reference in New Issue
Block a user