Browse Source

[4093] Extended TokenOption class ctor with representation type.

Marcin Siodelski 9 years ago
parent
commit
5ad49c1f75

+ 1 - 1
src/lib/eval/parser.yy

@@ -120,7 +120,7 @@ string_expr : STRING
             | OPTION "[" INTEGER "]" DOTTEXT
                   {
                       uint16_t numeric_code = convert_option_code($3, @3, ctx);
-                      TokenPtr opt(new TokenOption(numeric_code));
+                      TokenPtr opt(new TokenOption(numeric_code, TokenOption::TEXTUAL));
                       ctx.expression.push_back(opt);
                   }
             | SUBSTRING "(" string_expr "," start_expr "," length_expr ")"

+ 2 - 2
src/lib/eval/tests/evaluate_unittest.cc

@@ -196,7 +196,7 @@ TEST_F(EvaluateTest, packet) {
     TokenPtr tstring;
     TokenPtr tequal;
 
-    ASSERT_NO_THROW(toption.reset(new TokenOption(100)));
+    ASSERT_NO_THROW(toption.reset(new TokenOption(100, TokenOption::TEXTUAL)));
     e_.push_back(toption);
     ASSERT_NO_THROW(tstring.reset(new TokenString("hundred4")));
     e_.push_back(tstring);
@@ -219,7 +219,7 @@ TEST_F(EvaluateTest, complex) {
     TokenPtr tequal;
 
     // Get the option, i.e., "hundred[46]"
-    ASSERT_NO_THROW(toption.reset(new TokenOption(100)));
+    ASSERT_NO_THROW(toption.reset(new TokenOption(100, TokenOption::TEXTUAL)));
     e_.push_back(toption);
 
     // Get substring("hundred[46]", 0, 7), i.e., "hundred"

+ 4 - 4
src/lib/eval/tests/token_unittest.cc

@@ -253,8 +253,8 @@ TEST_F(TokenTest, optionString4) {
     TokenPtr not_found;
 
     // The packets we use have option 100 with a string in them.
-    ASSERT_NO_THROW(found.reset(new TokenOption(100)));
-    ASSERT_NO_THROW(not_found.reset(new TokenOption(101)));
+    ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::TEXTUAL)));
+    ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::TEXTUAL)));
 
     // This should evaluate to the content of the option 100 (i.e. "hundred4")
     ASSERT_NO_THROW(found->evaluate(*pkt4_, values_));
@@ -281,8 +281,8 @@ TEST_F(TokenTest, optionString6) {
     TokenPtr not_found;
 
     // The packets we use have option 100 with a string in them.
-    ASSERT_NO_THROW(found.reset(new TokenOption(100)));
-    ASSERT_NO_THROW(not_found.reset(new TokenOption(101)));
+    ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::TEXTUAL)));
+    ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::TEXTUAL)));
 
     // This should evaluate to the content of the option 100 (i.e. "hundred6")
     ASSERT_NO_THROW(found->evaluate(*pkt6_, values_));

+ 18 - 3
src/lib/eval/token.h

@@ -146,6 +146,19 @@ protected:
 /// option. If the option is not found, an empty string ("") is returned.
 class TokenOption : public Token {
 public:
+
+    /// @brief Token representation type.
+    ///
+    /// There are many possible ways in which option can be presented.
+    /// Currently the textual and hexadecimal representations are
+    /// supported. The type of representation is specified in the
+    /// constructor and it affects the value generated by the
+    /// @c TokenOption::evaluate function.
+    enum RepresentationType {
+        TEXTUAL,
+        HEXADECIMAL
+    };
+
     /// @brief Constructor that takes an option code as a parameter
     /// @param option_code code of the option
     ///
@@ -153,8 +166,9 @@ public:
     /// introduce complex dependency of the libkea-eval on libdhcpsrv.
     ///
     /// @param option_code code of the option to be represented.
-    TokenOption(uint16_t option_code)
-        :option_code_(option_code) {}
+    /// @param rep_type Token representation type.
+    TokenOption(const uint16_t option_code, const RepresentationType& rep_type)
+        : option_code_(option_code), representation_type_(rep_type) {}
 
     /// @brief Evaluates the values of the option
     ///
@@ -177,7 +191,8 @@ public:
     }
 
 private:
-    uint16_t option_code_; ///< code of the option to be extracted
+    uint16_t option_code_; ///< Code of the option to be extracted
+    RepresentationType representation_type_; ///< Representation type.
 };
 
 /// @brief Token that represents equality operator (compares two other tokens)