1 /*
2  * Copyright (c) 2023 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 STATE_PATTERN_NAIVE_H
17 #define STATE_PATTERN_NAIVE_H
18 
19 /**
20  * Highly not recommanded to inlcude this header in a header file.
21  * When using naive state design pattern, please define `STATE_PATTERN_NAIVE_STATE`
22  *   in the **cpp** file. And then include this header in the **cpp** file.
23  * If an error log is needed, please define `STATE_PATTERN_NAIVE_LOGGER`.
24 **/
25 #ifdef STATE_PATTERN_NAIVE_STATE
26 
27 #define STATE_PATTERN_NAIVE_STATE_SET_AND_RETURN(state, returnVal)     \
28 do {                                                                   \
29     STATE_PATTERN_NAIVE_STATE = state;                                 \
30     return returnVal;                                                  \
31 } while (0)                                                            \
32 
33 #define STATE_PATTERN_NAIVE_ACCEPT(state, returnVal)     \
34 do {                                                     \
35     if (STATE_PATTERN_NAIVE_STATE != state) {            \
36         return returnVal;                                \
37     }                                                    \
38 } while (0)                                              \
39 
40 #define STATE_PATTERN_NAIVE_REJECT(state, returnVal)     \
41 do {                                                     \
42     if (STATE_PATTERN_NAIVE_STATE == state) {            \
43         return returnVal;                                \
44     }                                                    \
45 } while (0)                                              \
46 
47 #ifdef STATE_PATTERN_NAIVE_LOGGER
48 #define STATE_PATTERN_NAIVE_ACCEPT_LOG(state, returnVal, ...)        \
49 do {                                                                 \
50     if (STATE_PATTERN_NAIVE_STATE != state) {                        \
51         STATE_PATTERN_NAIVE_LOGGER(__VA_ARGS__);                     \
52         return returnVal;                                            \
53     }                                                                \
54 } while (0)                                                          \
55 
56 #define STATE_PATTERN_NAIVE_REJECT_LOG(state, returnVal, ...)        \
57 do {                                                                 \
58     if (STATE_PATTERN_NAIVE_STATE == state) {                        \
59         STATE_PATTERN_NAIVE_LOGGER(__VA_ARGS__);                     \
60         return returnVal;                                            \
61     }                                                                \
62 } while (0)                                                          \
63 
64 #endif // STATE_PATTERN_NAIVE_LOGGER
65 #endif // STATE_PATTERN_NAIVE_STATE
66 #endif // STATE_PATTERN_NAIVE_H
67