blob: 7b630548426cbd86b776eec1953b87f3a0f699c9 [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
29const std::string &AnnotationParam::getName() const {
30 return mName;
31}
32
33const std::vector<std::string> *AnnotationParam::getValues() const {
34 return mValues;
35}
36
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070037Annotation::Annotation(const char *name,AnnotationParamVector *params)
38 : mName(name),
Steven Morelandd537ab02016-09-12 10:32:01 -070039 mParams(params) {
Andreas Huber3599d922016-08-09 10:42:57 -070040}
41
42std::string Annotation::name() const {
43 return mName;
44}
45
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070046const AnnotationParamVector &Annotation::params() const {
Steven Morelandd537ab02016-09-12 10:32:01 -070047 return *mParams;
48}
49
50const AnnotationParam *Annotation::getParam(const std::string &name) {
51 for (auto *i: *mParams) {
52 if (i->getName() == name) {
53 return i;
54 }
55 }
56
57 return nullptr;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070058}
59
Andreas Huber3599d922016-08-09 10:42:57 -070060void Annotation::dump(Formatter &out) const {
61 out << "@" << mName;
62
Steven Morelandd537ab02016-09-12 10:32:01 -070063 if (mParams->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -070064 return;
65 }
66
67 out << "(";
68
Steven Morelandd537ab02016-09-12 10:32:01 -070069 for (size_t i = 0; i < mParams->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -070070 if (i > 0) {
71 out << ", ";
72 }
73
Steven Morelandd537ab02016-09-12 10:32:01 -070074 const AnnotationParam* param = mParams->at(i);
Andreas Huber3599d922016-08-09 10:42:57 -070075
Steven Morelandd537ab02016-09-12 10:32:01 -070076 out << param->getName() << "=";
77
78 const std::vector<std::string> *values = param->getValues();
79 if (values->size() > 1) {
Andreas Huber3599d922016-08-09 10:42:57 -070080 out << "{";
81 }
82
83 bool first = true;
Steven Morelandd537ab02016-09-12 10:32:01 -070084 for (const auto &value : *values) {
Andreas Huber3599d922016-08-09 10:42:57 -070085 if (!first) {
86 out << ", ";
87 }
88
89 out << value;
90
91 first = false;
92 }
93
Steven Morelandd537ab02016-09-12 10:32:01 -070094 if (values->size() > 1) {
Andreas Huber3599d922016-08-09 10:42:57 -070095 out << "}";
96 }
97 }
98
99 out << ")";
100}
101
102} // namespace android
103