Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 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 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 17 | #include "Annotation.h" |
| 18 | |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 19 | #include <hidl-util/Formatter.h> |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
| 22 | namespace android { |
| 23 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 24 | |
| 25 | AnnotationParam::AnnotationParam(const std::string &name, |
| 26 | std::vector<std::string> *values) |
| 27 | : mName(name), mValues(values) {} |
| 28 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 29 | AnnotationParam::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 Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 38 | const std::string &AnnotationParam::getName() const { |
| 39 | return mName; |
| 40 | } |
| 41 | |
| 42 | const std::vector<std::string> *AnnotationParam::getValues() const { |
| 43 | return mValues; |
| 44 | } |
| 45 | |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 46 | Annotation::Annotation(const char *name,AnnotationParamVector *params) |
| 47 | : mName(name), |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 48 | mParams(params) { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | std::string Annotation::name() const { |
| 52 | return mName; |
| 53 | } |
| 54 | |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 55 | const AnnotationParamVector &Annotation::params() const { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 56 | return *mParams; |
| 57 | } |
| 58 | |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 59 | const AnnotationParam *Annotation::getParam(const std::string &name) const { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 60 | for (auto *i: *mParams) { |
| 61 | if (i->getName() == name) { |
| 62 | return i; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return nullptr; |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 69 | void Annotation::dump(Formatter &out) const { |
| 70 | out << "@" << mName; |
| 71 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 72 | if (mParams->size() == 0) { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 73 | return; |
| 74 | } |
| 75 | |
| 76 | out << "("; |
| 77 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 78 | for (size_t i = 0; i < mParams->size(); ++i) { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 79 | if (i > 0) { |
| 80 | out << ", "; |
| 81 | } |
| 82 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 83 | const AnnotationParam* param = mParams->at(i); |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 84 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 85 | out << param->getName() << "="; |
| 86 | |
| 87 | const std::vector<std::string> *values = param->getValues(); |
| 88 | if (values->size() > 1) { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 89 | out << "{"; |
| 90 | } |
| 91 | |
| 92 | bool first = true; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 93 | for (const auto &value : *values) { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 94 | if (!first) { |
| 95 | out << ", "; |
| 96 | } |
| 97 | |
| 98 | out << value; |
| 99 | |
| 100 | first = false; |
| 101 | } |
| 102 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 103 | if (values->size() > 1) { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 104 | out << "}"; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | out << ")"; |
| 109 | } |
| 110 | |
| 111 | } // namespace android |
| 112 | |