Skip to content

Commit 58eb2cb

Browse files
committed
adding area
1 parent 7d5399c commit 58eb2cb

File tree

7 files changed

+109
-1
lines changed

7 files changed

+109
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ unit_t -> an unit template that is the basis of all of the units
6767
6868
### Common
6969
70+
* area -> area with `m^2` as base unit
7071
* speed -> speed with `m / s` as base unit
7172
* acceleration -> acceleration with `m / s^2` as base unit
7273
* momentum -> momentum with `kg * m / s` as base unit

common/include/area.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include "unit_t.hpp"
4+
#include "length.hpp"
5+
6+
namespace sakurajin{
7+
namespace unit_system{
8+
namespace common{
9+
class area : public unit_t<22>{
10+
public:
11+
area();
12+
area(unit_t<22> val);
13+
explicit area(long double val);
14+
area(long double val, long double mult);
15+
};
16+
17+
base::length operator/(const area& a, const base::length& l);
18+
19+
area operator*(const base::length& l1, const base::length& l2);
20+
area square(const base::length& l);
21+
22+
inline namespace literals{
23+
area operator "" _km2(long double val);
24+
area operator "" _m2(long double val);
25+
area operator "" _mm2(long double val);
26+
27+
area operator "" _km2(unsigned long long int val);
28+
area operator "" _m2(unsigned long long int val);
29+
area operator "" _mm2(unsigned long long int val);
30+
}
31+
32+
}
33+
34+
}
35+
}
36+

common/include/common.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "force.hpp"
88
#include "energy.hpp"
99
#include "power.hpp"
10+
#include "area.hpp"
1011

1112
#if __has_include(<iostream>)
1213

@@ -16,6 +17,8 @@
1617
namespace unit_system{
1718
namespace common{
1819
std::ostream& operator<<(std::ostream& os, const acceleration& v);
20+
21+
std::ostream& operator<<(std::ostream& os, const area& a);
1922

2023
std::ostream& operator<<(std::ostream& os, const energy& v);
2124

common/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
cc = meson.get_compiler('cpp')
22

33
sources = [
4+
'src/area.cpp',
45
'src/speed.cpp',
56
'src/acceleration.cpp',
67
'src/momentum.cpp',

common/src/area.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "area.hpp"
2+
#include "prefix.hpp"
3+
4+
using namespace sakurajin::unit_system;
5+
using namespace prefix;
6+
7+
common::area::area(): area(0){};
8+
common::area::area(unit_t<22> val): area(val.value,val.multiplier){};
9+
common::area::area(long double val): area(val,1){};
10+
common::area::area(long double val, long double mult): unit_t<22>(val,mult){};
11+
12+
base::length common::operator/(const common::area& a, const base::length& l){
13+
auto a1 = sakurajin::unit_system::unit_cast(a,a.multiplier,0);
14+
auto l1 = sakurajin::unit_system::unit_cast(l,l.multiplier,0);
15+
return base::length{a1.value/l1.value,a1.multiplier/l1.multiplier};
16+
}
17+
18+
common::area common::operator*(const base::length& l1, const base::length& l2){
19+
auto _l1 = sakurajin::unit_system::unit_cast(l1,l1.multiplier,0);
20+
auto _l2 = sakurajin::unit_system::unit_cast(l2,l2.multiplier,0);
21+
22+
return common::area{_l1.value*_l2.value,_l1.multiplier*_l2.multiplier};
23+
}
24+
25+
common::area common::square(const base::length& l){
26+
return l*l;
27+
}
28+
29+
30+
common::area common::literals::operator "" _km2(long double len){
31+
return common::area{len, kilo*kilo};
32+
}
33+
34+
common::area common::literals::operator "" _m2(long double len){
35+
return common::area{len,1};
36+
}
37+
38+
common::area common::literals::operator "" _mm2(long double len){
39+
return common::area{len, milli*milli};
40+
}
41+
42+
43+
common::area common::literals::operator "" _km2(unsigned long long int len){
44+
return common::area{static_cast<long double>(len), kilo*kilo};
45+
}
46+
47+
common::area common::literals::operator "" _m2(unsigned long long int len){
48+
return common::area{static_cast<long double>(len),1};
49+
}
50+
51+
common::area common::literals::operator "" _mm2(unsigned long long int len){
52+
return common::area{static_cast<long double>(len), milli*milli};
53+
}

common/src/common.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
return os << a1.value << " meter per second^2";
1010
}
1111

12+
std::ostream& common::operator<<(std::ostream& os, const common::area& a){
13+
auto a1 = sakurajin::unit_system::unit_cast(a,1);
14+
return os << a1.value << " meters^2";
15+
}
16+
1217
std::ostream& common::operator<<(std::ostream& os, const common::energy& p){
1318
auto p1 = sakurajin::unit_system::unit_cast(p,1);
1419
return os << p1.value << " Joules";

common/tests/common_test.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static const auto p1 = 25_kgmps;
2525
static const auto F1 = 2.5_N;
2626
static const auto E1 = 25_J;
2727
static const auto P1 = 2.5_W;
28+
static const auto A1 = 100_m2;
2829

2930
TEST( common_tests, speed_test) {
3031

@@ -87,7 +88,6 @@ TEST( common_tests, energy_test ) {
8788

8889
};
8990

90-
9191
TEST( common_tests, power_test ) {
9292

9393
const auto _P2 = 10_kg * 1_G * 1_mps;
@@ -98,6 +98,15 @@ TEST( common_tests, power_test ) {
9898

9999
};
100100

101+
TEST( common_tests, area_test ) {
102+
103+
const auto _A2 = s1 * s2;
104+
EXPECT_TRUE(_A2 == 2500_m2);
105+
106+
EXPECT_TRUE(square(s2) == A1);
107+
EXPECT_TRUE(A1/s2 == s2);
108+
};
109+
101110
int main(int argc, char **argv){
102111
::testing::InitGoogleTest(&argc, argv);
103112
return RUN_ALL_TESTS();

0 commit comments

Comments
 (0)