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

C++: implemented push_back() (=append()), and extend() for proxies

parent a60c1b60
No related branches found
No related tags found
No related merge requests found
......@@ -494,6 +494,45 @@ IPAACA_HEADER_EXPORT class PayloadEntryProxy//{{{
connect_to_existing_parents();
_payload->set(key, document_entry->document);
}*/
/// Append a supported type to a list-type payload value
IPAACA_HEADER_EXPORT template<typename T> void push_back(T t)
{
if ((!json_value) || (!json_value->IsArray())) throw PayloadAddressingError();
PayloadDocumentEntry::ptr new_entry = document_entry->clone(); // copy-on-write, no lock required
rapidjson::Value& list = new_entry->get_or_create_nested_value_from_proxy_path(this);
rapidjson::Value newval;
pack_into_json_value(newval, new_entry->document.GetAllocator(), t);
list.PushBack(newval, new_entry->document.GetAllocator());
_payload->set(_key, new_entry);
}
/// Alias for push_back() (somewhat pythonic - since we also provide extend())
IPAACA_HEADER_EXPORT template<typename T> void append(T t) { push_back<T>(t); }
/// Extend a list-type payload value with a vector containing items of a supported type
IPAACA_HEADER_EXPORT template<typename T> void extend(const std::vector<T>& ts)
{
if ((!json_value) || (!json_value->IsArray())) throw PayloadAddressingError();
PayloadDocumentEntry::ptr new_entry = document_entry->clone(); // copy-on-write, no lock required
rapidjson::Value& list = new_entry->get_or_create_nested_value_from_proxy_path(this);
for (auto& t: ts) {
rapidjson::Value newval;
pack_into_json_value(newval, new_entry->document.GetAllocator(), t);
list.PushBack(newval, new_entry->document.GetAllocator());
}
_payload->set(_key, new_entry);
}
/// Extend a list-type payload value with a list containing items of a supported type
IPAACA_HEADER_EXPORT template<typename T> void extend(const std::list<T>& ts)
{
if ((!json_value) || (!json_value->IsArray())) throw PayloadAddressingError();
PayloadDocumentEntry::ptr new_entry = document_entry->clone(); // copy-on-write, no lock required
rapidjson::Value& list = new_entry->get_or_create_nested_value_from_proxy_path(this);
for (auto& t: ts) {
rapidjson::Value newval;
pack_into_json_value(newval, new_entry->document.GetAllocator(), t);
list.PushBack(newval, new_entry->document.GetAllocator());
}
_payload->set(_key, new_entry);
}
};
// Available interpretations of payload entries (or children thereof) below.
// Usage of standard complex data structures (vector etc.) currently entails
......
......@@ -152,6 +152,20 @@ int iterators_main(int argc, char** argv)//{{{
std::cout << " Unexpected exception: " << ex.what() << std::endl;
}
std::cout << std::endl << "Appending a string item to the end of payload['a']" << std::endl;
iu->payload()["a"].push_back("appended string entry");
std::cout << "Resulting entries in payload['a']:" << std::endl;
for (auto v: iu->payload()["a"].as_list()) {
std::cout << " " << v << std::endl;
}
std::cout << std::endl << "Extending payload['a'] by a list of three bools" << std::endl;
iu->payload()["a"].extend(std::list<bool>{false, false, true});
std::cout << "Resulting entries in payload['a']:" << std::endl;
for (auto v: iu->payload()["a"].as_list()) {
std::cout << " " << v << std::endl;
}
return 0;
}
//}}}
......@@ -192,7 +206,6 @@ int json_testbed_main(int argc, char** argv)//{{{
}
//}}}
/*
int fakeiu_main(int argc, char** argv)//{{{
{
//if (argc<2) {
......@@ -318,7 +331,6 @@ int fakeiu_main(int argc, char** argv)//{{{
return 0;
}
//}}}
*/
int legacy_iu_main(int argc, char** argv)//{{{
{
......
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