-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improvements on HJson creation #47
base: master
Are you sure you want to change the base?
Changes from all commits
a2e4a6b
919bdeb
64710c8
2c148e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -347,6 +347,27 @@ Value& Value::at(const char *name) { | |
return at(std::string(name)); | ||
} | ||
|
||
#if __cplusplus >= 201703L | ||
void Value::insert(const std::string&key, Value&& other) | ||
{ | ||
switch (prv->type) | ||
{ | ||
case Type::Undefined: | ||
prv->~ValueImpl(); | ||
// Recreate the private object using the same memory block. | ||
new(&(*prv)) ValueImpl(Type::Map); | ||
// Fall through | ||
case Type::Map: | ||
try { | ||
prv->m->m.insert_or_assign(key, std::move(other)); | ||
} catch(const std::out_of_range&) {} | ||
return; | ||
default: | ||
throw type_mismatch("Must be of type Map for that operation."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation seems to support type Undefined too, not just type Map. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true, this implementation follows a similar scheme as to that of operator [] , where if the Value is of type undefined, we assume the user wanted map and thus we convert it to a map type and then treat it as a map insertion. |
||
} | ||
} | ||
#endif // __cplusplus >= 201703L | ||
|
||
|
||
const Value Value::operator[](const std::string& name) const { | ||
if (prv->type == Type::Undefined) { | ||
|
@@ -1326,6 +1347,20 @@ void Value::push_back(const Value& other) { | |
prv->v->push_back(other); | ||
} | ||
|
||
#if __cplusplus >= 201703L | ||
void Value::push_back(Value&& other) { | ||
if (prv->type == Type::Undefined) { | ||
prv->~ValueImpl(); | ||
// Recreate the private object using the same memory block. | ||
new(&(*prv)) ValueImpl(Type::Vector); | ||
} else if (prv->type != Type::Vector) { | ||
throw type_mismatch("Must be of type Undefined or Vector for that operation."); | ||
} | ||
|
||
prv->v->push_back(std::move(other)); | ||
} | ||
#endif // __cplusplus >= 201703L | ||
|
||
|
||
void Value::move(int from, int to) { | ||
switch (prv->type) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would cause
std::out_of_range
to be thrown, and why should it be ignored?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right in that it does not make a lot of sense, maybe just not have a catch and throw whatever exception came from the container?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes better to not have a catch.