Skip to content
Snippets Groups Projects
Commit 4ddafbbb authored by Ramin Yaghoubzadeh Torky's avatar Ramin Yaghoubzadeh Torky
Browse files

read operations for standard containers OK

parent 3daf95b3
No related branches found
No related tags found
No related merge requests found
......@@ -105,6 +105,31 @@ IPAACA_HEADER_EXPORT class PayloadEntryProxy//{{{
IPAACA_HEADER_EXPORT operator long();
IPAACA_HEADER_EXPORT operator double();
IPAACA_HEADER_EXPORT operator bool();
IPAACA_HEADER_EXPORT template<typename Inner> operator std::vector<Inner>() {
if ((!json_value) || (!json_value->IsArray())) throw PayloadAddressingError();
std::vector<Inner> result;
for (auto it = json_value->Begin(); it != json_value->End(); ++it) {
result.push_back( json_value_cast<Inner>(*it) );
}
return result;
}
IPAACA_HEADER_EXPORT template<typename Inner> operator std::list<Inner>() {
if ((!json_value) || (!json_value->IsArray())) throw PayloadAddressingError();
std::list<Inner> result;
for (auto it = json_value->Begin(); it != json_value->End(); ++it) {
result.push_back( json_value_cast<Inner>(*it) );
}
return result;
}
IPAACA_HEADER_EXPORT template<typename Inner> operator std::map<std::string, Inner>() {
if ((!json_value) || (!json_value->IsObject())) throw PayloadAddressingError();
std::map<std::string, Inner> result;
for (auto it = json_value->MemberBegin(); it != json_value->MemberEnd(); ++it) {
result[std::string(it->name.GetString())] = json_value_cast<Inner>(it->value);
}
return result;
}
// FIXME why are these needed again?
IPAACA_HEADER_EXPORT std::string to_str();
//long to_int() { return operator long(); ;
IPAACA_HEADER_EXPORT long to_long();
......
......@@ -55,11 +55,80 @@ int main(int argc, char** argv) {
std::cout << "entry as long: " << (long) a << std::endl;
std::cout << "entry as double: " << (double) a << std::endl;
std::cout << "entry as bool: " << ((bool) a?"true":"false") << std::endl;
std::cout << "entry as list<string>: ";
// std::vector
std::cout << "entry as vector<string>: ";
try {
} catch {
std::cout << "(Error)" << std::endl;
std::vector<std::string> v = a;
std::for_each(v.begin(), v.end(), [](std::string& s) {
std::cout << s << " ";
});
std::cout << std::endl;
} catch (...) {
std::cout << "(n/a)" << std::endl;
}
std::cout << "entry as vector<long>: ";
try {
std::vector<long> v = a;
std::for_each(v.begin(), v.end(), [](long& s) {
std::cout << s << " ";
});
std::cout << std::endl;
} catch (...) {
std::cout << "(n/a)" << std::endl;
}
std::cout << "entry as vector<bool>: ";
try {
std::vector<bool> v = a;
std::for_each(v.begin(), v.end(), [](bool s) {
std::cout << (s?"true":"false") << " ";
});
std::cout << std::endl;
} catch (...) {
std::cout << "(n/a)" << std::endl;
}
// std::map
std::cout << "entry as map<string, string>: ";
try {
std::map<std::string, std::string> m = a;
for (auto it = m.begin(); it != m.end(); ++it) {
std::cout << it->first << ":" << it->second << " ";
}
std::cout << std::endl;
} catch (...) {
std::cout << "(n/a)" << std::endl;
}
std::cout << "entry as map<string, long>: ";
try {
std::map<std::string, long> m = a;
for (auto it = m.begin(); it != m.end(); ++it) {
std::cout << it->first << ":" << it->second << " ";
}
std::cout << std::endl;
} catch (...) {
std::cout << "(n/a)" << std::endl;
}
std::cout << "entry as map<string, double>: ";
try {
std::map<std::string, double> m = a;
for (auto it = m.begin(); it != m.end(); ++it) {
std::cout << it->first << ":" << it->second << " ";
}
std::cout << std::endl;
} catch (...) {
std::cout << "(n/a)" << std::endl;
}
std::cout << "entry as map<string, bool>: ";
try {
std::map<std::string, bool> m = a;
for (auto it = m.begin(); it != m.end(); ++it) {
std::cout << it->first << ":" << (it->second?"true":"false") << " ";
}
std::cout << std::endl;
} catch (...) {
std::cout << "(n/a)" << std::endl;
}
// Done
return 0;
////////////////////////////////////////////////////////////////////////////
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment