AidlConstantValue::Cast returns typed value

ValueString() squashes constant value into std::string, which is mostly
okay when generating code. But for Annotations, it's the AIDL compiler
which consumes param's values. Stringified values are difficult to
process. For example, there's duplicate logic to unquote stringified
string. (@JavaPassthrough, @Backing, @Description, ...)

Cast() returns typed value for AidlConstantValues. For example, we can
retrieve unquoted std::string from AidlContantValue::String() and
std::vector<T> from AidlConstantValue::Array().

Bug: none
Test: aidl_unittests
Change-Id: I8c350bfd999bcacd2dbbb0d5296b147eb469e216
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index 36d82c9..e5fe9e9 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -1049,6 +1049,27 @@
   EXPECT_THAT(code, testing::HasSubstr("public static final int y = 1;"));
 }
 
+TEST_F(AidlTest, ConstantValueCast) {
+  using Ptr = unique_ptr<AidlConstantValue>;
+  const AidlLocation& loc = AIDL_LOCATION_HERE;
+
+  EXPECT_EQ('c', Ptr(AidlConstantValue::Character(loc, 'c'))->Cast<char>());
+  EXPECT_EQ("abc", Ptr(AidlConstantValue::String(loc, "\"abc\""))->Cast<string>());
+  EXPECT_FLOAT_EQ(1.0f, Ptr(AidlConstantValue::Floating(loc, "1.0"))->Cast<float>());
+  EXPECT_EQ(true, Ptr(AidlConstantValue::Boolean(loc, true))->Cast<bool>());
+
+  AidlBinaryConstExpression one_plus_one(loc, Ptr(AidlConstantValue::Integral(loc, "1")), "+",
+                                         Ptr(AidlConstantValue::Integral(loc, "1")));
+  EXPECT_EQ(2, one_plus_one.Cast<int32_t>());
+
+  auto values = unique_ptr<vector<Ptr>>{new vector<Ptr>};
+  values->emplace_back(AidlConstantValue::String(loc, "\"hello\""));
+  values->emplace_back(AidlConstantValue::String(loc, "\"world\""));
+  vector<string> expected{"hello", "world"};
+  EXPECT_EQ(expected,
+            Ptr(AidlConstantValue::Array(loc, std::move(values)))->Cast<vector<string>>());
+}
+
 TEST_P(AidlTest, FailOnManyDefinedTypes) {
   AidlError error;
   const string expected_stderr =