From 36dc528c6d1608e2aa3de0eff35c0453d8e12a44 Mon Sep 17 00:00:00 2001 From: Wenqing Wang <wenqing.wang@ufz.de> Date: Wed, 28 Jul 2021 12:40:49 +0200 Subject: [PATCH] [ProcessLib/Python] Moved a double defined class to a new file. --- .../Python/FlushStdoutGuard.h | 48 +++++++++++++++++++ .../Python/PythonBoundaryCondition.cpp | 38 +-------------- .../Python/PythonSourceTerm.cpp | 38 +-------------- 3 files changed, 50 insertions(+), 74 deletions(-) create mode 100644 ProcessLib/BoundaryConditionAndSourceTerm/Python/FlushStdoutGuard.h diff --git a/ProcessLib/BoundaryConditionAndSourceTerm/Python/FlushStdoutGuard.h b/ProcessLib/BoundaryConditionAndSourceTerm/Python/FlushStdoutGuard.h new file mode 100644 index 00000000000..89dba0f13f5 --- /dev/null +++ b/ProcessLib/BoundaryConditionAndSourceTerm/Python/FlushStdoutGuard.h @@ -0,0 +1,48 @@ +/** + * \file + * \copyright + * Copyright (c) 2012-2021, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + * + */ + +#pragma once + +namespace +{ +//! Optionally flushes the standard output upon creation and destruction. +//! Can be used to improve the debug output readability when printing debug +//! messages both from OGS and from Python. +class FlushStdoutGuard final +{ +public: + //! Optionally flushes C++ stdout before running Python code. + explicit FlushStdoutGuard(bool const flush) : _flush(flush) + { + if (!flush) + { + return; + } + + std::cout << std::flush; + } + + //! Optionally flushes Python's stdout after running Python code. + ~FlushStdoutGuard() + { + if (!_flush) + { + return; + } + + using namespace pybind11::literals; + pybind11::print("end"_a = "", "flush"_a = true); + } + +private: + //! To flush or not to flush. + const bool _flush; +}; +} // anonymous namespace diff --git a/ProcessLib/BoundaryConditionAndSourceTerm/Python/PythonBoundaryCondition.cpp b/ProcessLib/BoundaryConditionAndSourceTerm/Python/PythonBoundaryCondition.cpp index 15d6f05f0df..eb451916878 100644 --- a/ProcessLib/BoundaryConditionAndSourceTerm/Python/PythonBoundaryCondition.cpp +++ b/ProcessLib/BoundaryConditionAndSourceTerm/Python/PythonBoundaryCondition.cpp @@ -15,47 +15,11 @@ #include <iostream> #include "BaseLib/ConfigTree.h" +#include "FlushStdoutGuard.h" #include "MeshLib/MeshSearch/NodeSearch.h" #include "ProcessLib/BoundaryConditionAndSourceTerm/Utils/CreateLocalAssemblers.h" #include "PythonBoundaryConditionLocalAssembler.h" -namespace -{ -//! Optionally flushes the standard output upon creation and destruction. -//! Can be used to improve the debug output readability when printing debug -//! messages both from OGS and from Python. -class FlushStdoutGuard -{ -public: - //! Optionally flushes C++ stdout before running Python code. - explicit FlushStdoutGuard(bool const flush) : _flush(flush) - { - if (!flush) - { - return; - } - - std::cout << std::flush; - } - - //! Optionally flushes Python's stdout after running Python code. - ~FlushStdoutGuard() - { - if (!_flush) - { - return; - } - - using namespace pybind11::literals; - pybind11::print("end"_a = "", "flush"_a = true); - } - -private: - //! To flush or not to flush. - const bool _flush; -}; -} // anonymous namespace - namespace ProcessLib { PythonBoundaryCondition::PythonBoundaryCondition( diff --git a/ProcessLib/BoundaryConditionAndSourceTerm/Python/PythonSourceTerm.cpp b/ProcessLib/BoundaryConditionAndSourceTerm/Python/PythonSourceTerm.cpp index 91c145ebafb..0df3287aa57 100644 --- a/ProcessLib/BoundaryConditionAndSourceTerm/Python/PythonSourceTerm.cpp +++ b/ProcessLib/BoundaryConditionAndSourceTerm/Python/PythonSourceTerm.cpp @@ -14,48 +14,12 @@ #include <iostream> +#include "FlushStdoutGuard.h" #include "MeshLib/MeshSearch/NodeSearch.h" #include "NumLib/DOF/LocalToGlobalIndexMap.h" #include "ProcessLib/BoundaryConditionAndSourceTerm/Utils/CreateLocalAssemblers.h" #include "PythonSourceTermLocalAssembler.h" -namespace -{ -//! Optionally flushes the standard output upon creation and destruction. -//! Can be used to improve the debug output readability when printing debug -//! messages both from OGS and from Python. -class FlushStdoutGuard final -{ -public: - //! Optionally flushes C++ stdout before running Python code. - explicit FlushStdoutGuard(bool const flush) : _flush(flush) - { - if (!flush) - { - return; - } - - std::cout << std::flush; - } - - //! Optionally flushes Python's stdout after running Python code. - ~FlushStdoutGuard() - { - if (!_flush) - { - return; - } - - using namespace pybind11::literals; - pybind11::print("end"_a = "", "flush"_a = true); - } - -private: - //! To flush or not to flush. - const bool _flush; -}; -} // anonymous namespace - namespace ProcessLib { namespace SourceTerms -- GitLab