Skip to content

Commit

Permalink
Remove EventsFunctionsExtension inheritance to EventsFunctionsContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H committed Jan 19, 2025
1 parent ad7ce3c commit b434886
Show file tree
Hide file tree
Showing 27 changed files with 240 additions and 175 deletions.
8 changes: 5 additions & 3 deletions Core/GDCore/IDE/ProjectBrowserHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ void ProjectBrowserHelper::ExposeEventsFunctionsExtensionEvents(
gd::Project &project, const gd::EventsFunctionsExtension &eventsFunctionsExtension,
gd::ArbitraryEventsWorker &worker) {
// Add (free) events functions
for (auto &&eventsFunction : eventsFunctionsExtension.GetInternalVector()) {
for (auto &&eventsFunction :
eventsFunctionsExtension.GetEventsFunctions().GetInternalVector()) {
worker.Launch(eventsFunction->GetEvents());
}

Expand All @@ -169,7 +170,8 @@ void ProjectBrowserHelper::ExposeEventsFunctionsExtensionEvents(
gd::Project &project, const gd::EventsFunctionsExtension &eventsFunctionsExtension,
gd::ArbitraryEventsWorkerWithContext &worker) {
// Add (free) events functions
for (auto &&eventsFunction : eventsFunctionsExtension.GetInternalVector()) {
for (auto &&eventsFunction :
eventsFunctionsExtension.GetEventsFunctions().GetInternalVector()) {
gd::ObjectsContainer parameterObjectsContainer(
gd::ObjectsContainer::SourceType::Function);
gd::VariablesContainer parameterVariablesContainer(
Expand Down Expand Up @@ -324,7 +326,7 @@ void ProjectBrowserHelper::ExposeProjectFunctions(
for (std::size_t e = 0; e < project.GetEventsFunctionsExtensionsCount();
e++) {
auto &eventsFunctionsExtension = project.GetEventsFunctionsExtension(e);
worker.Launch(eventsFunctionsExtension);
worker.Launch(eventsFunctionsExtension.GetEventsFunctions());

for (auto &&eventsBasedBehavior :
eventsFunctionsExtension.GetEventsBasedBehaviors()
Expand Down
2 changes: 1 addition & 1 deletion Core/GDCore/IDE/ProjectStripper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void GD_CORE_API ProjectStripper::StripProjectForExport(gd::Project &project) {
eventsBasedObject.GetPropertyDescriptors().GetInternalVector().clear();
}
extension.GetEventsBasedBehaviors().Clear();
extension.ClearEventsFunctions();
extension.GetEventsFunctions().ClearEventsFunctions();
}
}

Expand Down
18 changes: 11 additions & 7 deletions Core/GDCore/IDE/WholeProjectRefactorer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,8 @@ void WholeProjectRefactorer::RenameEventsFunctionsExtension(
// instructions after they are renamed.

// Free expressions
for (auto &&eventsFunction : eventsFunctionsExtension.GetInternalVector()) {
for (auto &&eventsFunction :
eventsFunctionsExtension.GetEventsFunctions().GetInternalVector()) {
if (eventsFunction->IsExpression()) {
renameEventsFunction(*eventsFunction);
}
Expand All @@ -617,7 +618,8 @@ void WholeProjectRefactorer::RenameEventsFunctionsExtension(
}

// Free instructions
for (auto &&eventsFunction : eventsFunctionsExtension.GetInternalVector()) {
for (auto &&eventsFunction :
eventsFunctionsExtension.GetEventsFunctions().GetInternalVector()) {
if (eventsFunction->IsAction() || eventsFunction->IsCondition()) {
renameEventsFunction(*eventsFunction);
}
Expand Down Expand Up @@ -697,11 +699,12 @@ void WholeProjectRefactorer::RenameEventsFunction(
gd::Project &project,
const gd::EventsFunctionsExtension &eventsFunctionsExtension,
const gd::String &oldFunctionName, const gd::String &newFunctionName) {
if (!eventsFunctionsExtension.HasEventsFunctionNamed(oldFunctionName))
const auto &eventsFunctions = eventsFunctionsExtension.GetEventsFunctions();
if (!eventsFunctions.HasEventsFunctionNamed(oldFunctionName))
return;

const gd::EventsFunction &eventsFunction =
eventsFunctionsExtension.GetEventsFunction(oldFunctionName);
eventsFunctions.GetEventsFunction(oldFunctionName);

const WholeProjectBrowser wholeProjectExposer;
DoRenameEventsFunction(
Expand All @@ -714,7 +717,7 @@ void WholeProjectRefactorer::RenameEventsFunction(

if (eventsFunction.GetFunctionType() ==
gd::EventsFunction::ExpressionAndCondition) {
for (auto &&otherFunction : eventsFunctionsExtension.GetInternalVector()) {
for (auto &&otherFunction : eventsFunctions.GetInternalVector()) {
if (otherFunction->GetFunctionType() ==
gd::EventsFunction::ActionWithOperator &&
otherFunction->GetGetterName() == oldFunctionName) {
Expand Down Expand Up @@ -884,11 +887,12 @@ void WholeProjectRefactorer::MoveEventsFunctionParameter(
const gd::EventsFunctionsExtension &eventsFunctionsExtension,
const gd::String &functionName, std::size_t oldIndex,
std::size_t newIndex) {
if (!eventsFunctionsExtension.HasEventsFunctionNamed(functionName))
const auto &eventsFunctions = eventsFunctionsExtension.GetEventsFunctions();
if (!eventsFunctions.HasEventsFunctionNamed(functionName))
return;

const gd::EventsFunction &eventsFunction =
eventsFunctionsExtension.GetEventsFunction(functionName);
eventsFunctions.GetEventsFunction(functionName);

const gd::String &eventsFunctionType =
gd::PlatformExtension::GetEventsFunctionFullType(
Expand Down
11 changes: 11 additions & 0 deletions Core/GDCore/Project/EventsFunctionsContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ class GD_CORE_API EventsFunctionsContainer

EventsFunctionsContainer(FunctionOwner source_) : owner(source_) {}

EventsFunctionsContainer(const EventsFunctionsContainer &other) {
Init(other);
}

EventsFunctionsContainer &operator=(const EventsFunctionsContainer &other) {
if (this != &other)
Init(other);

return *this;
}

/**
* \brief Get the source of the function container.
*
Expand Down
13 changes: 7 additions & 6 deletions Core/GDCore/Project/EventsFunctionsExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
namespace gd {

EventsFunctionsExtension::EventsFunctionsExtension() :
gd::EventsFunctionsContainer(
gd::EventsFunctionsContainer::FunctionOwner::Extension),
eventsFunctionsContainer(gd::EventsFunctionsContainer::FunctionOwner::Extension),
globalVariables(gd::VariablesContainer::SourceType::ExtensionGlobal),
sceneVariables(gd::VariablesContainer::SourceType::ExtensionScene) {}

EventsFunctionsExtension::EventsFunctionsExtension(
const EventsFunctionsExtension& other) :
gd::EventsFunctionsContainer(
eventsFunctionsContainer(
gd::EventsFunctionsContainer::FunctionOwner::Extension) {
Init(other);
}
Expand All @@ -48,7 +47,7 @@ void EventsFunctionsExtension::Init(const gd::EventsFunctionsExtension& other) {
previewIconUrl = other.previewIconUrl;
iconUrl = other.iconUrl;
helpPath = other.helpPath;
EventsFunctionsContainer::Init(other);
eventsFunctionsContainer = other.eventsFunctionsContainer;
eventsBasedBehaviors = other.eventsBasedBehaviors;
eventsBasedObjects = other.eventsBasedObjects;
globalVariables = other.GetGlobalVariables();
Expand Down Expand Up @@ -97,7 +96,8 @@ void EventsFunctionsExtension::SerializeTo(SerializerElement& element) const {
GetGlobalVariables().SerializeTo(element.AddChild("globalVariables"));
GetSceneVariables().SerializeTo(element.AddChild("sceneVariables"));

SerializeEventsFunctionsTo(element.AddChild("eventsFunctions"));
eventsFunctionsContainer.SerializeEventsFunctionsTo(
element.AddChild("eventsFunctions"));
eventsBasedBehaviors.SerializeElementsTo(
"eventsBasedBehavior", element.AddChild("eventsBasedBehaviors"));
eventsBasedObjects.SerializeElementsTo(
Expand Down Expand Up @@ -205,7 +205,8 @@ void EventsFunctionsExtension::UnserializeExtensionDeclarationFrom(
void EventsFunctionsExtension::UnserializeExtensionImplementationFrom(
gd::Project& project,
const SerializerElement& element) {
UnserializeEventsFunctionsFrom(project, element.GetChild("eventsFunctions"));
eventsFunctionsContainer.UnserializeEventsFunctionsFrom(
project, element.GetChild("eventsFunctions"));
eventsBasedBehaviors.UnserializeElementsFrom(
"eventsBasedBehavior", project, element.GetChild("eventsBasedBehaviors"));

Expand Down
18 changes: 17 additions & 1 deletion Core/GDCore/Project/EventsFunctionsExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace gd {
*
* \ingroup PlatformDefinition
*/
class GD_CORE_API EventsFunctionsExtension : public EventsFunctionsContainer {
class GD_CORE_API EventsFunctionsExtension {
public:
EventsFunctionsExtension();
EventsFunctionsExtension(const EventsFunctionsExtension&);
Expand Down Expand Up @@ -181,6 +181,21 @@ class GD_CORE_API EventsFunctionsExtension : public EventsFunctionsContainer {
return originIdentifier;
}

/**
* \brief Return a reference to the functions of the events based behavior or object.
*/
EventsFunctionsContainer& GetEventsFunctions() {
return eventsFunctionsContainer;
}

/**
* \brief Return a const reference to the functions of the events based
* behavior or object.
*/
const EventsFunctionsContainer& GetEventsFunctions() const {
return eventsFunctionsContainer;
}

/** \name Dependencies
*/
///@{
Expand Down Expand Up @@ -375,6 +390,7 @@ class GD_CORE_API EventsFunctionsExtension : public EventsFunctionsContainer {
std::vector<gd::DependencyMetadata> dependencies;
std::vector<gd::SourceFileMetadata> sourceFiles;

gd::EventsFunctionsContainer eventsFunctionsContainer;
gd::VariablesContainer globalVariables;
gd::VariablesContainer sceneVariables;
};
Expand Down
6 changes: 3 additions & 3 deletions Core/GDCore/Project/ProjectScopedContainers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ ProjectScopedContainers::MakeNewProjectScopedContainersForFreeEventsFunction(
gd::VariablesContainer &parameterVariablesContainer) {

gd::EventsFunctionTools::FreeEventsFunctionToObjectsContainer(
project, eventsFunctionsExtension, eventsFunction,
project, eventsFunctionsExtension.GetEventsFunctions(), eventsFunction,
parameterObjectsContainer);

ProjectScopedContainers projectScopedContainers(
Expand All @@ -91,8 +91,8 @@ ProjectScopedContainers::MakeNewProjectScopedContainersForFreeEventsFunction(
&eventsFunctionsExtension.GetSceneVariables(),
PropertiesContainersList::MakeNewEmptyPropertiesContainersList());

projectScopedContainers.AddParameters(
eventsFunction.GetParametersForEvents(eventsFunctionsExtension));
projectScopedContainers.AddParameters(eventsFunction.GetParametersForEvents(
eventsFunctionsExtension.GetEventsFunctions()));

return projectScopedContainers;
};
Expand Down
2 changes: 1 addition & 1 deletion Core/GDCore/Project/VariablesContainersList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ VariablesContainersList::MakeNewVariablesContainersListForFreeEventsFunction(
variablesContainersList.Push(extension.GetSceneVariables());

gd::EventsFunctionTools::ParametersToVariablesContainer(
eventsFunction.GetParametersForEvents(extension),
eventsFunction.GetParametersForEvents(extension.GetEventsFunctions()),
parameterVariablesContainer);
variablesContainersList.Push(parameterVariablesContainer);

Expand Down
10 changes: 6 additions & 4 deletions Core/tests/ArbitraryResourceWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,9 @@ TEST_CASE("ArbitraryResourceWorker", "[common][resources]") {
ArbitraryResourceWorkerTest worker(project.GetResourcesManager());

auto& extension = project.InsertNewEventsFunctionsExtension("MyEventExtension", 0);
auto& function = extension.InsertNewEventsFunction("MyFreeFunction", 0);

auto &function = extension.GetEventsFunctions().InsertNewEventsFunction(
"MyFreeFunction", 0);

gd::StandardEvent standardEvent;
gd::Instruction instruction;
instruction.SetType("MyExtension::DoSomethingWithResources");
Expand Down Expand Up @@ -777,8 +778,9 @@ TEST_CASE("ArbitraryResourceWorker", "[common][resources]") {
ArbitraryResourceWorkerTest worker(project.GetResourcesManager());

auto& extension = project.InsertNewEventsFunctionsExtension("MyEventExtension", 0);
auto& function = extension.InsertNewEventsFunction("MyFreeFunction", 0);

auto &function = extension.GetEventsFunctions().InsertNewEventsFunction(
"MyFreeFunction", 0);

gd::StandardEvent standardEvent;
gd::Instruction instruction;
instruction.SetType("MyExtension::DoSomethingWithResources");
Expand Down
36 changes: 19 additions & 17 deletions Core/tests/EventsFunctionsExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
TEST_CASE("EventsFunctionsExtension", "[common]") {
SECTION("Sanity checks") {
gd::EventsFunctionsExtension eventsFunctionExtension;
eventsFunctionExtension.InsertNewEventsFunction("Function1", 0);
eventsFunctionExtension.InsertNewEventsFunction("Function2", 1);
eventsFunctionExtension.InsertNewEventsFunction("Function3", 2);
auto &freeEventsFunctions = eventsFunctionExtension.GetEventsFunctions();
freeEventsFunctions.InsertNewEventsFunction("Function1", 0);
freeEventsFunctions.InsertNewEventsFunction("Function2", 1);
freeEventsFunctions.InsertNewEventsFunction("Function3", 2);
eventsFunctionExtension.GetEventsBasedBehaviors().InsertNew("MyBehavior",
0);
eventsFunctionExtension.GetEventsBasedBehaviors().InsertNew("MyBehavior2",
Expand All @@ -22,12 +23,13 @@ TEST_CASE("EventsFunctionsExtension", "[common]") {
// Check that copy operator is working
gd::EventsFunctionsExtension eventsFunctionExtension2 =
eventsFunctionExtension;
REQUIRE(eventsFunctionExtension2.GetEventsFunctionsCount() == 3);
REQUIRE(eventsFunctionExtension2.GetEventsFunction(0).GetName() ==
auto &freeEventsFunctions2 = eventsFunctionExtension2.GetEventsFunctions();
REQUIRE(freeEventsFunctions2.GetEventsFunctionsCount() == 3);
REQUIRE(freeEventsFunctions2.GetEventsFunction(0).GetName() ==
"Function1");
REQUIRE(eventsFunctionExtension2.GetEventsFunction(1).GetName() ==
REQUIRE(freeEventsFunctions2.GetEventsFunction(1).GetName() ==
"Function2");
REQUIRE(eventsFunctionExtension2.GetEventsFunction(2).GetName() ==
REQUIRE(freeEventsFunctions2.GetEventsFunction(2).GetName() ==
"Function3");
REQUIRE(eventsFunctionExtension2.GetEventsBasedBehaviors().GetCount() == 2);
REQUIRE(
Expand All @@ -39,21 +41,21 @@ TEST_CASE("EventsFunctionsExtension", "[common]") {

// Check that the copy has not somehow shared the same pointers
// to the events functions.
eventsFunctionExtension.GetEventsFunction(1).SetName("Function2.x");
eventsFunctionExtension2.GetEventsFunction(0).SetName("Function1.y");
REQUIRE(eventsFunctionExtension.GetEventsFunctionsCount() == 3);
REQUIRE(eventsFunctionExtension.GetEventsFunction(0).GetName() ==
freeEventsFunctions.GetEventsFunction(1).SetName("Function2.x");
freeEventsFunctions2.GetEventsFunction(0).SetName("Function1.y");
REQUIRE(freeEventsFunctions.GetEventsFunctionsCount() == 3);
REQUIRE(freeEventsFunctions.GetEventsFunction(0).GetName() ==
"Function1");
REQUIRE(eventsFunctionExtension.GetEventsFunction(1).GetName() ==
REQUIRE(freeEventsFunctions.GetEventsFunction(1).GetName() ==
"Function2.x");
REQUIRE(eventsFunctionExtension.GetEventsFunction(2).GetName() ==
REQUIRE(freeEventsFunctions.GetEventsFunction(2).GetName() ==
"Function3");
REQUIRE(eventsFunctionExtension2.GetEventsFunctionsCount() == 3);
REQUIRE(eventsFunctionExtension2.GetEventsFunction(0).GetName() ==
REQUIRE(freeEventsFunctions2.GetEventsFunctionsCount() == 3);
REQUIRE(freeEventsFunctions2.GetEventsFunction(0).GetName() ==
"Function1.y");
REQUIRE(eventsFunctionExtension2.GetEventsFunction(1).GetName() ==
REQUIRE(freeEventsFunctions2.GetEventsFunction(1).GetName() ==
"Function2");
REQUIRE(eventsFunctionExtension2.GetEventsFunction(2).GetName() ==
REQUIRE(freeEventsFunctions2.GetEventsFunction(2).GetName() ==
"Function3");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,8 @@ TEST_CASE("WholeProjectRefactorer::ApplyRefactoringForVariablesContainer",
auto &extension = project.InsertNewEventsFunctionsExtension("Extension", 0);
extension.GetSceneVariables().InsertNew("MySceneVariable").SetValue(123);

auto &function = extension.InsertNewEventsFunction("MyFunction", 0);
auto &function =
extension.GetEventsFunctions().InsertNewEventsFunction("MyFunction", 0);
gd::StandardEvent &event =
dynamic_cast<gd::StandardEvent &>(function.GetEvents().InsertNewEvent(
project, "BuiltinCommonInstructions::Standard"));
Expand Down
Loading

0 comments on commit b434886

Please sign in to comment.