Skip to content

Commit cdf98bb

Browse files
author
ranga.code
committed
namespaceified.
1 parent 88105d1 commit cdf98bb

File tree

3 files changed

+101
-76
lines changed

3 files changed

+101
-76
lines changed

example-code/checker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "yavl.h"
55

66
using namespace std;
7+
using namespace YAVL;
78

89
int main(int argc, char **argv)
910
{
@@ -31,7 +32,7 @@ int main(int argc, char **argv)
3132
return 2;
3233
}
3334

34-
YAVL yavl(gr, doc);
35+
Validator yavl(gr, doc);
3536
bool ok = yavl.validate();
3637
if (!ok) {
3738
cout << "ERRORS FOUND: " << endl << endl;

src/yavl.cpp

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@
44
#include "yavl.h"
55

66
using namespace std;
7+
using namespace YAVL;
8+
9+
namespace YAVL {
10+
template <>
11+
std::string ctype2str<unsigned long long>()
12+
{
13+
return "unsigned long long";
14+
}
15+
16+
template <>
17+
std::string ctype2str<string>()
18+
{
19+
return "string";
20+
}
21+
}
722

823
ostream& operator << (ostream& os, const Path& path)
924
{
@@ -17,7 +32,7 @@ ostream& operator << (ostream& os, const Path& path)
1732
return os;
1833
}
1934

20-
ostream& operator << (ostream& os, const YAVL_Exception& v)
35+
ostream& operator << (ostream& os, const Exception& v)
2136
{
2237
os << "REASON: " << v.why << endl;
2338
os << " doc path: " << v.doc_path << endl;
@@ -34,7 +49,7 @@ ostream& operator << (ostream& os, const Errors& v)
3449
return os;
3550
}
3651

37-
const string& YAVL::type2str(YAML::CONTENT_TYPE t)
52+
const string& Validator::type2str(YAML::CONTENT_TYPE t)
3853
{
3954
static string nonestr = "none";
4055
static string scalarstr = "scalar";
@@ -57,7 +72,7 @@ const string& YAVL::type2str(YAML::CONTENT_TYPE t)
5772
return nonestr;
5873
}
5974

60-
int YAVL::num_keys(const YAML::Node& doc)
75+
int Validator::num_keys(const YAML::Node& doc)
6176
{
6277
if (doc.GetType() != YAML::CT_MAP) {
6378
return 0;
@@ -69,11 +84,11 @@ int YAVL::num_keys(const YAML::Node& doc)
6984
return num;
7085
}
7186

72-
bool YAVL::validate_map(const YAML::Node &mapNode, const YAML::Node &doc)
87+
bool Validator::validate_map(const YAML::Node &mapNode, const YAML::Node &doc)
7388
{
7489
if (doc.GetType() != YAML::CT_MAP) {
7590
string reason = "expected map, but found " + type2str(doc.GetType());
76-
gen_error(YAVL_Exception(reason, gr_path, doc_path));
91+
gen_error(Exception(reason, gr_path, doc_path));
7792
return false;
7893
}
7994

@@ -84,7 +99,7 @@ bool YAVL::validate_map(const YAML::Node &mapNode, const YAML::Node &doc)
8499
const YAML::Node *docMapNode = 0;
85100
if (!(docMapNode = doc.FindValue(key))) {
86101
string reason = "key: " + key + " not found.";
87-
gen_error(YAVL_Exception(reason, gr_path, doc_path));
102+
gen_error(Exception(reason, gr_path, doc_path));
88103
ok = false;
89104
} else {
90105
doc_path.push_back(key);
@@ -99,7 +114,7 @@ bool YAVL::validate_map(const YAML::Node &mapNode, const YAML::Node &doc)
99114
return ok;
100115
}
101116

102-
bool YAVL::validate_leaf(const YAML::Node &gr, const YAML::Node &doc)
117+
bool Validator::validate_leaf(const YAML::Node &gr, const YAML::Node &doc)
103118
{
104119
assert( gr.GetType() == YAML::CT_SEQUENCE );
105120

@@ -111,25 +126,9 @@ bool YAVL::validate_leaf(const YAML::Node &gr, const YAML::Node &doc)
111126

112127
bool ok = true;
113128
if (type == "string") {
114-
try {
115-
string tmp = doc;
116-
ok = true;
117-
} catch (const YAML::InvalidScalar& e) {
118-
string reason = "unable to convert to string.";
119-
gen_error(YAVL_Exception(reason, gr_path, doc_path));
120-
ok = false;
121-
}
129+
attempt_to_convert<string>(doc, ok);
122130
} else if (type == "uint64") {
123-
try {
124-
unsigned long long tmp;
125-
doc >> tmp;
126-
ok = true;
127-
} catch (const YAML::InvalidScalar& e) {
128-
string tmp = doc;
129-
string reason = "unable to convert " + tmp + " to long long.";
130-
gen_error(YAVL_Exception(reason, gr_path, doc_path));
131-
ok = false;
132-
}
131+
attempt_to_convert<unsigned long long>(doc, ok);
133132
} else if (type == "enum") {
134133
ok = false;
135134
string docValue = doc;
@@ -141,17 +140,17 @@ bool YAVL::validate_leaf(const YAML::Node &gr, const YAML::Node &doc)
141140
}
142141
if (!ok) {
143142
string reason = "enum string " + docValue + " is not allowed.";
144-
gen_error(YAVL_Exception(reason, gr_path, doc_path));
143+
gen_error(Exception(reason, gr_path, doc_path));
145144
}
146145
}
147146
return ok;
148147
}
149148

150-
bool YAVL::validate_list(const YAML::Node &gr, const YAML::Node &doc)
149+
bool Validator::validate_list(const YAML::Node &gr, const YAML::Node &doc)
151150
{
152151
if (doc.GetType() != YAML::CT_SEQUENCE) {
153152
string reason = "expected list, but found " + type2str(doc.GetType());
154-
gen_error(YAVL_Exception(reason, gr_path, doc_path));
153+
gen_error(Exception(reason, gr_path, doc_path));
155154
return false;
156155
}
157156

@@ -168,7 +167,7 @@ bool YAVL::validate_list(const YAML::Node &gr, const YAML::Node &doc)
168167
return ok;
169168
}
170169

171-
bool YAVL::validate_doc(const YAML::Node &gr, const YAML::Node &doc)
170+
bool Validator::validate_doc(const YAML::Node &gr, const YAML::Node &doc)
172171
{
173172
bool ok = true;
174173
const YAML::Node *mapNode = 0;

src/yavl.h

Lines changed: 71 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,83 @@
1-
#ifndef YAVL_H_
2-
#define YAVL_H_
1+
#ifndef _YAVL_H_
2+
#define _YAVL_H_
33

44
#include "yaml.h"
55
#include <vector>
66
#include <string>
77
#include <ostream>
88

9-
typedef std::vector<std::string> Path;
10-
11-
class YAVL_Exception {
12-
public:
13-
std::string why;
14-
Path gr_path;
15-
Path doc_path;
16-
YAVL_Exception(const std::string _why,
17-
const Path& _gr_path,
18-
const Path& _doc_path) :
19-
why(_why), gr_path(_gr_path), doc_path(_doc_path) {};
20-
};
21-
22-
typedef std::vector<YAVL_Exception> Errors;
23-
24-
class YAVL {
25-
const YAML::Node& gr;
26-
const YAML::Node& doc;
27-
Path gr_path;
28-
Path doc_path;
29-
Errors errors;
30-
31-
int num_keys(const YAML::Node& doc);
32-
const std::string& type2str(YAML::CONTENT_TYPE t);
33-
bool validate_map(const YAML::Node &mapNode, const YAML::Node &doc);
34-
bool validate_leaf(const YAML::Node &gr, const YAML::Node &doc);
35-
bool validate_list(const YAML::Node &gr, const YAML::Node &doc);
36-
bool validate_doc(const YAML::Node &gr, const YAML::Node &doc);
37-
38-
void gen_error(const YAVL_Exception& err) {
39-
errors.push_back(err);
40-
}
9+
namespace YAVL
10+
{
4111

42-
public:
43-
YAVL(const YAML::Node& _gr, const YAML::Node& _doc) :
44-
gr(_gr), doc(_doc) {};
45-
bool validate() {
46-
return validate_doc(gr, doc);
47-
}
48-
const Errors& get_errors() {
49-
return errors;
12+
typedef std::vector<std::string> Path;
13+
14+
// really sucks that I have to do this sort of crap since I can't
15+
// pass a type as an argument to a function.
16+
template <typename T>
17+
std::string ctype2str()
18+
{
19+
return "FAIL";
5020
}
51-
};
5221

53-
std::ostream& operator << (std::ostream& os, const Path& path);
54-
std::ostream& operator << (std::ostream& os, const YAVL_Exception& v);
55-
std::ostream& operator << (std::ostream& os, const Errors& v);
22+
class Exception {
23+
public:
24+
std::string why;
25+
Path gr_path;
26+
Path doc_path;
27+
Exception(const std::string _why,
28+
const Path& _gr_path,
29+
const Path& _doc_path) :
30+
why(_why), gr_path(_gr_path), doc_path(_doc_path) {};
31+
};
32+
33+
typedef std::vector<Exception> Errors;
34+
35+
class Validator {
36+
const YAML::Node& gr;
37+
const YAML::Node& doc;
38+
Path gr_path;
39+
Path doc_path;
40+
Errors errors;
41+
42+
int num_keys(const YAML::Node& doc);
43+
const std::string& type2str(YAML::CONTENT_TYPE t);
44+
bool validate_map(const YAML::Node &mapNode, const YAML::Node &doc);
45+
bool validate_leaf(const YAML::Node &gr, const YAML::Node &doc);
46+
bool validate_list(const YAML::Node &gr, const YAML::Node &doc);
47+
bool validate_doc(const YAML::Node &gr, const YAML::Node &doc);
48+
49+
void gen_error(const Exception& err) {
50+
errors.push_back(err);
51+
}
52+
53+
template<typename T>
54+
void attempt_to_convert(const YAML::Node& scalar_node, bool& ok) {
55+
try {
56+
T tmp;
57+
scalar_node >> tmp;
58+
ok = true;
59+
} catch (const YAML::InvalidScalar& e) {
60+
std::string reason = "unable to convert to " + YAVL::ctype2str<T>();
61+
gen_error(Exception(reason, gr_path, doc_path));
62+
ok = false;
63+
}
64+
}
65+
66+
public:
67+
Validator(const YAML::Node& _gr, const YAML::Node& _doc) :
68+
gr(_gr), doc(_doc) {};
69+
bool validate() {
70+
return validate_doc(gr, doc);
71+
}
72+
const Errors& get_errors() {
73+
return errors;
74+
}
75+
};
76+
}
77+
78+
std::ostream& operator << (std::ostream& os, const YAVL::Path& path);
79+
std::ostream& operator << (std::ostream& os, const YAVL::Exception& v);
80+
std::ostream& operator << (std::ostream& os, const YAVL::Errors& v);
5681

5782
#endif
5883

0 commit comments

Comments
 (0)