Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup classes handling Events #2010

Merged
merged 3 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 2 additions & 23 deletions arcane/src/arcane/tests/UtilsUnitTest.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
//-----------------------------------------------------------------------------
// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
// Copyright 2000-2025 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: Apache-2.0
//-----------------------------------------------------------------------------
/*---------------------------------------------------------------------------*/
/* UtilsUnitTest.cc (C) 2000-2024 */
/* UtilsUnitTest.cc (C) 2000-2025 */
/* */
/* Test des fonctions utilitaires de Arcane. */
/*---------------------------------------------------------------------------*/
Expand All @@ -16,7 +16,6 @@
#include "arcane/utils/ArgumentException.h"
#include "arcane/utils/AutoDestroyUserData.h"
#include "arcane/utils/HPReal.h"
#include "arcane/utils/IFunctorWithArgument.h"
#include "arcane/utils/IMemoryInfo.h"
#include "arcane/utils/ITraceMng.h"
#include "arcane/utils/PlatformUtils.h"
Expand All @@ -33,19 +32,11 @@
#include "arcane/core/FactoryService.h"
#include "arcane/core/ServiceBuilder.h"

#include "arcane/tests/ArcaneTestGlobal.h"

#include <fenv.h>

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

namespace Arcane
{
extern "C++" ARCANE_UTILS_EXPORT void
_internalTestEvent();
}

