| 1 | #include "Project/Stage/StageSwitchAccesser.h" |
|---|---|
| 2 | |
| 3 | #include "Library/Base/StringUtil.h" |
| 4 | #include "Library/Placement/PlacementId.h" |
| 5 | #include "Library/Stage/StageSwitchDirector.h" |
| 6 | |
| 7 | namespace al { |
| 8 | |
| 9 | StageSwitchAccesser::StageSwitchAccesser() = default; |
| 10 | |
| 11 | bool StageSwitchAccesser::init(StageSwitchDirector* director, const char* linkName, |
| 12 | const PlacementId& placementId) { |
| 13 | mStageSwitchDirector = director; |
| 14 | mLinkName = linkName; |
| 15 | mPlacementId = new PlacementId(placementId); |
| 16 | |
| 17 | if (isMatchString(linkName, MatchStr{.str: "*On"}) || isMatchString(linkName, MatchStr{.str: "*Off"})) |
| 18 | mSwitchKind = SwitchKind::Write; |
| 19 | else |
| 20 | mSwitchKind = SwitchKind::Read; |
| 21 | mInfoIndex = mStageSwitchDirector->useSwitch(accesser: this); |
| 22 | return isValid(); |
| 23 | } |
| 24 | |
| 25 | StageSwitchDirector* StageSwitchAccesser::getStageSwitchDirector() const { |
| 26 | return mStageSwitchDirector; |
| 27 | } |
| 28 | |
| 29 | bool StageSwitchAccesser::isValid() const { |
| 30 | return mInfoIndex >= 0; |
| 31 | } |
| 32 | |
| 33 | void StageSwitchAccesser::onSwitch() { |
| 34 | if (isValid()) |
| 35 | mStageSwitchDirector->onSwitch(accesser: this); |
| 36 | } |
| 37 | |
| 38 | void StageSwitchAccesser::offSwitch() { |
| 39 | if (isValid()) |
| 40 | mStageSwitchDirector->offSwitch(accesser: this); |
| 41 | } |
| 42 | |
| 43 | bool StageSwitchAccesser::isOnSwitch() const { |
| 44 | return mStageSwitchDirector->isOnSwitch(accesser: this); |
| 45 | } |
| 46 | |
| 47 | bool StageSwitchAccesser::isEnableRead() const { |
| 48 | return mSwitchKind == SwitchKind::Read || mSwitchKind == SwitchKind::Write; |
| 49 | } |
| 50 | |
| 51 | bool StageSwitchAccesser::isEnableWrite() const { |
| 52 | return mSwitchKind == SwitchKind::Write; |
| 53 | } |
| 54 | |
| 55 | bool StageSwitchAccesser::isEqualSwitch(const StageSwitchAccesser* other) const { |
| 56 | if (!other) |
| 57 | return false; |
| 58 | |
| 59 | return mInfoIndex == other->mInfoIndex; |
| 60 | } |
| 61 | |
| 62 | void StageSwitchAccesser::addListener(StageSwitchListener* listener) { |
| 63 | mStageSwitchDirector->addListener(listener, accesser: this); |
| 64 | } |
| 65 | |
| 66 | } // namespace al |
| 67 |