• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 FRAMEWORKS_VSYNC_INCLUDE_RETURN_VALUE_TESTER_H
17 #define FRAMEWORKS_VSYNC_INCLUDE_RETURN_VALUE_TESTER_H
18 
19 #include <any>
20 #include <refbase.h>
21 #include <mutex>
22 #include <map>
23 
24 namespace OHOS {
25 namespace Vsync {
26 class ReturnValueTester : public RefBase {
27 public:
28     static sptr<ReturnValueTester> GetInstance();
29     void SetValue(int next, const std::any &rval);
30     std::any GetValue();
31     bool HasReturnValue();
32 
33     template<class T>
Set(const T & next,const T & returnvalue)34     static void Set(const T &next, const T &returnvalue)
35     {
36         ReturnValueTester::GetInstance()->SetValue(next, returnvalue);
37     }
38 
39     template<class T>
Get(const T & b)40     static T Get(const T &b)
41     {
42         auto tester = ReturnValueTester::GetInstance();
43         if (tester->HasReturnValue()) {
44             T ret = b;
45             const std::any &retval = tester->GetValue();
46             auto pRet = std::any_cast<T>(&retval);
47             if (pRet != nullptr) {
48                 ret = *pRet;
49             } else {
50                 fprintf(stderr, "pRet is null\n");
51             }
52             return ret;
53         } else {
54             return b;
55         }
56     }
57 
58 private:
59     ReturnValueTester() = default;
60     virtual ~ReturnValueTester() = default;
61     static inline sptr<ReturnValueTester> instance = nullptr;
62     std::map<int, std::any> anyMap;
63     int id = 0;
64 };
65 } // namespace Vsync
66 } // namespace OHOS
67 
68 #endif // FRAMEWORKS_VSYNC_INCLUDE_RETURN_VALUE_TESTER_H
69