change some const string refs to string views and change getID to return a ref

This commit is contained in:
dankmeme01 2024-11-10 17:47:09 +01:00
parent 084fea220e
commit 76db1268bf
2 changed files with 25 additions and 11 deletions

View file

@ -888,7 +888,7 @@ public:
* @returns The ID, or an empty string if the node has no ID. * @returns The ID, or an empty string if the node has no ID.
* @note Geode addition * @note Geode addition
*/ */
GEODE_DLL std::string getID(); GEODE_DLL const std::string& getID();
/** /**
* Set the string ID of this node. String IDs are a Geode addition * Set the string ID of this node. String IDs are a Geode addition
* that are much safer to use to get nodes than absolute indexes * that are much safer to use to get nodes than absolute indexes
@ -899,13 +899,23 @@ public:
*/ */
GEODE_DLL void setID(std::string const& id); GEODE_DLL void setID(std::string const& id);
/**
* Set the string ID of this node. String IDs are a Geode addition
* that are much safer to use to get nodes than absolute indexes
* @param id The ID of the node, recommended to be in kebab case
* without any spaces or uppercase letters. If the node is added
* by a mod, use the _spr literal to append the mod ID to it
* @note Geode addition
*/
GEODE_DLL void setID(std::string&& id);
/** /**
* Get a child by its string ID * Get a child by its string ID
* @param id ID of the child * @param id ID of the child
* @returns The child, or nullptr if none was found * @returns The child, or nullptr if none was found
* @note Geode addition * @note Geode addition
*/ */
GEODE_DLL CCNode* getChildByID(std::string const& id); GEODE_DLL CCNode* getChildByID(std::string_view id);
/** /**
* Get a child by its string ID. Recursively searches all the children * Get a child by its string ID. Recursively searches all the children
@ -913,7 +923,7 @@ public:
* @returns The child, or nullptr if none was found * @returns The child, or nullptr if none was found
* @note Geode addition * @note Geode addition
*/ */
GEODE_DLL CCNode* getChildByIDRecursive(std::string const& id); GEODE_DLL CCNode* getChildByIDRecursive(std::string_view id);
/** /**
* Get a child based on a query. Searches the child tree for a matching * Get a child based on a query. Searches the child tree for a matching
@ -929,14 +939,14 @@ public:
* ->getChildByID("mod.id/epic-button")` * ->getChildByID("mod.id/epic-button")`
* @returns The first matching node, or nullptr if none was found * @returns The first matching node, or nullptr if none was found
*/ */
GEODE_DLL CCNode* querySelector(std::string const& query); GEODE_DLL CCNode* querySelector(std::string_view query);
/** /**
* Removes a child from the container by its ID. * Removes a child from the container by its ID.
* @param id The ID of the node * @param id The ID of the node
* @note Geode addition * @note Geode addition
*/ */
GEODE_DLL void removeChildByID(std::string const& id); GEODE_DLL void removeChildByID(std::string_view id);
/** /**
* Add a child before a specified existing child * Add a child before a specified existing child

View file

@ -104,7 +104,7 @@ FieldContainer* CCNode::getFieldContainer(char const* forClass) {
return GeodeNodeMetadata::set(this)->getFieldContainer(forClass); return GeodeNodeMetadata::set(this)->getFieldContainer(forClass);
} }
std::string CCNode::getID() { const std::string& CCNode::getID() {
return GeodeNodeMetadata::set(this)->m_id; return GeodeNodeMetadata::set(this)->m_id;
} }
@ -112,7 +112,11 @@ void CCNode::setID(std::string const& id) {
GeodeNodeMetadata::set(this)->m_id = id; GeodeNodeMetadata::set(this)->m_id = id;
} }
CCNode* CCNode::getChildByID(std::string const& id) { void CCNode::setID(std::string&& id) {
GeodeNodeMetadata::set(this)->m_id = std::move(id);
}
CCNode* CCNode::getChildByID(std::string_view id) {
for (auto child : CCArrayExt<CCNode*>(this->getChildren())) { for (auto child : CCArrayExt<CCNode*>(this->getChildren())) {
if (child->getID() == id) { if (child->getID() == id) {
return child; return child;
@ -121,7 +125,7 @@ CCNode* CCNode::getChildByID(std::string const& id) {
return nullptr; return nullptr;
} }
CCNode* CCNode::getChildByIDRecursive(std::string const& id) { CCNode* CCNode::getChildByIDRecursive(std::string_view id) {
if (auto child = this->getChildByID(id)) { if (auto child = this->getChildByID(id)) {
return child; return child;
} }
@ -180,7 +184,7 @@ private:
std::unique_ptr<NodeQuery> m_next = nullptr; std::unique_ptr<NodeQuery> m_next = nullptr;
public: public:
static Result<std::unique_ptr<NodeQuery>> parse(std::string const& query) { static Result<std::unique_ptr<NodeQuery>> parse(std::string_view query) {
if (query.empty()) { if (query.empty()) {
return Err("Query may not be empty"); return Err("Query may not be empty");
} }
@ -278,7 +282,7 @@ public:
} }
}; };
CCNode* CCNode::querySelector(std::string const& queryStr) { CCNode* CCNode::querySelector(std::string_view queryStr) {
auto res = NodeQuery::parse(queryStr); auto res = NodeQuery::parse(queryStr);
if (!res) { if (!res) {
log::error("Invalid CCNode::querySelector query '{}': {}", queryStr, res.unwrapErr()); log::error("Invalid CCNode::querySelector query '{}': {}", queryStr, res.unwrapErr());
@ -289,7 +293,7 @@ CCNode* CCNode::querySelector(std::string const& queryStr) {
return query->match(this); return query->match(this);
} }
void CCNode::removeChildByID(std::string const& id) { void CCNode::removeChildByID(std::string_view id) {
if (auto child = this->getChildByID(id)) { if (auto child = this->getChildByID(id)) {
this->removeChild(child); this->removeChild(child);
} }