namespace ArcaneTest
{

Expand Down Expand Up @@ -119,7 +110,6 @@ class UtilsUnitTest
void _testStackTrace();
void _testSetClassConfig();
void _testFloatingException();
void _testEvents();
void _testConvertFromString();
void _testConvertFromStringToInt32Array();
void _testCommandLine();
Expand Down Expand Up @@ -164,7 +154,6 @@ executeTest()
_testUserData();
_testHPReal();
_testTruncateReal();
_testEvents();

info() << Trace::Color::darkRed() << "[TEST_COLOR] DARK_RED";
info() << Trace::Color::darkGreen() << "[TEST_COLOR] DARK_GREEN";
Expand Down Expand Up @@ -760,16 +749,6 @@ _printMemoryInfos()
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

void UtilsUnitTest::
_testEvents()
{
info() << "INTERNAL TEST EVENT";
_internalTestEvent();
}

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

void UtilsUnitTest::
_testConvertFromString()
{
Expand Down
134 changes: 24 additions & 110 deletions arcane/src/arcane/utils/Event.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
//-----------------------------------------------------------------------------
// Copyright 2000-2023 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
// Copyright 2000-2025 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: Apache-2.0
//-----------------------------------------------------------------------------
/*---------------------------------------------------------------------------*/
/* Event.cc (C) 2000-2023 */
/* Event.cc (C) 2000-2025 */
/* */
/* Gestionnaires d'évènements. */
/*---------------------------------------------------------------------------*/
Expand Down Expand Up @@ -37,19 +37,13 @@ namespace Arcane
class EventObservableBase::Impl
{
public:
Impl(){}
public:
void rebuildOberversArray()
{
m_observers_array.clear();
m_observers_array.reserve(arcaneCheckArraySize(m_observers.size()));
for( auto o : m_observers )
m_observers_array.add(o);
}

Impl() {}

public:

std::set<EventObserverBase*> m_auto_destroy_observers;
std::set<EventObserverBase*> m_observers;
UniqueArray<EventObserverBase*> m_observers_array;
};

/*---------------------------------------------------------------------------*/
Expand Down Expand Up @@ -80,14 +74,26 @@ EventObservableBase::
/*---------------------------------------------------------------------------*/

void EventObservableBase::
_attachObserver(EventObserverBase* obs,bool is_auto_destroy)
_rebuildObserversArray()
{
m_observers_array.clear();
m_observers_array.reserve(m_p->m_observers.size());
for (auto o : m_p->m_observers)
m_observers_array.add(o);
}

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

void EventObservableBase::
_attachObserver(EventObserverBase* obs, bool is_auto_destroy)
{
// Vérifie que l'observeur n'est pas dans la liste.
if (m_p->m_observers.find(obs)!=m_p->m_observers.end())
if (m_p->m_observers.find(obs) != m_p->m_observers.end())
ARCANE_FATAL("Observer is already attached to this observable");
obs->_notifyAttach(this);
m_p->m_observers.insert(obs);
m_p->rebuildOberversArray();
_rebuildObserversArray();
if (is_auto_destroy)
m_p->m_auto_destroy_observers.insert(obs);
}
Expand All @@ -102,8 +108,8 @@ _detachObserver(EventObserverBase* obs)
// dynamiquement. Il n'y a donc pas besoin de mettre à jour
// m_p->m_auto_destroy_observers.
bool is_ok = false;
for( auto o : m_p->m_observers )
if (o==obs){
for (auto o : m_p->m_observers)
if (o == obs) {
m_p->m_observers.erase(o);
is_ok = true;
break;
Expand All @@ -113,16 +119,7 @@ _detachObserver(EventObserverBase* obs)
if (!is_ok)
ARCANE_FATAL("observer is not registered to this observable");
obs->_notifyDetach();
m_p->rebuildOberversArray();
}

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

ConstArrayView<EventObserverBase*> EventObservableBase::
_observers() const
{
return m_p->m_observers_array;
_rebuildObserversArray();
}

/*---------------------------------------------------------------------------*/
Expand All @@ -138,16 +135,6 @@ detachAllObservers()
delete o;
}

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

bool EventObservableBase::
hasObservers() const
{
return (m_p->m_observers.size()!=0);
}


/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

Expand Down Expand Up @@ -218,79 +205,6 @@ add(EventObserverBase* obs)
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

namespace
{
class TestMemberCall
{
public:
void my_func(int a,int b)
{
std::cout << "THIS_IS_MY FUNC XA=" << a << " B=" << b << '\n';
}
void operator()(int a,int b)
{
std::cout << "THIS_IS OPERATOR() FUNC XA=" << a << " B=" << b << '\n';
}
};
}
extern "C++" ARCANE_UTILS_EXPORT void
_internalTestEvent()
{
using std::placeholders::_1;
using std::placeholders::_2;

int f = 3;
auto func = [&](int a,int b){
std::cout << "XA=" << a << " B=" << b << " f=" << f << '\n';
f = a+b;
};
auto func2 = [&](int a,int b){
std::cout << "FUNC2: XA=" << a << " B=" << b << " f=" << f << '\n';
};
TestMemberCall tmc;
EventObserver<int,int> x2(func);
{
EventObservable<int,int> xevent;
EventObserverPool pool;
{
EventObserver<int,int> xobserver;
// NOTE: le test suivnant ne marche pas avec MSVS2013
//std::function<void(TestMemberCall*,int,int)> kk1(&TestMemberCall::my_func);
std::function<void(int,int)> kk( std::bind( &TestMemberCall::my_func, tmc, _1, _2 ) );
//std::function<void(int,int)> kk2( std::bind( &TestMemberCall::my_func, tmc ) );
//auto kk( std::bind( &TestMemberCall::my_func, &tmc ) );
EventObserver<int,int> x4(kk);
EventObserver<int,int> x3(tmc);
xevent.attach(&x2);
xevent.attach(&x3);
xevent.attach(&x4);
xevent.attach(&xobserver);
xevent.notify(2,3);
xevent.detach(&x4);
}
xevent.attach(pool,func2);
}
std::cout << "(After) F=" << f << '\n';
if (f!=5)
ARCANE_FATAL("Bad value for f");
{
EventObserver<int,int>* eo1 = nullptr;
EventObservable<int,int> xevent;
{
eo1 = new EventObserver<int,int>( std::bind( &TestMemberCall::my_func, tmc, _1, _2 ) );
xevent.attach(eo1);
}
xevent.notify(2,4);
delete eo1;
}
}

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

} // End namespace Arcane

/*---------------------------------------------------------------------------*/
Expand Down
Loading
Loading