blob: d3b4d01ff4661ba2bc2d3163f08c41d71ff3f0ed [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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
Andreas Huber3599d922016-08-09 10:42:57 -070017#include "Annotation.h"
18
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070019#include <hidl-util/Formatter.h>
Andreas Huber3599d922016-08-09 10:42:57 -070020#include <vector>
21
22namespace android {
23
Steven Morelandd537ab02016-09-12 10:32:01 -070024
25AnnotationParam::AnnotationParam(const std::string &name,
26 std::vector<std::string> *values)
27: mName(name), mValues(values) {}
28
Yifan Hongf24fa852016-09-23 11:03:15 -070029AnnotationParam::AnnotationParam(const std::string &name,
30 std::vector<ConstantExpression *> *values)
31 : mName(name) {
32 mValues = new std::vector<std::string>();
33 for(ConstantExpression *ce : *values) {
34 mValues->push_back(ce->value() + " /* " + ce->description() + " */");
35 }
36}
37
Steven Morelandd537ab02016-09-12 10:32:01 -070038const std::string &AnnotationParam::getName() const {
39 return mName;
40}
41
42const std::vector<std::string> *AnnotationParam::getValues() const {
43 return mValues;
44}
45
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070046Annotation::Annotation(const char *name,AnnotationParamVector *params)
47 : mName(name),
Steven Morelandd537ab02016-09-12 10:32:01 -070048 mParams(params) {
Andreas Huber3599d922016-08-09 10:42:57 -070049}
50
51std::string Annotation::name() const {
52 return mName;
53}
54
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070055const AnnotationParamVector &Annotation::params() const {
Steven Morelandd537ab02016-09-12 10:32:01 -070056 return *mParams;
57}
58
Andreas Huber019d21d2016-10-03 12:59:47 -070059const AnnotationParam *Annotation::getParam(const std::string &name) const {
Steven Morelandd537ab02016-09-12 10:32:01 -070060 for (auto *i: *mParams) {
61 if (i->getName() == name) {
62 return i;
63 }
64 }
65
66 return nullptr;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070067}
68
Andreas Huber3599d922016-08-09 10:42:57 -070069void Annotation::dump(Formatter &out) const {
70 out << "@" << mName;
71
Steven Morelandd537ab02016-09-12 10:32:01 -070072 if (mParams->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -070073 return;
74 }
75
76 out << "(";
77
Steven Morelandd537ab02016-09-12 10:32:01 -070078 for (size_t i = 0; i < mParams->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -070079 if (i > 0) {
80 out << ", ";
81 }
82
Steven Morelandd537ab02016-09-12 10:32:01 -070083 const AnnotationParam* param = mParams->at(i);
Andreas Huber3599d922016-08-09 10:42:57 -070084
Steven Morelandd537ab02016-09-12 10:32:01 -070085 out << param->getName() << "=";
86
87 const std::vector<std::string> *values = param->getValues();
88 if (values->size() > 1) {
Andreas Huber3599d922016-08-09 10:42:57 -070089 out << "{";
90 }
91
92 bool first = true;
Steven Morelandd537ab02016-09-12 10:32:01 -070093 for (const auto &value : *values) {
Andreas Huber3599d922016-08-09 10:42:57 -070094 if (!first) {
95 out << ", ";
96 }
97
98 out << value;
99
100 first = false;
101 }
102
Steven Morelandd537ab02016-09-12 10:32:01 -0700103 if (values->size() > 1) {
Andreas Huber3599d922016-08-09 10:42:57 -0700104 out << "}";
105 }
106 }
107
108 out << ")";
109}
110
111} // namespace android
112