Kevin Lin | 266f09e | 2020-08-19 15:18:28 +0800 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Wei-Han Chen | 8c4808a | 2018-12-05 22:05:58 +0800 | [diff] [blame] | 4 | |
| 5 | #include <string> |
| 6 | #include <utility> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "runtime_probe/probe_function.h" |
| 10 | #include "runtime_probe/probe_function_argument.h" |
| 11 | |
| 12 | namespace runtime_probe { |
| 13 | |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 14 | #define _DEFINE_PARSE_ARGUMENT(type, GetType) \ |
| 15 | template <> \ |
| 16 | bool ParseArgumentInternal<type>(const char* function_name, \ |
| 17 | const char* member_name, type* member, \ |
| 18 | const base::Value& value) { \ |
| 19 | if (!value.is_##type()) { \ |
| 20 | LOG(ERROR) << function_name << ": `" << member_name \ |
| 21 | << "` should be " #type; \ |
| 22 | return false; \ |
| 23 | } \ |
| 24 | *member = value.GetType(); \ |
| 25 | return true; \ |
Chun-Ta Lin | 01b740f | 2018-12-18 18:24:08 +0800 | [diff] [blame] | 26 | } |
Wei-Han Chen | 8c4808a | 2018-12-05 22:05:58 +0800 | [diff] [blame] | 27 | |
| 28 | using std::string; |
| 29 | _DEFINE_PARSE_ARGUMENT(string, GetString); |
| 30 | _DEFINE_PARSE_ARGUMENT(bool, GetBool); |
| 31 | _DEFINE_PARSE_ARGUMENT(double, GetDouble); |
| 32 | _DEFINE_PARSE_ARGUMENT(int, GetInt); |
| 33 | |
Chun-Ta Lin | 01b740f | 2018-12-18 18:24:08 +0800 | [diff] [blame] | 34 | template <> |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 35 | bool ParseArgumentInternal<std::vector<std::string>>( |
| 36 | const char* function_name, |
| 37 | const char* member_name, |
| 38 | std::vector<std::string>* member, |
| 39 | const base::Value& value) { |
Wei-Han Chen | 90b034f | 2018-12-13 15:51:24 +0800 | [diff] [blame] | 40 | if (!value.is_list()) { |
| 41 | LOG(ERROR) << "failed to parse " << value << " as a list of string."; |
| 42 | return false; |
| 43 | } |
| 44 | |
hscham | f0166d0 | 2020-02-07 11:22:09 +0900 | [diff] [blame] | 45 | std::vector<std::string> tmp; |
Wei-Han Chen | 90b034f | 2018-12-13 15:51:24 +0800 | [diff] [blame] | 46 | |
hscham | f0166d0 | 2020-02-07 11:22:09 +0900 | [diff] [blame] | 47 | for (const auto& v : value.GetList()) { |
| 48 | if (!v.is_string()) { |
Wei-Han Chen | 90b034f | 2018-12-13 15:51:24 +0800 | [diff] [blame] | 49 | LOG(ERROR) << "failed to parse " << value << " as a list of string."; |
| 50 | return false; |
| 51 | } |
| 52 | |
hscham | f0166d0 | 2020-02-07 11:22:09 +0900 | [diff] [blame] | 53 | tmp.push_back(v.GetString()); |
Wei-Han Chen | 90b034f | 2018-12-13 15:51:24 +0800 | [diff] [blame] | 54 | } |
| 55 | member->swap(tmp); |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | template <> |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 60 | bool ParseArgumentInternal<std::vector<std::unique_ptr<ProbeFunction>>>( |
Wei-Han Chen | 8c4808a | 2018-12-05 22:05:58 +0800 | [diff] [blame] | 61 | const char* function_name, |
| 62 | const char* member_name, |
| 63 | std::vector<std::unique_ptr<ProbeFunction>>* member, |
| 64 | const base::Value& value) { |
| 65 | if (!value.is_list()) { |
| 66 | LOG(ERROR) << "failed to parse " << value |
Chun-Ta Lin | 01b740f | 2018-12-18 18:24:08 +0800 | [diff] [blame] | 67 | << " as a list of probe functions."; |
Wei-Han Chen | 8c4808a | 2018-12-05 22:05:58 +0800 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
hscham | f0166d0 | 2020-02-07 11:22:09 +0900 | [diff] [blame] | 71 | std::vector<std::unique_ptr<ProbeFunction>> tmp; |
Wei-Han Chen | 8c4808a | 2018-12-05 22:05:58 +0800 | [diff] [blame] | 72 | |
hscham | f0166d0 | 2020-02-07 11:22:09 +0900 | [diff] [blame] | 73 | for (const auto& v : value.GetList()) { |
| 74 | auto ptr = runtime_probe::ProbeFunction::FromValue(v); |
Wei-Han Chen | 8c4808a | 2018-12-05 22:05:58 +0800 | [diff] [blame] | 75 | if (!ptr) { |
| 76 | LOG(ERROR) << "failed to parse " << value |
Chun-Ta Lin | 01b740f | 2018-12-18 18:24:08 +0800 | [diff] [blame] | 77 | << " as a list of probe functions."; |
Wei-Han Chen | 8c4808a | 2018-12-05 22:05:58 +0800 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | |
| 81 | tmp.push_back(std::move(ptr)); |
| 82 | } |
| 83 | |
| 84 | member->swap(tmp); |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | } // namespace runtime_probe |