Skip to content

Commit 5ddc59c

Browse files
committed
feat: get<> fallback to get<string> (close #8)
1 parent 76a578a commit 5ddc59c

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

ini/INIReader.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,13 @@ class INIReader
249249
// Return the list of sections found in ini file
250250
const std::set<std::string>& Sections() const;
251251

252-
template<typename T>
252+
template<typename T = std::string>
253253
T Get(std::string section, std::string name) const;
254254

255255
template<typename T>
256256
T Get(std::string section, std::string name, T&& default_v) const;
257257

258-
template<typename T>
258+
template<typename T = std::string>
259259
std::vector<T> GetVector(std::string section, std::string name) const;
260260

261261
template<typename T>
@@ -315,7 +315,7 @@ inline const std::set<std::string>& INIReader::Sections() const
315315
return _sections;
316316
}
317317

318-
template<typename T>
318+
template<typename T = std::string>
319319
inline T INIReader::Get(std::string section, std::string name) const {
320320
std::string key = MakeKey(section, name);
321321
if (!_values.count(key)) {
@@ -351,8 +351,7 @@ inline T INIReader::Get(std::string section, std::string name, T&& default_v) co
351351
return Get<T>(section, name);
352352
}
353353

354-
355-
template<typename T>
354+
template<typename T = std::string>
356355
inline std::vector<T> INIReader::GetVector(std::string section, std::string name) const {
357356
std::string key = MakeKey(section, name);
358357
if (!_values.count(key)) {
@@ -387,6 +386,10 @@ inline std::vector<T> INIReader::GetVector(std::string section, std::string name
387386

388387
template<typename T>
389388
inline T INIReader::Converter(std::string s) const {
389+
if constexpr (std::is_same<T, std::string>()) {
390+
return s;
391+
}
392+
390393
T v{};
391394
std::istringstream _{s};
392395
_.exceptions(std::ios::failbit);

test/tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ using namespace inih;
99
TEST(INIReader, get_single_value) {
1010
INIReader r{"./fixtures/config.ini"};
1111

12-
EXPECT_EQ(r.Get<std::string>("section1", "any"), std::string("1"));
12+
EXPECT_EQ(r.Get<>("section1", "any"), std::string("1"));
1313
EXPECT_EQ(r.Get<float>("section1", "any"), float(1.0));
1414
EXPECT_EQ(r.Get<double>("section1", "any"), double(1.0));
1515
EXPECT_EQ(r.Get<long>("section1", "any"), long(1));
1616
EXPECT_EQ(r.Get<unsigned long>("section1", "any"), (unsigned long)(1));
1717

18-
EXPECT_EQ(r.Get<std::string>("section1", "any2"), std::string("true"));
18+
EXPECT_EQ(r.Get("section1", "any2"), std::string("true"));
1919
EXPECT_EQ(r.Get<bool>("section1", "any2"), true);
2020
}
2121

@@ -26,7 +26,7 @@ TEST(INIReader, get_vector) {
2626
const std::vector<std::string> ans2{"1", "2", "3"};
2727

2828
const auto& vec1 = r.GetVector<int>("section2", "any_vec");
29-
const auto& vec2 = r.GetVector<std::string>("section2", "any_vec");
29+
const auto& vec2 = r.GetVector<>("section2", "any_vec");
3030

3131
for (int i = 0; i < ans1.size(); ++i) {
3232
EXPECT_EQ(vec1[i], ans1[i]);

0 commit comments

Comments
 (0)