Arcane  v3.16.0.0
Documentation développeur
Tout Classes Espaces de nommage Fichiers Fonctions Variables Définitions de type Énumérations Valeurs énumérées Amis Macros Groupes Pages Concepts
TestEvent.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7
8#include <gtest/gtest.h>
9
10#include "arcane/utils/Event.h"
11#include "arcane/utils/FatalErrorException.h"
12
13#include <iostream>
14
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18using namespace Arcane;
19
20
21namespace
22{
23class TestMemberCall
24{
25 public:
26 void my_func(int a,int b)
27 {
28 std::cout << "THIS_IS_MY FUNC XA=" << a << " B=" << b << '\n';
29 }
30 void operator()(int a,int b)
31 {
32 std::cout << "THIS_IS OPERATOR() FUNC XA=" << a << " B=" << b << '\n';
33 }
34};
35}
36TEST(TestEvent, Misc)
37{
38 using std::placeholders::_1;
39 using std::placeholders::_2;
40
41 int f = 3;
42 auto func = [&](int a, int b) {
43 std::cout << "XA=" << a << " B=" << b << " f=" << f << '\n';
44 f = a + b;
45 };
46 auto func2 = [&](int a, int b) {
47 std::cout << "FUNC2: XA=" << a << " B=" << b << " f=" << f << '\n';
48 };
49 TestMemberCall tmc;
51 {
54 {
56 // NOTE: le test suivnant ne marche pas avec MSVS2013
57 std::function<void(TestMemberCall*, int, int)> kk1(&TestMemberCall::my_func);
58 std::function<void(int, int)> kk(std::bind(&TestMemberCall::my_func, tmc, _1, _2));
59 //std::function<void(int,int)> kk2( std::bind( &TestMemberCall::my_func, tmc ) );
60 //auto kk( std::bind( &TestMemberCall::my_func, &tmc ) );
63 xevent.attach(&x2);
64 xevent.attach(&x3);
65 xevent.attach(&x4);
66 xevent.attach(&xobserver);
67 xevent.notify(2, 3);
68 xevent.detach(&x4);
69 }
70 xevent.attach(pool, func2);
71 }
72 std::cout << "(After) F=" << f << '\n';
73
74 ASSERT_EQ(f, 5);
75
76 {
77 EventObserver<int, int>* eo1 = nullptr;
79 {
80 eo1 = new EventObserver<int, int>(std::bind(&TestMemberCall::my_func, tmc, _1, _2));
81 xevent.attach(eo1);
82 }
83 xevent.notify(2, 4);
84 delete eo1;
85 }
86}
87
88/*---------------------------------------------------------------------------*/
89/*---------------------------------------------------------------------------*/
Fichier contenant les mécanismes de gestion des évènements.
Classe de base d'un handler d'évènement.
Definition Event.h:193
void detach(ObserverType *o)
Détache l'observateur o de cet observable.
Definition Event.h:215
void attach(ObserverType *o)
Attache l'observateur o à cet observable.
Definition Event.h:209
void notify(Args... args)
Appelle les observeurs associés à cet observable.
Definition Event.h:230
Conserve des références d'observateurs.
Definition Event.h:143
Observateur d'évènements.
Definition Event.h:112
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-