Commit 08f01516 authored by Thomas Markwalder's avatar Thomas Markwalder
Browse files

[3156] Changed d2::LabeledValue label from char* to std::string

parent 86050220
......@@ -19,9 +19,9 @@ namespace d2 {
/**************************** LabeledValue ****************************/
LabeledValue::LabeledValue(const int value, const char* label)
LabeledValue::LabeledValue(const int value, const std::string& label)
: value_(value), label_(label) {
if (label == NULL || strlen(label) == 0) {
if (label.empty()) {
isc_throw(LabeledValueError, "labels cannot be empty");
}
}
......@@ -34,7 +34,7 @@ LabeledValue::getValue() const {
return (value_);
}
const char*
std::string
LabeledValue::getLabel() const {
return (label_);
}
......@@ -86,7 +86,7 @@ LabeledValueSet::add(LabeledValuePtr entry) {
}
void
LabeledValueSet::add(const int value, const char* label) {
LabeledValueSet::add(const int value, const std::string& label) {
add (LabeledValuePtr(new LabeledValue(value,label)));
}
......@@ -108,7 +108,7 @@ LabeledValueSet::isDefined(const int value) const {
return (it != map_.end());
}
const char*
std::string
LabeledValueSet::getLabel(const int value) const {
LabeledValueMap::const_iterator it = map_.find(value);
if (it != map_.end()) {
......@@ -116,7 +116,7 @@ LabeledValueSet::getLabel(const int value) const {
return (ptr->getLabel());
}
return (UNDEFINED_LABEL);
return (std::string(UNDEFINED_LABEL));
}
} // namespace isc::d2
......
......@@ -31,8 +31,7 @@ namespace d2 {
/// @brief Thrown if an error is encountered handling a LabeledValue.
class LabeledValueError : public isc::Exception {
public:
LabeledValueError(const char* file, size_t line,
const char* what) :
LabeledValueError(const char* file, size_t line, const char* what) :
isc::Exception(file, line, what) { };
};
......@@ -53,8 +52,8 @@ public:
/// @param value the numeric constant value to be labeled.
/// @param label the text label to associate to this value.
///
/// @throw LabeledValueError if label is null or empty.
LabeledValue(const int value, const char* label);
/// @throw LabeledValueError if label is empty.
LabeledValue(const int value, const std::string& label);
/// @brief Destructor.
///
......@@ -68,8 +67,8 @@ public:
/// @brief Gets the text label of this instance.
///
/// @return The text label as const char*
const char* getLabel() const;
/// @return The text label as string
std::string getLabel() const;
/// @brief Equality operator
///
......@@ -91,7 +90,7 @@ private:
int value_;
/// @brief The text label for the value.
const char* label_;
std::string label_;
};
/// @brief Dumps the label to ostream.
......@@ -148,9 +147,9 @@ public:
/// @param value the numeric constant value to be labeled.
/// @param label the text label to associate to this value.
///
/// @throw LabeledValuePtr if the label is null or empty, or if the set
/// @throw LabeledValuePtr if the label is empty, or if the set
/// already contains an entry with the same value.
void add(const int value, const char* label);
void add(const int value, const std::string& label);
/// @brief Fetches a pointer to the entry associated with value
///
......@@ -173,7 +172,7 @@ public:
///
/// @return the label of the value if defined, otherwise it returns
/// UNDEFINED_LABEL.
const char* getLabel(const int value) const;
std::string getLabel(const int value) const;
private:
/// @brief The map of labeled values.
......
......@@ -22,7 +22,7 @@ namespace d2 {
/********************************** State *******************************/
State::State(const int value, const char* label, StateHandler handler)
State::State(const int value, const std::string& label, StateHandler handler)
: LabeledValue(value, label), handler_(handler) {
}
......@@ -43,7 +43,7 @@ StateSet::~StateSet() {
}
void
StateSet::add(const int value, const char* label, StateHandler handler) {
StateSet::add(const int value, const std::string& label, StateHandler handler) {
try {
LabeledValueSet::add(LabeledValuePtr(new State(value, label, handler)));
} catch (const std::exception& ex) {
......@@ -151,7 +151,7 @@ StateModel::nopStateHandler() {
void
StateModel::defineEvent(unsigned int event_value, const char* label) {
StateModel::defineEvent(unsigned int event_value, const std::string& label) {
if (!isModelNew()) {
// Don't allow for self-modifying models.
isc_throw(StateModelError, "Events may only be added to a new model."
......@@ -177,7 +177,7 @@ StateModel::getEvent(unsigned int event_value) {
}
void
StateModel::defineState(unsigned int state_value, const char* label,
StateModel::defineState(unsigned int state_value, const std::string& label,
StateHandler handler) {
if (!isModelNew()) {
// Don't allow for self-modifying maps.
......@@ -341,12 +341,12 @@ StateModel::didModelFail() const {
return (isModelDone() && (next_event_ == FAIL_EVT));
}
const char*
std::string
StateModel::getStateLabel(const int state) const {
return (states_.getLabel(state));
}
const char*
std::string
StateModel::getEventLabel(const int event) const {
return (events_.getLabel(event));
}
......
......@@ -34,8 +34,7 @@ namespace d2 {
/// @brief Thrown if the state machine encounters a general error.
class StateModelError : public isc::Exception {
public:
StateModelError(const char* file, size_t line,
const char* what) :
StateModelError(const char* file, size_t line, const char* what) :
isc::Exception(file, line, what) { };
};
......@@ -71,7 +70,7 @@ public:
/// @endcode
///
/// @throw StateModelError if label is null or blank.
State(const int value, const char* label, StateHandler handler);
State(const int value, const std::string& label, StateHandler handler);
/// @brief Destructor
virtual ~State();
......@@ -108,7 +107,7 @@ public:
///
/// @throw StateModelError if the value is already defined in the set, or
/// if the label is null or blank.
void add(const int value, const char* label, StateHandler handler);
void add(const int value, const std::string& label, StateHandler handler);
/// @brief Fetches a state for the given value.
///
......@@ -355,8 +354,8 @@ protected:
/// exceptions.
///
/// @throw StateModelError if the model has already been started, if
/// the value is already defined, or if the label is null or empty.
void defineEvent(unsigned int value, const char* label);
/// the value is already defined, or if the label is empty.
void defineEvent(unsigned int value, const std::string& label);
/// @brief Fetches the event referred to by value.
///
......@@ -422,8 +421,8 @@ protected:
/// exceptions.
///
/// @throw StateModelError if the model has already been started, if
/// the value is already defined, or if the label is null or empty.
void defineState(unsigned int value, const char* label,
/// the value is already defined, or if the label is empty.
void defineState(unsigned int value, const std::string& label,
StateHandler handler);
/// @brief Fetches the state referred to by value.
......@@ -601,9 +600,9 @@ public:
///
/// @param event is the numeric event value for which the label is desired.
///
/// @return Returns a const char* containing the event label or
/// @return Returns a string containing the event label or
/// LabeledValueSet::UNDEFINED_LABEL if the value is undefined.
const char* getEventLabel(const int event) const;
std::string getEventLabel(const int event) const;
/// @brief Fetches the label associated with an state value.
///
......@@ -611,7 +610,7 @@ public:
///
/// @return Returns a const char* containing the state label or
/// LabeledValueSet::UNDEFINED_LABEL if the value is undefined.
const char* getStateLabel(const int state) const;
std::string getStateLabel(const int state) const;
/// @brief Convenience method which returns a string rendition of the
/// current state and next event.
......
......@@ -24,9 +24,6 @@ namespace {
/// @brief Verifies basic construction and accessors for LabeledValue.
TEST(LabeledValue, construction) {
/// Verify that a null label is not allowed.
ASSERT_THROW(LabeledValue(1, NULL), LabeledValueError);
/// Verify that an empty label is not allowed.
ASSERT_THROW(LabeledValue(1, ""), LabeledValueError);
......
......@@ -238,7 +238,7 @@ public:
event = getEvent(value);
EXPECT_TRUE(event);
EXPECT_EQ(value, event->getValue());
EXPECT_EQ(label, std::string(event->getLabel()));
EXPECT_EQ(label, event->getLabel());
} catch (const std::exception& ex) {
return false;
}
......@@ -253,7 +253,7 @@ public:
state = getState(value);
EXPECT_TRUE(state);
EXPECT_EQ(value, state->getValue());
EXPECT_EQ(label, std::string(state->getLabel()));
EXPECT_EQ(label, state->getLabel());
} catch (const std::exception& ex) {
return false;
}
......@@ -381,7 +381,7 @@ TEST_F(StateModelTest, stateDefinition) {
// Verify the state's value and label.
EXPECT_EQ(READY_ST, state->getValue());
EXPECT_EQ("READY_ST", std::string(state->getLabel()));
EXPECT_EQ("READY_ST", state->getLabel());
// Now verify that retrieved state's handler executes the correct method.
// Make sure the dummy called flag is false prior to invocation.
......@@ -426,8 +426,7 @@ TEST_F(StateModelTest, stateDictionary) {
// Verify that undefined states are handled correctly.
EXPECT_THROW(getState(9999), StateModelError);
EXPECT_EQ(LabeledValueSet::UNDEFINED_LABEL,
std::string(getStateLabel(9999)));
EXPECT_EQ(LabeledValueSet::UNDEFINED_LABEL, getStateLabel(9999));
}
/// @brief General testing of state context accessors.
......
......@@ -473,10 +473,12 @@ private:
/// initiate server shutdown procedure.
volatile bool shutdown_;
#if 0
/// Indexes for registered hook points
int hook_index_pkt6_receive_;
int hook_index_subnet6_select_;
int hook_index_pkt6_send_;
#endif
/// UDP port number on which server listens.
uint16_t port_;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment