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 #include "ievent.h"
17 
18 #include "db_errno.h"
19 #include "event_impl.h"
20 
21 namespace DistributedDB {
CreateEvent(EventTime timeout,int & errCode)22 IEvent *IEvent::CreateEvent(EventTime timeout, int &errCode)
23 {
24     if (timeout < 0) {
25         errCode = -E_INVALID_ARGS;
26         return nullptr;
27     }
28 
29     IEvent *event = new (std::nothrow) EventImpl(timeout);
30     if (event == nullptr) {
31         errCode = -E_OUT_OF_MEMORY;
32         return nullptr;
33     }
34     errCode = E_OK;
35     return event;
36 }
37 
CreateEvent(EventFd fd,EventsMask events,EventTime timeout,int & errCode)38 IEvent *IEvent::CreateEvent(EventFd fd, EventsMask events,
39     EventTime timeout, int &errCode)
40 {
41     errCode = -E_INVALID_ARGS;
42     if (!events) {
43         return nullptr;
44     }
45     if ((events & ET_TIMEOUT) && (timeout < 0)) {
46         return nullptr;
47     }
48     if (!(events & ET_TIMEOUT) && !fd.IsValid()) {
49         return nullptr;
50     }
51 
52     IEvent *event = new (std::nothrow) EventImpl(fd, events, timeout);
53     if (event == nullptr) {
54         errCode = -E_OUT_OF_MEMORY;
55         return nullptr;
56     }
57     errCode = E_OK;
58     return event;
59 }
60 } // namespace DistributedDB
61