1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "Annotation.h"
18 
19 #include <android-base/logging.h>
20 #include <hidl-util/Formatter.h>
21 #include <hidl-util/StringHelper.h>
22 #include <vector>
23 
24 namespace android {
25 
26 
AnnotationParam(const std::string & name,std::vector<std::string> * values)27 AnnotationParam::AnnotationParam(const std::string &name,
28                 std::vector<std::string> *values)
29 : mName(name), mValues(values) {}
30 
AnnotationParam(const std::string & name,std::vector<ConstantExpression * > * values)31 AnnotationParam::AnnotationParam(const std::string &name,
32                 std::vector<ConstantExpression *> *values)
33         : mName(name) {
34     mValues = new std::vector<std::string>();
35     for(ConstantExpression *ce : *values) {
36         mValues->push_back(ce->value() + " /* " + ce->description() + " */");
37     }
38 }
39 
getName() const40 const std::string &AnnotationParam::getName() const {
41     return mName;
42 }
43 
getValues() const44 const std::vector<std::string> *AnnotationParam::getValues() const {
45     return mValues;
46 }
47 
getSingleValue() const48 const std::string &AnnotationParam::getSingleValue() const {
49     CHECK_EQ(mValues->size(), 1u) << mName << " requires one values but has multiple";
50     return mValues->at(0);
51 }
52 
getSingleString() const53 std::string AnnotationParam::getSingleString() const {
54     std::string value = getSingleValue();
55 
56     CHECK(value.size() >= 2 && value[0] == '"' && value[value.size() - 1] == '"')
57         << mName << " must be a string";
58 
59     // unquote string
60     value = value.substr(1, value.size() - 2);
61 
62     return value;
63 }
64 
getSingleBool() const65 bool AnnotationParam::getSingleBool() const {
66     std::string value = getSingleString();
67 
68     if (value == "true") {
69         return true;
70     } else if (value == "false") {
71         return false;
72     }
73 
74     CHECK(false) << mName << " must be of boolean value (true/false).";
75     return false;
76 }
77 
Annotation(const char * name,AnnotationParamVector * params)78 Annotation::Annotation(const char *name,AnnotationParamVector *params)
79         : mName(name),
80           mParams(params) {
81 }
82 
name() const83 std::string Annotation::name() const {
84     return mName;
85 }
86 
params() const87 const AnnotationParamVector &Annotation::params() const {
88     return *mParams;
89 }
90 
getParam(const std::string & name) const91 const AnnotationParam *Annotation::getParam(const std::string &name) const {
92     for (auto *i: *mParams) {
93         if (i->getName() == name) {
94             return i;
95         }
96     }
97 
98     return nullptr;
99 }
100 
dump(Formatter & out) const101 void Annotation::dump(Formatter &out) const {
102     out << "@" << mName;
103 
104     if (mParams->size() == 0) {
105         return;
106     }
107 
108     out << "(";
109 
110     for (size_t i = 0; i < mParams->size(); ++i) {
111         if (i > 0) {
112             out << ", ";
113         }
114 
115         const AnnotationParam* param = mParams->at(i);
116 
117         out << param->getName() << "=";
118 
119         const std::vector<std::string> *values = param->getValues();
120         if (values->size() > 1) {
121             out << "{";
122         }
123 
124         out << StringHelper::JoinStrings(*values, ", ");
125 
126         if (values->size() > 1) {
127             out << "}";
128         }
129     }
130 
131     out << ")";
132 }
133 
134 }  // namespace android
135 
136