• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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  * Description: message definition
15  * Author: lijianzhao
16  * Create: 2022-01-19
17  */
18 
19 #ifndef MESSAGE_H
20 #define MESSAGE_H
21 
22 #include <chrono>
23 #include <functional>
24 #include <string>
25 
26 #include "cast_engine_common.h"
27 
28 namespace OHOS {
29 namespace CastEngine {
30 namespace CastEngineService {
31 class Message {
32 public:
33     int what_;
34     int arg1_{ 0 };
35     int arg2_{ 0 };
36     EventCode eventCode_{ EventCode::DEFAULT_EVENT };
37 
38     using Function = std::function<void()>;
39     Function task_ = nullptr;
40 
41     std::chrono::system_clock::time_point when_;
42 
43     // 用于保存指针类型数据
44     intptr_t ptrArg_ = -1;
45 
46     std::string strArg_;
47 
48 public:
49     Message();
50     Message(const Message &msg);
51     Message &operator=(const Message &msg);
52     explicit Message(int what);
53     Message(int what, std::string strArg);
Message(int what,std::string deviceId,EventCode eventCode)54     Message(int what, std::string deviceId, EventCode eventCode) : what_(what),
55         eventCode_(eventCode), strArg_(deviceId) {}
56     Message(int what, int arg1);
57     Message(int what, int arg1, int arg2);
58     Message(int what, int arg1, int arg2, long uptimeMillis);
59     Message(int what, int arg1, int arg2, long uptimeMillis, std::string strArg);
60     Message(int what, int arg1, std::string strArg);
61     virtual ~Message() = default;
62 
63     void SetWhen(long uptimeMillis);
64     void SetFunction(Function func);
65     void SetPtrArg(intptr_t arg);
66     void SetStrArg(std::string strArg);
67 
68     bool operator>(const Message &msg) const
69     {
70         return (this->when_ > msg.when_);
71     }
72 
73     bool operator<(const Message &msg) const
74     {
75         return (this->when_ < msg.when_);
76     }
77 
78     bool operator==(const Message &msg) const
79     {
80         return (this->what_ == msg.what_) && (this->task_ != nullptr) && (msg.task_ != nullptr);
81     }
82 
83     bool operator==(int what) const
84     {
85         return (this->what_ == what);
86     }
87 };
88 } // namespace CastEngineService
89 } // namespace CastEngine
90 } // namespace OHOS
91 
92 #endif