1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-22 11:44:17 +01:00

Codechange: Remove CCountedPtr.

This was originally generic and used by YAPF, but now it is used only by script objects.

CCountedPtr provided much more (untested) functionality than used.

ScriptObjectRef already exists for script objects and does the same thing, so use this instead.
This commit is contained in:
Peter Nelson
2024-10-16 00:16:43 +01:00
committed by Peter Nelson
parent d8029b1d84
commit 8360fab18a
18 changed files with 66 additions and 282 deletions

View File

@@ -37,7 +37,7 @@
/* static */ bool ScriptBaseStation::SetName(StationID station_id, Text *name)
{
CCountedPtr<Text> counter(name);
ScriptObjectRef counter(name);
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidBaseStation(station_id));

View File

@@ -46,7 +46,7 @@
/* static */ bool ScriptCompany::SetName(Text *name)
{
CCountedPtr<Text> counter(name);
ScriptObjectRef counter(name);
EnforceCompanyModeValid(false);
EnforcePrecondition(false, name != nullptr);
@@ -68,7 +68,7 @@
/* static */ bool ScriptCompany::SetPresidentName(Text *name)
{
CCountedPtr<Text> counter(name);
ScriptObjectRef counter(name);
EnforceCompanyModeValid(false);
EnforcePrecondition(false, name != nullptr);

View File

@@ -44,7 +44,7 @@
/* static */ ScriptGoal::GoalID ScriptGoal::New(ScriptCompany::CompanyID company, Text *goal, GoalType type, SQInteger destination)
{
CCountedPtr<Text> counter(goal);
ScriptObjectRef counter(goal);
EnforceDeityMode(GOAL_INVALID);
EnforcePrecondition(GOAL_INVALID, goal != nullptr);
@@ -79,7 +79,7 @@
/* static */ bool ScriptGoal::SetText(GoalID goal_id, Text *goal)
{
CCountedPtr<Text> counter(goal);
ScriptObjectRef counter(goal);
EnforcePrecondition(false, IsValidGoal(goal_id));
EnforceDeityMode(false);
@@ -92,7 +92,7 @@
/* static */ bool ScriptGoal::SetProgress(GoalID goal_id, Text *progress)
{
CCountedPtr<Text> counter(progress);
ScriptObjectRef counter(progress);
EnforcePrecondition(false, IsValidGoal(goal_id));
EnforceDeityMode(false);
@@ -119,7 +119,7 @@
/* static */ bool ScriptGoal::DoQuestion(SQInteger uniqueid, uint32_t target, bool is_client, Text *question, QuestionType type, SQInteger buttons)
{
CCountedPtr<Text> counter(question);
ScriptObjectRef counter(question);
EnforceDeityMode(false);
EnforcePrecondition(false, question != nullptr);

View File

@@ -56,7 +56,7 @@
/* static */ bool ScriptGroup::SetName(GroupID group_id, Text *name)
{
CCountedPtr<Text> counter(name);
ScriptObjectRef counter(name);
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsValidGroup(group_id));

View File

@@ -58,7 +58,7 @@
/* static */ bool ScriptIndustry::SetText(IndustryID industry_id, Text *text)
{
CCountedPtr<Text> counter(text);
ScriptObjectRef counter(text);
EnforceDeityMode(false);
EnforcePrecondition(false, IsValidIndustry(industry_id));
@@ -305,7 +305,7 @@
/* static */ bool ScriptIndustry::SetProductionLevel(IndustryID industry_id, SQInteger prod_level, bool show_news, Text *custom_news)
{
CCountedPtr<Text> counter(custom_news);
ScriptObjectRef counter(custom_news);
EnforceDeityMode(false);
EnforcePrecondition(false, IsValidIndustry(industry_id));

View File

@@ -26,9 +26,9 @@
/* static */ ScriptLeagueTable::LeagueTableID ScriptLeagueTable::New(Text *title, Text *header, Text *footer)
{
CCountedPtr<Text> title_counter(title);
CCountedPtr<Text> header_counter(header);
CCountedPtr<Text> footer_counter(footer);
ScriptObjectRef title_counter(title);
ScriptObjectRef header_counter(header);
ScriptObjectRef footer_counter(footer);
EnforceDeityMode(LEAGUE_TABLE_INVALID);
EnforcePrecondition(LEAGUE_TABLE_INVALID, title != nullptr);
@@ -51,8 +51,8 @@
/* static */ ScriptLeagueTable::LeagueTableElementID ScriptLeagueTable::NewElement(ScriptLeagueTable::LeagueTableID table, SQInteger rating, ScriptCompany::CompanyID company, Text *text, Text *score, LinkType link_type, SQInteger link_target)
{
CCountedPtr<Text> text_counter(text);
CCountedPtr<Text> score_counter(score);
ScriptObjectRef text_counter(text);
ScriptObjectRef score_counter(score);
EnforceDeityMode(LEAGUE_TABLE_ELEMENT_INVALID);
@@ -80,7 +80,7 @@
/* static */ bool ScriptLeagueTable::UpdateElementData(LeagueTableElementID element, ScriptCompany::CompanyID company, Text *text, LinkType link_type, SQInteger link_target)
{
CCountedPtr<Text> text_counter(text);
ScriptObjectRef text_counter(text);
EnforceDeityMode(false);
EnforcePrecondition(false, IsValidLeagueTableElement(element));
@@ -100,7 +100,7 @@
/* static */ bool ScriptLeagueTable::UpdateElementScore(LeagueTableElementID element, SQInteger rating, Text *score)
{
CCountedPtr<Text> score_counter(score);
ScriptObjectRef score_counter(score);
EnforceDeityMode(false);
EnforcePrecondition(false, IsValidLeagueTableElement(element));

View File

@@ -22,7 +22,7 @@
/* static */ bool ScriptNews::Create(NewsType type, Text *text, ScriptCompany::CompanyID company, NewsReferenceType ref_type, SQInteger reference)
{
CCountedPtr<Text> counter(text);
ScriptObjectRef counter(text);
EnforceDeityMode(false);
EnforcePrecondition(false, text != nullptr);

View File

@@ -25,6 +25,21 @@
#include "../../safeguards.h"
void SimpleCountedObject::Release()
{
int32_t res = --this->ref_count;
assert(res >= 0);
if (res == 0) {
try {
this->FinalRelease(); // may throw, for example ScriptTest/ExecMode
} catch (...) {
delete this;
throw;
}
delete this;
}
}
/**
* Get the storage associated with the current ScriptInstance.
* @return The storage.

View File

@@ -10,7 +10,6 @@
#ifndef SCRIPT_OBJECT_HPP
#define SCRIPT_OBJECT_HPP
#include "../../misc/countedptr.hpp"
#include "../../road_type.h"
#include "../../rail_type.h"
#include "../../string_func.h"
@@ -34,6 +33,27 @@ typedef bool (ScriptModeProc)();
*/
typedef bool (ScriptAsyncModeProc)();
/**
* Simple counted object. Use it as base of your struct/class if you want to use
* basic reference counting. Your struct/class will destroy and free itself when
* last reference to it is released (using Release() method). The initial reference
* count (when it is created) is zero (don't forget AddRef() at least one time if
* not using ScriptObjectRef.
* @api -all
*/
class SimpleCountedObject {
public:
SimpleCountedObject() : ref_count(0) {}
virtual ~SimpleCountedObject() {}
inline void AddRef() { ++this->ref_count; }
void Release();
virtual void FinalRelease() {};
private:
int32_t ref_count;
};
/**
* Uper-parent object of all API classes. You should never use this class in
* your script, as it doesn't publish any public functions. It is used
@@ -406,7 +426,7 @@ public:
*/
ScriptObjectRef(T *data) : data(data)
{
this->data->AddRef();
if (this->data != nullptr) this->data->AddRef();
}
/* No copy constructor. */

View File

@@ -35,7 +35,7 @@
/* static */ bool ScriptSign::SetName(SignID sign_id, Text *name)
{
CCountedPtr<Text> counter(name);
ScriptObjectRef counter(name);
EnforceDeityOrCompanyModeValid(false);
EnforcePrecondition(false, IsValidSign(sign_id));
@@ -72,7 +72,7 @@
/* static */ SignID ScriptSign::BuildSign(TileIndex location, Text *name)
{
CCountedPtr<Text> counter(name);
ScriptObjectRef counter(name);
EnforceDeityOrCompanyModeValid(INVALID_SIGN);
EnforcePrecondition(INVALID_SIGN, ::IsValidTile(location));

View File

@@ -45,7 +45,7 @@ static inline bool StoryPageElementTypeRequiresText(StoryPageElementType type)
/* static */ ScriptStoryPage::StoryPageID ScriptStoryPage::New(ScriptCompany::CompanyID company, Text *title)
{
CCountedPtr<Text> counter(title);
ScriptObjectRef counter(title);
EnforceDeityMode(STORY_PAGE_INVALID);
EnforcePrecondition(STORY_PAGE_INVALID, company == ScriptCompany::COMPANY_INVALID || ScriptCompany::ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID);
@@ -62,7 +62,7 @@ static inline bool StoryPageElementTypeRequiresText(StoryPageElementType type)
/* static */ ScriptStoryPage::StoryPageElementID ScriptStoryPage::NewElement(StoryPageID story_page_id, StoryPageElementType type, SQInteger reference, Text *text)
{
CCountedPtr<Text> counter(text);
ScriptObjectRef counter(text);
::StoryPageElementType btype = static_cast<::StoryPageElementType>(type);
@@ -109,7 +109,7 @@ static inline bool StoryPageElementTypeRequiresText(StoryPageElementType type)
/* static */ bool ScriptStoryPage::UpdateElement(StoryPageElementID story_page_element_id, SQInteger reference, Text *text)
{
CCountedPtr<Text> counter(text);
ScriptObjectRef counter(text);
EnforceDeityMode(false);
EnforcePrecondition(false, IsValidStoryPageElement(story_page_element_id));
@@ -165,7 +165,7 @@ static inline bool StoryPageElementTypeRequiresText(StoryPageElementType type)
/* static */ bool ScriptStoryPage::SetTitle(StoryPageID story_page_id, Text *title)
{
CCountedPtr<Text> counter(title);
ScriptObjectRef counter(title);
EnforcePrecondition(false, IsValidStoryPage(story_page_id));
EnforceDeityMode(false);

View File

@@ -42,7 +42,7 @@
/* static */ bool ScriptTown::SetName(TownID town_id, Text *name)
{
CCountedPtr<Text> counter(name);
ScriptObjectRef counter(name);
EnforceDeityMode(false);
EnforcePrecondition(false, IsValidTown(town_id));
@@ -57,7 +57,7 @@
/* static */ bool ScriptTown::SetText(TownID town_id, Text *text)
{
CCountedPtr<Text> counter(text);
ScriptObjectRef counter(text);
EnforceDeityMode(false);
EnforcePrecondition(false, IsValidTown(town_id));
@@ -283,7 +283,7 @@
/* static */ bool ScriptTown::FoundTown(TileIndex tile, TownSize size, bool city, RoadLayout layout, Text *name)
{
CCountedPtr<Text> counter(name);
ScriptObjectRef counter(name);
EnforceDeityOrCompanyModeValid(false);
EnforcePrecondition(false, ScriptCompanyMode::IsDeity() || _settings_game.economy.found_town != TF_FORBIDDEN);

View File

@@ -247,7 +247,7 @@
/* static */ bool ScriptVehicle::SetName(VehicleID vehicle_id, Text *name)
{
CCountedPtr<Text> counter(name);
ScriptObjectRef counter(name);
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsPrimaryVehicle(vehicle_id));