Better error message when a blueprint key is not found

This commit is contained in:
nsubiron 2018-10-15 15:59:43 +02:00
parent 5de16e9eee
commit 27fbca5900
4 changed files with 33 additions and 13 deletions

View File

@ -35,6 +35,19 @@ namespace client {
});
}
const ActorAttribute &ActorBlueprint::GetAttribute(const std::string &id) const {
auto it = _attributes.find(id);
if (it == _attributes.end()) {
using namespace std::string_literals;
throw std::out_of_range("attribute '"s + id + "' not found");
}
return it->second;
}
void ActorBlueprint::SetAttribute(const std::string &id, std::string value) {
const_cast<ActorAttribute &>(GetAttribute(id)).Set(std::move(value));
}
rpc::ActorDescription ActorBlueprint::MakeActorDescription() const {
rpc::ActorDescription description;
description.uid = _uid;

View File

@ -76,18 +76,14 @@ namespace client {
}
/// @throw std::out_of_range if no such element exists.
const ActorAttribute &GetAttribute(const std::string &id) const {
return _attributes.at(id);
}
const ActorAttribute &GetAttribute(const std::string &id) const;
/// Set the value of the attribute given by @a id.
///
/// @throw std::out_of_range if no such element exists.
/// @throw InvalidAttributeValue if attribute is not modifiable.
/// @throw InvalidAttributeValue if format does not match the attribute type.
void SetAttribute(const std::string &id, std::string value) {
_attributes.at(id).Set(std::move(value));
}
void SetAttribute(const std::string &id, std::string value);
size_t size() const {
return _attributes.size();

View File

@ -31,6 +31,20 @@ namespace client {
return SharedPtr<BlueprintLibrary>{new BlueprintLibrary(result)};
}
BlueprintLibrary::const_pointer BlueprintLibrary::Find(const std::string &key) const {
auto it = _blueprints.find(key);
return it != _blueprints.end() ? &it->second : nullptr;
}
BlueprintLibrary::const_reference BlueprintLibrary::at(const std::string &key) const {
auto it = _blueprints.find(key);
if (it == _blueprints.end()) {
using namespace std::string_literals;
throw std::out_of_range("blueprint '"s + key + "' not found");
}
return it->second;
}
BlueprintLibrary::const_reference BlueprintLibrary::at(size_type pos) const {
if (pos >= size())
throw std::out_of_range("index out of range");

View File

@ -43,14 +43,10 @@ namespace client {
/// Filters a list of ActorBlueprint with tags matching @a wildcard_pattern.
SharedPtr<BlueprintLibrary> Filter(const std::string &wildcard_pattern) const;
const_pointer Find(const std::string &key) const {
auto it = _blueprints.find(key);
return it != _blueprints.end() ? &it->second : nullptr;
}
const_pointer Find(const std::string &key) const;
const_reference at(const std::string &key) const {
return _blueprints.at(key);
}
/// @throw std::out_of_range if no such element exists.
const_reference at(const std::string &key) const;
/// @warning Linear complexity.
const_reference operator[](size_type pos) const {
@ -58,6 +54,7 @@ namespace client {
}
/// @warning Linear complexity.
/// @throw std::out_of_range if !(pos < size()).
const_reference at(size_type pos) const;
const_iterator begin() const /*noexcept*/ {