Skip to content

Commit f46a4e5

Browse files
authored
Initial and final function stacks: Use std::function (#4218)
Use std::function instead of function pointer for the initial and final function stacks. This is more flexible because we can now use lambda functions with captures (which cannot be converted to function pointer, but can be converted std::function).
1 parent 4536558 commit f46a4e5

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

Src/Base/AMReX.H

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ namespace amrex
102102
* classes to clean up any "global" state that they maintain when we're
103103
* exiting from AMReX.
104104
*/
105-
void ExecOnFinalize (PTR_TO_VOID_FUNC);
106-
void ExecOnInitialize (PTR_TO_VOID_FUNC);
105+
void ExecOnFinalize (std::function<void()>);
106+
void ExecOnInitialize (std::function<void()>);
107107

108108
//! This shuts up the compiler about unused variables
109109
template <class... Ts>

Src/Base/AMReX.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,20 +302,20 @@ amrex::Assert_host (const char* EX, const char* file, int line, const char* msg)
302302

303303
namespace
304304
{
305-
std::stack<amrex::PTR_TO_VOID_FUNC> The_Finalize_Function_Stack;
306-
std::stack<amrex::PTR_TO_VOID_FUNC> The_Initialize_Function_Stack;
305+
std::stack<std::function<void()>> The_Finalize_Function_Stack;
306+
std::stack<std::function<void()>> The_Initialize_Function_Stack;
307307
}
308308

309309
void
310-
amrex::ExecOnFinalize (PTR_TO_VOID_FUNC fp)
310+
amrex::ExecOnFinalize (std::function<void()> f)
311311
{
312-
The_Finalize_Function_Stack.push(fp);
312+
The_Finalize_Function_Stack.push(std::move(f));
313313
}
314314

315315
void
316-
amrex::ExecOnInitialize (PTR_TO_VOID_FUNC fp)
316+
amrex::ExecOnInitialize (std::function<void()> f)
317317
{
318-
The_Initialize_Function_Stack.push(fp);
318+
The_Initialize_Function_Stack.push(std::move(f));
319319
}
320320

321321
amrex::AMReX*
@@ -391,7 +391,7 @@ amrex::Initialize (int& argc, char**& argv, bool build_parm_parse,
391391
//
392392
// Call the registered function.
393393
//
394-
(*The_Initialize_Function_Stack.top())();
394+
The_Initialize_Function_Stack.top()();
395395
//
396396
// And then remove it from the stack.
397397
//
@@ -756,7 +756,7 @@ amrex::Finalize (amrex::AMReX* pamrex)
756756
//
757757
// Call the registered function.
758758
//
759-
(*The_Finalize_Function_Stack.top())();
759+
The_Finalize_Function_Stack.top()();
760760
//
761761
// And then remove it from the stack.
762762
//

0 commit comments

Comments
 (0)