-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from andrsd/from-name
Adding `fprops::from_name`
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// SPDX-FileCopyrightText: 2024 David Andrs <andrsd@gmail.com> | ||
// SPDX-License-Identifier: MIT | ||
|
||
#pragma once | ||
|
||
#include "SinglePhaseFluidProperties.h" | ||
#include <string> | ||
|
||
namespace fprops { | ||
|
||
/// Construct fluid properties from a fluid name | ||
/// | ||
/// @param name Fluid name | ||
/// @return Fluid properties | ||
SinglePhaseFluidProperties * from_name(const std::string & name); | ||
|
||
} // namespace fprops |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// SPDX-FileCopyrightText: 2024 David Andrs <andrsd@gmail.com> | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include "fprops/fprops.h" | ||
#include "fprops/Air.h" | ||
#include "fprops/CarbonDioxide.h" | ||
#include "fprops/Helium.h" | ||
#include "fprops/Nitrogen.h" | ||
#include "fprops/Exception.h" | ||
|
||
namespace fprops { | ||
|
||
std::string | ||
to_lower(const std::string & name) | ||
{ | ||
std::string lower(name); | ||
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower); | ||
return lower; | ||
} | ||
|
||
SinglePhaseFluidProperties * | ||
from_name(const std::string & name) | ||
{ | ||
auto n = to_lower(name); | ||
if (n == "air") | ||
return new Air(); | ||
else if (n == "carbon_dioxide" || n == "co2") | ||
Check warning on line 27 in src/fprops.cpp GitHub Actions / c++ lintersrc/fprops.cpp:27:5 [readability-else-after-return]
|
||
return new CarbonDioxide(); | ||
else if (n == "helium" || n == "he") | ||
return new Helium(); | ||
else if (n == "nitrogen" || n == "n2") | ||
return new Nitrogen(); | ||
else | ||
throw Exception("Unknown fluid name '{}'", name); | ||
} | ||
|
||
} // namespace fprops |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "gmock/gmock.h" | ||
#include "fprops/fprops.h" | ||
#include "fprops/Air.h" | ||
#include "fprops/CarbonDioxide.h" | ||
#include "fprops/Helium.h" | ||
#include "fprops/Nitrogen.h" | ||
|
||
using namespace fprops; | ||
using namespace testing; | ||
|
||
TEST(FpropsTest, from_name) | ||
{ | ||
auto air = fprops::from_name("air"); | ||
EXPECT_THAT(dynamic_cast<Air *>(air), NotNull()); | ||
|
||
auto co2 = fprops::from_name("co2"); | ||
EXPECT_THAT(dynamic_cast<CarbonDioxide *>(co2), NotNull()); | ||
|
||
auto he = fprops::from_name("helium"); | ||
EXPECT_THAT(dynamic_cast<Helium *>(he), NotNull()); | ||
|
||
auto n2 = fprops::from_name("nitrogen"); | ||
EXPECT_THAT(dynamic_cast<Nitrogen *>(n2), NotNull()); | ||
} |