44#include " yavl.h"
55
66using 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
823ostream& 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 ;
0 commit comments