Skip to content

Commit 91e5022

Browse files
committed
Added more operators
1 parent 14b07cc commit 91e5022

File tree

4 files changed

+45
-29
lines changed

4 files changed

+45
-29
lines changed

src/examples/example_operators.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ void example_operator_1() {
1717
WsjcppYamlCursor cur = yaml.getCursor();
1818
std::cout << "before : " << cur["test"].valStr() << std::endl;
1919
cur["test"] = "some new string";
20-
std::cout << "after : " << cur["test"].valStr() << std::endl;
21-
20+
std::cout << "after 1 : " << cur["test"].valStr() << std::endl;
21+
cur["test"] = 111;
22+
std::cout << "after 2 : " << cur["test"].valStr() << std::endl;
23+
cur["test"] = true;
24+
std::cout << "after 3 : " << cur["test"].valStr() << std::endl;
2225
}

src/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,11 @@ int main(int argc, char* argv[]) {
5555
return -1;
5656
}
5757
std::cout << "Done." << std::endl;
58+
std::string s = yaml["version"];
59+
// int nVer = yaml["version"];
60+
// bool bVer = yaml["version"];
61+
std::cout << s << std::endl;
62+
63+
example_operator_1();
5864
return 0;
5965
}

src/wsjcpp_yaml.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,29 +1160,23 @@ WsjcppYamlCursor &WsjcppYamlCursor::comment(const std::string& sComment) {
11601160

11611161
// ---------------------------------------------------------------------
11621162

1163-
std::string WsjcppYamlCursor::valStr() {
1163+
std::string WsjcppYamlCursor::valStr() const {
11641164
return m_pCurrentNode != nullptr ? m_pCurrentNode->getValue() : "";
11651165
}
11661166

1167-
// ---------------------------------------------------------------------
1168-
11691167
WsjcppYamlCursor &WsjcppYamlCursor::val(const std::string &sValue) {
11701168
if (m_pCurrentNode != nullptr) {
11711169
m_pCurrentNode->setValue(sValue); // TODO reserch need or not add quotes
11721170
}
11731171
return *this;
11741172
}
11751173

1176-
// ---------------------------------------------------------------------
1177-
11781174
WsjcppYamlCursor &WsjcppYamlCursor::val(const char *sValue) {
11791175
this->val(std::string(sValue));
11801176
return *this;
11811177
}
11821178

1183-
// ---------------------------------------------------------------------
1184-
1185-
int WsjcppYamlCursor::valInt() {
1179+
int WsjcppYamlCursor::valInt() const {
11861180
if (m_pCurrentNode != nullptr) {
11871181
std::string sValue = m_pCurrentNode->getValue();
11881182
sValue = WsjcppYaml::toLower(sValue);
@@ -1195,18 +1189,14 @@ int WsjcppYamlCursor::valInt() {
11951189
return 0;
11961190
}
11971191

1198-
// ---------------------------------------------------------------------
1199-
12001192
WsjcppYamlCursor &WsjcppYamlCursor::val(int nValue) {
12011193
if (m_pCurrentNode != nullptr) {
12021194
m_pCurrentNode->setValue(std::to_string(nValue));
12031195
}
12041196
return *this;
12051197
}
12061198

1207-
// ---------------------------------------------------------------------
1208-
1209-
bool WsjcppYamlCursor::valBool() {
1199+
bool WsjcppYamlCursor::valBool() const {
12101200
if (m_pCurrentNode != nullptr) {
12111201
std::string sValue = m_pCurrentNode->getValue();
12121202
sValue = WsjcppYaml::toLower(sValue);
@@ -1222,39 +1212,47 @@ bool WsjcppYamlCursor::valBool() {
12221212
return false;
12231213
}
12241214

1225-
// ---------------------------------------------------------------------
1226-
12271215
WsjcppYamlCursor &WsjcppYamlCursor::val(bool bValue) {
12281216
if (m_pCurrentNode != nullptr) {
12291217
m_pCurrentNode->setValue((bValue ? "yes" : "no"));
12301218
}
12311219
return *this;
12321220
}
12331221

1234-
// ---------------------------------------------------------------------
1235-
12361222
WsjcppYamlNode *WsjcppYamlCursor::node() {
12371223
return m_pCurrentNode;
12381224
}
12391225

1240-
// ---------------------------------------------------------------------
1241-
12421226
WsjcppYamlCursor WsjcppYamlCursor::operator[](int idx) const {
12431227
if (m_pCurrentNode != nullptr && m_pCurrentNode->isArray() && idx < m_pCurrentNode->getLength() && idx >= 0) {
12441228
return WsjcppYamlCursor(m_pCurrentNode->getElement(idx));
12451229
}
12461230
return WsjcppYamlCursor();
12471231
}
12481232

1249-
// ---------------------------------------------------------------------
1250-
12511233
WsjcppYamlCursor WsjcppYamlCursor::operator[](const std::string &sName) const {
12521234
if (m_pCurrentNode != nullptr && m_pCurrentNode->isMap() && m_pCurrentNode->hasElement(sName)) {
12531235
return WsjcppYamlCursor(m_pCurrentNode->getElement(sName));
12541236
}
12551237
return WsjcppYamlCursor();
12561238
}
12571239

1240+
WsjcppYamlCursor& WsjcppYamlCursor::operator=(const char *sVal) {
1241+
return this->val(std::string(sVal));
1242+
}
1243+
1244+
WsjcppYamlCursor& WsjcppYamlCursor::operator=(const std::string &sVal) {
1245+
return this->val(sVal);
1246+
}
1247+
1248+
WsjcppYamlCursor& WsjcppYamlCursor::operator=(const int &nVal) {
1249+
return this->val(nVal);
1250+
}
1251+
1252+
WsjcppYamlCursor& WsjcppYamlCursor::operator=(const bool &bVal) {
1253+
return this->val(bVal);
1254+
}
1255+
12581256
// ---------------------------------------------------------------------
12591257
// WsjcppYaml
12601258

src/wsjcpp_yaml.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <iostream>
55
#include <vector>
6+
#include <cstddef>
67
#include <map>
78
#include <stdio.h>
89
#include <stdlib.h>
@@ -246,22 +247,30 @@ class WsjcppYamlCursor {
246247
WsjcppYamlCursor &comment(const std::string& sComment);
247248

248249
// val
249-
std::string valStr();
250+
std::string valStr() const;
250251
WsjcppYamlCursor &val(const std::string &sValue);
251252
WsjcppYamlCursor &val(const char *sValue);
252-
int valInt();
253+
int valInt() const;
253254
WsjcppYamlCursor &val(int nValue);
254-
bool valBool();
255+
bool valBool() const;
255256
WsjcppYamlCursor &val(bool bValue);
256257

257258
// node
258259
WsjcppYamlNode *node();
259260

261+
std::string getCurrentNodePath();
262+
263+
explicit operator int() const { return valInt(); };
264+
explicit operator bool() const { return valBool(); }
265+
operator std::string() const { return valStr(); };
266+
260267
WsjcppYamlCursor operator[](int idx) const;
261268
WsjcppYamlCursor operator[](const std::string &sName) const;
262-
WsjcppYamlCursor& operator=(const std::string &sVal) noexcept {
263-
return this->val(sVal);
264-
}
269+
270+
WsjcppYamlCursor& operator=(const char *sVal);
271+
WsjcppYamlCursor& operator=(const std::string &sVal);
272+
WsjcppYamlCursor& operator=(const int &nVal);
273+
WsjcppYamlCursor& operator=(const bool &bVal);
265274

266275
private:
267276
std::string TAG;

0 commit comments

Comments
 (0)