• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef DPROF_CONVERTER_FEATURES_MANAGER_H
17 #define DPROF_CONVERTER_FEATURES_MANAGER_H
18 
19 #include "dprof/storage.h"
20 #include "utils/logger.h"
21 
22 #include <vector>
23 #include <unordered_map>
24 #include <functional>
25 
26 namespace ark::dprof {
27 class FeaturesManager {
28 public:
29     struct Functor {
30         virtual bool operator()(const AppData &appData, const std::vector<uint8_t> &data) = 0;
31     };
32 
RegisterFeature(const std::string & featureName,Functor & functor)33     bool RegisterFeature(const std::string &featureName, Functor &functor)
34     {
35         auto it = map_.find(featureName);
36         if (it != map_.end()) {
37             LOG(ERROR, DPROF) << "Feature already exists, featureName=" << featureName;
38             return false;
39         }
40         map_.insert({featureName, functor});
41         return true;
42     }
43 
UnregisterFeature(const std::string & featureName)44     bool UnregisterFeature(const std::string &featureName)
45     {
46         if (map_.erase(featureName) != 1) {
47             LOG(ERROR, DPROF) << "Feature does not exist, featureName=" << featureName;
48             return false;
49         }
50         return true;
51     }
52 
ProcessingFeature(const AppData & appData,const std::string & featureName,const std::vector<uint8_t> & data)53     bool ProcessingFeature(const AppData &appData, const std::string &featureName,
54                            const std::vector<uint8_t> &data) const
55     {
56         auto it = map_.find(featureName);
57         if (it == map_.end()) {
58             LOG(ERROR, DPROF) << "Feature is not supported, featureName=" << featureName;
59             return false;
60         }
61 
62         return it->second(appData, data);
63     }
64 
ProcessingFeatures(const AppData & appData)65     bool ProcessingFeatures(const AppData &appData) const
66     {
67         for (const auto &it : appData.GetFeaturesMap()) {
68             if (!ProcessingFeature(appData, it.first, it.second)) {
69                 LOG(ERROR, DPROF) << "Cannot processing feature: " << it.first << ", app: " << appData.GetName();
70                 return false;
71             }
72         }
73         return true;
74     }
75 
76 private:
77     std::unordered_map<std::string, Functor &> map_;
78 };
79 }  // namespace ark::dprof
80 
81 #endif  // DPROF_CONVERTER_FEATURES_MANAGER_H
82