robot_name: iteration 4; throw an exception

This commit is contained in:
Dmitry Kokorin 2016-03-07 20:42:14 +03:00
parent e1085191f9
commit e1d89f3467

View file

@ -1,6 +1,7 @@
#include "robot_name.h"
#include <algorithm>
#include <stdexcept>
#include <set>
#include <random>
@ -49,6 +50,9 @@ void robot::reset()
static distribution_t alpha_distribution('A', 'Z');
static distribution_t digit_distribution('0', '9');
if (used_names.size() >= name_capacity)
throw length_error("no more names available");
string new_name;
do {
@ -56,7 +60,7 @@ void robot::reset()
new_name = random_string(alpha_part_size, alpha_distribution)
+ random_string(digit_part_size, digit_distribution);
} while (used_names.size() < name_capacity && used_names.count(new_name));
} while (used_names.count(new_name));
used_names.erase(name_);
name_ = new_name;