Skip to content

Commit

Permalink
size_2d, size_3d and size_nd classes now have a specialization with f…
Browse files Browse the repository at this point in the history
…loat. size_nd now trims inside the string initialization constructor
  • Loading branch information
iarfen committed Jul 12, 2023
1 parent e675368 commit 74b8c2a
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 2 deletions.
13 changes: 13 additions & 0 deletions special_units/size_2d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "size_2d.hpp"

using namespace std;

namespace scifir
{
string to_string(const size_2d<float>& x)
{
ostringstream output;
output << x.width << " * " << x.height;
return output.str();
}
}
79 changes: 79 additions & 0 deletions special_units/size_2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,92 @@ namespace scifir
T height;
};

template<>
class size_2d<float>
{
public:
size_2d<float>() : width(),height()
{}

size_2d<float>(const size_2d<float>& x) : width(x.width),height(x.height)
{}

size_2d<float>(size_2d<float>&& x) : width(move(x.width)),height(move(x.height))
{}

explicit size_2d<float>(const float& new_width,const float& new_height) : width(new_width),height(new_height)
{}

explicit size_2d<float>(const string& new_width,const string& new_height) : width(stof(new_width)),height(stof(new_height))
{}

explicit size_2d<float>(const string& init_size_2d) : size_2d<float>()
{
vector<string> widths;
boost::split(widths,init_size_2d,boost::is_any_of("*"));
if (widths.size() == 2)
{
boost::trim(widths[0]);
boost::trim(widths[1]);
width = stof(widths[0]);
height = stof(widths[1]);
}
}

size_2d<float>& operator=(const size_2d<float>& x)
{
width = x.width;
height = x.height;
return *this;
}

size_2d<float>& operator=(size_2d<float>&& x)
{
width = move(x.width);
height = move(x.height);
return *this;
}

size_2d<float> operator +(const size_2d<float>& x) const
{
return size_2d<float>(width + x.width,height + x.height);
}

size_2d<float> operator -(const size_2d<float>& x) const
{
return size_2d<float>(width - x.width,height - x.height);
}

void operator +=(const size_2d<float>& x)
{
width += x.width;
height += x.height;
}

void operator -=(const size_2d<float>& x)
{
width -= x.width;
height -= x.height;
}

float get_area() const
{
return width * height;
}

float width;
float height;
};

template<typename T>
string to_string(const size_2d<T>& x)
{
ostringstream output;
output << x.width << " * " << x.height;
return output.str();
}

string to_string(const size_2d<float>&);
}

template<typename T>
Expand Down
13 changes: 13 additions & 0 deletions special_units/size_3d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "size_3d.hpp"

using namespace std;

namespace scifir
{
string to_string(const size_3d<float>& x)
{
ostringstream output;
output << x.width << " * " << x.height << " * " << x.depth;
return output.str();
}
}
86 changes: 86 additions & 0 deletions special_units/size_3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,99 @@ namespace scifir
T depth;
};

template<>
class size_3d<float>
{
public:
size_3d<float>() : width(),height(),depth()
{}

size_3d<float>(const size_3d<float>& x) : width(x.width),height(x.height),depth(x.depth)
{}

size_3d<float>(size_3d<float>&& x) : width(move(x.width)),height(move(x.height)),depth(move(x.depth))
{}

explicit size_3d<float>(const float& new_width,const float& new_height,const float& new_depth) : width(new_width),height(new_height),depth(new_depth)
{}

explicit size_3d<float>(const string& new_width,const string& new_height,const string& new_depth) : width(stof(new_width)),height(stof(new_height)),depth(stof(new_depth))
{}

explicit size_3d<float>(const string& init_size_3d) : size_3d<float>()
{
vector<string> widths;
boost::split(widths,init_size_3d,boost::is_any_of("*"));
if (widths.size() == 3)
{
boost::trim(widths[0]);
boost::trim(widths[1]);
boost::trim(widths[2]);
width = stof(widths[0]);
height = stof(widths[1]);
depth = stof(widths[2]);
}
}

size_3d<float>& operator=(const size_3d<float>& x)
{
width = x.width;
height = x.height;
depth = x.depth;
return *this;
}

size_3d<float>& operator=(size_3d<float>&& x)
{
width = move(x.width);
height = move(x.height);
depth = move(x.depth);
return *this;
}

size_3d<float> operator +(const size_3d<float>& x) const
{
return size_3d<float>(width + x.width,height + x.height,depth + x.depth);
}

size_3d<float> operator -(const size_3d<float>& x) const
{
return size_3d<float>(width - x.width,height - x.height,depth - x.depth);
}

void operator +=(const size_3d<float>& x)
{
width += x.width;
height += x.height;
depth += x.depth;
}

void operator -=(const size_3d<float>& x)
{
width -= x.width;
height -= x.height;
depth -= x.depth;
}

float get_volume() const
{
return width * height * depth;
}

float width;
float height;
float depth;
};

template<typename T>
string to_string(const size_3d<T>& x)
{
ostringstream output;
output << x.width << " * " << x.height << " * " << x.depth;
return output.str();
}

string to_string(const size_3d<float>&);
}

template<typename T>
Expand Down
17 changes: 17 additions & 0 deletions special_units/size_nd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "size_nd.hpp"

using namespace std;

namespace scifir
{
string to_string(const size_nd<float>& x)
{
ostringstream output;
output << x.widths[0];
for (int i = 1; i < x.widths.size(); i++)
{
output << " * " << x.widths[i];
}
return output.str();
}
}
Loading

0 comments on commit 74b8c2a

Please sign in to comment.