blob: 93309ab91be7bc810885ff68d5266b77f237759a [file] [log] [blame]
Kevin Lin266f09e2020-08-19 15:18:28 +08001// 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 Chen8c4808a2018-12-05 22:05:58 +08004
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
12namespace runtime_probe {
13
Kevin Linaf1ee812020-06-23 16:53:53 +080014#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 Lin01b740f2018-12-18 18:24:08 +080026 }
Wei-Han Chen8c4808a2018-12-05 22:05:58 +080027
28using 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 Lin01b740f2018-12-18 18:24:08 +080034template <>
Kevin Linaf1ee812020-06-23 16:53:53 +080035bool 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 Chen90b034f2018-12-13 15:51:24 +080040 if (!value.is_list()) {
41 LOG(ERROR) << "failed to parse " << value << " as a list of string.";
42 return false;
43 }
44
hschamf0166d02020-02-07 11:22:09 +090045 std::vector<std::string> tmp;
Wei-Han Chen90b034f2018-12-13 15:51:24 +080046
hschamf0166d02020-02-07 11:22:09 +090047 for (const auto& v : value.GetList()) {
48 if (!v.is_string()) {
Wei-Han Chen90b034f2018-12-13 15:51:24 +080049 LOG(ERROR) << "failed to parse " << value << " as a list of string.";
50 return false;
51 }
52
hschamf0166d02020-02-07 11:22:09 +090053 tmp.push_back(v.GetString());
Wei-Han Chen90b034f2018-12-13 15:51:24 +080054 }
55 member->swap(tmp);
56 return true;
57}
58
59template <>
Kevin Linaf1ee812020-06-23 16:53:53 +080060bool ParseArgumentInternal<std::vector<std::unique_ptr<ProbeFunction>>>(
Wei-Han Chen8c4808a2018-12-05 22:05:58 +080061 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 Lin01b740f2018-12-18 18:24:08 +080067 << " as a list of probe functions.";
Wei-Han Chen8c4808a2018-12-05 22:05:58 +080068 return false;
69 }
70
hschamf0166d02020-02-07 11:22:09 +090071 std::vector<std::unique_ptr<ProbeFunction>> tmp;
Wei-Han Chen8c4808a2018-12-05 22:05:58 +080072
hschamf0166d02020-02-07 11:22:09 +090073 for (const auto& v : value.GetList()) {
74 auto ptr = runtime_probe::ProbeFunction::FromValue(v);
Wei-Han Chen8c4808a2018-12-05 22:05:58 +080075 if (!ptr) {
76 LOG(ERROR) << "failed to parse " << value
Chun-Ta Lin01b740f2018-12-18 18:24:08 +080077 << " as a list of probe functions.";
Wei-Han Chen8c4808a2018-12-05 22:05:58 +080078 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