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 <algorithm>
23 #include <string>
24 #include <vector>
25
26 namespace android {
27
AnnotationParam(const std::string & name)28 AnnotationParam::AnnotationParam(const std::string& name) : mName(name) {}
29
getName() const30 const std::string& AnnotationParam::getName() const {
31 return mName;
32 }
33
getSingleString() const34 std::string AnnotationParam::getSingleString() const {
35 std::string value = getSingleValue();
36
37 CHECK(value.size() >= 2 && value[0] == '"' && value[value.size() - 1] == '"')
38 << mName << " must be a string";
39
40 // unquote string
41 value = value.substr(1, value.size() - 2);
42
43 return value;
44 }
45
getSingleBool() const46 bool AnnotationParam::getSingleBool() const {
47 std::string value = getSingleString();
48
49 if (value == "true") {
50 return true;
51 } else if (value == "false") {
52 return false;
53 }
54
55 CHECK(false) << mName << " must be of boolean value (true/false).";
56 return false;
57 }
58
StringAnnotationParam(const std::string & name,std::vector<std::string> * values)59 StringAnnotationParam::StringAnnotationParam(const std::string& name,
60 std::vector<std::string>* values)
61 : AnnotationParam(name), mValues(values) {}
62
getValues() const63 std::vector<std::string> StringAnnotationParam::getValues() const {
64 return *mValues;
65 }
66
getSingleValue() const67 std::string StringAnnotationParam::getSingleValue() const {
68 CHECK_EQ(mValues->size(), 1u) << mName << " requires one value but has multiple";
69 return mValues->at(0);
70 }
71
Annotation(const std::string & name,AnnotationParamVector * params)72 Annotation::Annotation(const std::string& name, AnnotationParamVector* params)
73 : mName(name), mParams(params) {}
74
name() const75 std::string Annotation::name() const {
76 return mName;
77 }
78
params() const79 const AnnotationParamVector &Annotation::params() const {
80 return *mParams;
81 }
82
getParam(const std::string & name) const83 const AnnotationParam *Annotation::getParam(const std::string &name) const {
84 for (const auto* i : *mParams) {
85 if (i->getName() == name) {
86 return i;
87 }
88 }
89
90 return nullptr;
91 }
92
dump(Formatter & out) const93 void Annotation::dump(Formatter &out) const {
94 out << "@" << mName;
95
96 if (mParams->size() == 0) {
97 return;
98 }
99
100 out << "(";
101
102 for (size_t i = 0; i < mParams->size(); ++i) {
103 if (i > 0) {
104 out << ", ";
105 }
106
107 const AnnotationParam* param = mParams->at(i);
108
109 out << param->getName() << " = ";
110
111 const std::vector<std::string>& values = param->getValues();
112 if (values.size() > 1) {
113 out << "{";
114 }
115
116 out << StringHelper::JoinStrings(values, ", ");
117
118 if (values.size() > 1) {
119 out << "}";
120 }
121 }
122
123 out << ")";
124 }
125
126 } // namespace android
127
128