1/*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 "thread_state_flag.h"
17
18namespace SysTuning {
19namespace TraceStreamer {
20Direction ThreadStateFlag::SetStatByChar(char ch)
21{
22    if (ch == 'R') {
23        if (state_ == 0) {
24            return NEED_CONTINUE;
25        }
26    }
27
28    if (statMap_.find(ch) == statMap_.end()) {
29        return NEED_BREAK;
30    }
31
32    state_ |= static_cast<uint32_t>(statMap_[ch]);
33    return NEED_GO;
34}
35
36void ThreadStateFlag::ProcessSate(const std::string &stateStr)
37{
38    for (size_t i = 0; i < stateStr.size(); i++) {
39        if (stateStr[i] == '+') {
40            SetStat(Stat::TASKNEW);
41            continue;
42        }
43
44        Direction ret = SetStatByChar(stateStr[i]);
45        if (ret == NEED_CONTINUE) {
46            continue;
47        } else if (ret == NEED_BREAK) {
48            break;
49        }
50    }
51}
52
53ThreadStateFlag::ThreadStateFlag(const std::string &stateStr)
54{
55    ProcessSate(stateStr);
56}
57} // namespace TraceStreamer
58} // namespace SysTuning
59