Skip to content

Commit

Permalink
don't use exceptions for getting text node content
Browse files Browse the repository at this point in the history
  • Loading branch information
Cvolton committed Jan 19, 2024
1 parent ba6d4f7 commit 7e4e060
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 26 deletions.
8 changes: 3 additions & 5 deletions src/layers/JumpToPageLayer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "JumpToPageLayer.h"

#include "../utils.hpp"

JumpToPageLayer* JumpToPageLayer::create(InfoLayer* infoLayer){
auto ret = new JumpToPageLayer();
if (ret && ret->init(infoLayer)) {
Expand Down Expand Up @@ -64,11 +66,7 @@ void JumpToPageLayer::onReset(cocos2d::CCObject* sender)
}

int JumpToPageLayer::pageNumber(){
int pageNumber = 1;
try{
pageNumber = std::stoi(m_textNode->getString());
}catch(...){}
return pageNumber;
return BetterInfo::stoi(m_textNode->getString());
}

void JumpToPageLayer::onOK(cocos2d::CCObject* sender){
Expand Down
14 changes: 4 additions & 10 deletions src/layers/LevelFiltering/IDRangePopup.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "IDRangePopup.h"

#include "../../utils.hpp"

IDRangePopup* IDRangePopup::create(IDRangeDelegate* delegate, int min, int max, const char* text, int additional){
auto ret = new IDRangePopup();
if (ret && ret->init(delegate, min, max, text, additional)) {
Expand Down Expand Up @@ -71,17 +73,9 @@ bool IDRangePopup::init(IDRangeDelegate* delegate, int min, int max, const char*
}

int IDRangePopup::minID(){
int ret = 0;
try{
ret = std::stoi(m_minNode->getString());
}catch(...){}
return ret;
return BetterInfo::stoi(m_minNode->getString());
}

int IDRangePopup::maxID(){
int ret = 0;
try{
ret = std::stoi(m_maxNode->getString());
}catch(...){}
return ret;
return BetterInfo::stoi(m_maxNode->getString());
}
6 changes: 1 addition & 5 deletions src/layers/LevelFiltering/ProfileSearchOptionsSongSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ void ProfileSearchOptionsSongSelect::drawToggles(){
}

int ProfileSearchOptionsSongSelect::songID(){
int songID = 0;
try{
songID = std::stoi(m_textNode->getString());
}catch(...){}
return songID;
return BetterInfo::stoi(m_textNode->getString());
}

void ProfileSearchOptionsSongSelect::onSaved(CCObject* sender){
Expand Down
10 changes: 4 additions & 6 deletions src/layers/LevelIDLayer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "LevelIDLayer.h"

#include "../utils.hpp"

LevelIDLayer* LevelIDLayer::create(){
auto ret = new LevelIDLayer();
if (ret && ret->init()) {
Expand All @@ -14,11 +16,7 @@ LevelIDLayer* LevelIDLayer::create(){
}

int LevelIDLayer::getNumber(){
int pageNumber = 1;
try{
pageNumber = std::stoi(m_textNode->getString());
}catch(...){}
return pageNumber;
return BetterInfo::stoi(m_textNode->getString());
}

void LevelIDLayer::onLevelComments(cocos2d::CCObject* sender){
Expand All @@ -43,7 +41,7 @@ bool LevelIDLayer::init(){

m_textNode = CCTextInputNode::create(150, 30, "ID", "bigFont.fnt");
m_textNode->setLabelPlaceholderColor({0x75, 0xAA, 0xF0});
m_textNode->setAllowedChars("0123456789");
m_textNode->setAllowedChars("-0123456789");
m_textNode->setMaxLabelScale(0.7f);
m_textNode->setMaxLabelWidth(140);
m_textNode->setPosition({0,6});
Expand Down
2 changes: 2 additions & 0 deletions src/layers/_bases/CvoltonOptionsLayer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include "CvoltonAlertLayerStub.h"

#include "../../utils.hpp"

class CvoltonOptionsLayer : public CvoltonAlertLayerStub {
protected:
int m_toggleCount = 0;
Expand Down
6 changes: 6 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,4 +704,10 @@ AxisLayoutOptions* BetterInfo::copyLayoutOptions(AxisLayoutOptions* a) {
->setBreakLine(a->getBreakLine())
->setSameLine(a->getSameLine())
->setScalePriority(a->getScalePriority());
}

int BetterInfo::stoi(std::string_view str) {
int result = 0;
std::from_chars(str.data(), str.data() + str.size(), result);
return result;
}
2 changes: 2 additions & 0 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ namespace BetterInfo {

AxisLayoutOptions* copyLayoutOptions(CCNode* a);
AxisLayoutOptions* copyLayoutOptions(AxisLayoutOptions* a);

int stoi(std::string_view str);
}

0 comments on commit 7e4e060

Please sign in to comment.