From 0b7c2ee470d1b74a081734125b2c4418a8bc28a4 Mon Sep 17 00:00:00 2001
From: Henry <Henry>
Date: Thu, 5 Feb 2015 18:10:42 +0000
Subject: [PATCH] registerSwitch: rationalization of info and optimization
 switch registration plus support for other than integer types

---
 src/OpenFOAM/db/IOstreams/Pstreams/UPstream.C | 35 ++++----
 src/OpenFOAM/db/regIOobject/regIOobject.C     | 25 +++---
 .../global/debug/registerInfoSwitch.H         | 65 --------------
 src/OpenFOAM/global/debug/registerOptSwitch.H | 65 --------------
 src/OpenFOAM/global/debug/registerSwitch.H    | 90 +++++++++++++++++++
 5 files changed, 120 insertions(+), 160 deletions(-)
 delete mode 100644 src/OpenFOAM/global/debug/registerInfoSwitch.H
 delete mode 100644 src/OpenFOAM/global/debug/registerOptSwitch.H
 create mode 100644 src/OpenFOAM/global/debug/registerSwitch.H

diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.C b/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.C
index 9e3f0cb766..821c1dd796 100644
--- a/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.C
+++ b/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.C
@@ -25,7 +25,7 @@ License
 
 #include "UPstream.H"
 #include "debug.H"
-#include "registerOptSwitch.H"
+#include "registerSwitch.H"
 #include "dictionary.H"
 #include "IOstreams.H"
 
@@ -424,32 +424,32 @@ Foam::UPstream::communicator serialComm
 // in accuracy
 bool Foam::UPstream::floatTransfer
 (
-    debug::optimisationSwitch("floatTransfer", 0)
+    Foam::debug::optimisationSwitch("floatTransfer", 0)
 );
-registerOptSwitchWithName
+registerOptSwitch
 (
-    Foam::UPstream::floatTransfer,
-    floatTransfer,
-    "floatTransfer"
+    "floatTransfer",
+    bool,
+    Foam::UPstream::floatTransfer
 );
 
 // Number of processors at which the reduce algorithm changes from linear to
 // tree
 int Foam::UPstream::nProcsSimpleSum
 (
-    debug::optimisationSwitch("nProcsSimpleSum", 16)
+    Foam::debug::optimisationSwitch("nProcsSimpleSum", 16)
 );
-registerOptSwitchWithName
+registerOptSwitch
 (
-    Foam::UPstream::nProcsSimpleSum,
-    nProcsSimpleSum,
-    "nProcsSimpleSum"
+    "nProcsSimpleSum",
+    int,
+    Foam::UPstream::nProcsSimpleSum
 );
 
 // Default commsType
 Foam::UPstream::commsTypes Foam::UPstream::defaultCommsType
 (
-    commsTypeNames.read(debug::optimisationSwitches().lookup("commsType"))
+    commsTypeNames.read(Foam::debug::optimisationSwitches().lookup("commsType"))
 );
 // Register re-reader
 class addcommsTypeToOpt
@@ -489,13 +489,14 @@ Foam::label Foam::UPstream::warnComm(-1);
 // Number of polling cycles in processor updates
 int Foam::UPstream::nPollProcInterfaces
 (
-    debug::optimisationSwitch("nPollProcInterfaces", 0)
+    Foam::debug::optimisationSwitch("nPollProcInterfaces", 0)
 );
-registerOptSwitchWithName
+registerOptSwitch
 (
-    Foam::UPstream::nPollProcInterfaces,
-    nPollProcInterfaces,
-    "nPollProcInterfaces"
+    "nPollProcInterfaces",
+    int,
+    Foam::UPstream::nPollProcInterfaces
 );
 
+
 // ************************************************************************* //
diff --git a/src/OpenFOAM/db/regIOobject/regIOobject.C b/src/OpenFOAM/db/regIOobject/regIOobject.C
index fe52fe787b..847d7cdb32 100644
--- a/src/OpenFOAM/db/regIOobject/regIOobject.C
+++ b/src/OpenFOAM/db/regIOobject/regIOobject.C
@@ -26,7 +26,7 @@ License
 #include "regIOobject.H"
 #include "Time.H"
 #include "polyMesh.H"
-#include "registerOptSwitch.H"
+#include "registerSwitch.H"
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
@@ -34,18 +34,6 @@ namespace Foam
 {
     defineTypeNameAndDebug(regIOobject, 0);
 
-    int regIOobject::fileModificationSkew
-    (
-        debug::optimisationSwitch("fileModificationSkew", 30)
-    );
-    registerOptSwitchWithName
-    (
-        Foam::regIOobject::fileModificationSkew,
-        fileModificationSkew,
-        "fileModificationSkew"
-    );
-
-
     template<>
     const char* NamedEnum
     <
@@ -60,6 +48,17 @@ namespace Foam
     };
 }
 
+int Foam::regIOobject::fileModificationSkew
+(
+    Foam::debug::optimisationSwitch("fileModificationSkew", 30)
+);
+registerOptSwitch
+(
+    "fileModificationSkew",
+    int,
+    Foam::regIOobject::fileModificationSkew
+);
+
 
 const Foam::NamedEnum<Foam::regIOobject::fileCheckTypes, 4>
     Foam::regIOobject::fileCheckTypesNames;
diff --git a/src/OpenFOAM/global/debug/registerInfoSwitch.H b/src/OpenFOAM/global/debug/registerInfoSwitch.H
deleted file mode 100644
index 54598d2c33..0000000000
--- a/src/OpenFOAM/global/debug/registerInfoSwitch.H
+++ /dev/null
@@ -1,65 +0,0 @@
-/*---------------------------------------------------------------------------*\
-  =========                 |
-  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
-   \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2012-2015 OpenFOAM Foundation
-     \\/     M anipulation  |
--------------------------------------------------------------------------------
-License
-    This file is part of OpenFOAM.
-
-    OpenFOAM is free software: you can redistribute it and/or modify it
-    under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
-    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-    for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
-
-Description
-    Macro definitions for info switches.
-
-\*---------------------------------------------------------------------------*/
-
-#ifndef registerInfoSwitch_H
-#define registerInfoSwitch_H
-
-#include "simpleRegIOobject.H"
-
-// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
-
-//- Register info switch (if int), lookup as \a Name
-#define registerInfoSwitchWithName(Switch,Tag,Name)                           \
-    class add##Tag##ToInfo                                                    \
-    :                                                                         \
-        public ::Foam::simpleRegIOobject                                      \
-    {                                                                         \
-    public:                                                                   \
-        add##Tag##ToInfo(const char* name)                                    \
-        :                                                                     \
-            ::Foam::simpleRegIOobject(Foam::debug::addInfoObject, name)       \
-        {}                                                                    \
-        virtual ~add##Tag##ToInfo()                                           \
-        {}                                                                    \
-        virtual void readData(Foam::Istream& is)                              \
-        {                                                                     \
-            Switch = readLabel(is);                                           \
-        }                                                                     \
-        virtual void writeData(Foam::Ostream& os) const                       \
-        {                                                                     \
-            os << Switch;                                                     \
-        }                                                                     \
-    };                                                                        \
-    add##Tag##ToInfo add##Tag##ToInfo_(Name)
-
-
-// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
-
-#endif
-
-// ************************************************************************* //
diff --git a/src/OpenFOAM/global/debug/registerOptSwitch.H b/src/OpenFOAM/global/debug/registerOptSwitch.H
deleted file mode 100644
index ccd282d966..0000000000
--- a/src/OpenFOAM/global/debug/registerOptSwitch.H
+++ /dev/null
@@ -1,65 +0,0 @@
-/*---------------------------------------------------------------------------*\
-  =========                 |
-  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
-   \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2012-2015 OpenFOAM Foundation
-     \\/     M anipulation  |
--------------------------------------------------------------------------------
-License
-    This file is part of OpenFOAM.
-
-    OpenFOAM is free software: you can redistribute it and/or modify it
-    under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
-    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-    for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
-
-Description
-    Macro definitions for optimization switches.
-
-\*---------------------------------------------------------------------------*/
-
-#ifndef registerOptSwitch_H
-#define registerOptSwitch_H
-
-#include "simpleRegIOobject.H"
-
-// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
-
-//- Register optimisation switch (if int), lookup as \a Name
-#define registerOptSwitchWithName(Switch,Tag,Name)                            \
-    class add##Tag##ToOpt                                                     \
-    :                                                                         \
-        public ::Foam::simpleRegIOobject                                      \
-    {                                                                         \
-    public:                                                                   \
-        add##Tag##ToOpt(const char* name)                                     \
-        :                                                                     \
-            ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject,name)\
-        {}                                                                    \
-        virtual ~add##Tag##ToOpt()                                            \
-        {}                                                                    \
-        virtual void readData(Foam::Istream& is)                              \
-        {                                                                     \
-            Switch = readLabel(is);                                           \
-        }                                                                     \
-        virtual void writeData(Foam::Ostream& os) const                       \
-        {                                                                     \
-            os << Switch;                                                     \
-        }                                                                     \
-    };                                                                        \
-    add##Tag##ToOpt add##Tag##ToOpt_(Name)
-
-
-// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
-
-#endif
-
-// ************************************************************************* //
diff --git a/src/OpenFOAM/global/debug/registerSwitch.H b/src/OpenFOAM/global/debug/registerSwitch.H
new file mode 100644
index 0000000000..9c5cd26275
--- /dev/null
+++ b/src/OpenFOAM/global/debug/registerSwitch.H
@@ -0,0 +1,90 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2015 OpenFOAM Foundation
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+License
+    This file is part of OpenFOAM.
+
+    OpenFOAM is free software: you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+    for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
+
+Description
+    Class and registration macros for InfoSwitches and OptimisationSwitches
+    to support reading from system/controlDict and dynamic update.
+
+\*---------------------------------------------------------------------------*/
+
+#ifndef registerSwitch_H
+#define registerSwitch_H
+
+#include "simpleRegIOobject.H"
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+template<class Type>
+class RegisterSwitch
+:
+    public ::Foam::simpleRegIOobject
+{
+    Type& optSwitch_;
+
+public:
+
+    RegisterSwitch
+    (
+        void (*registryFn)(const char* name, simpleRegIOobject*),
+        const char* name,
+        Type& optSwitch
+    )
+    :
+        ::Foam::simpleRegIOobject(registryFn, name),
+        optSwitch_(optSwitch)
+    {}
+
+    virtual ~RegisterSwitch()
+    {}
+
+    virtual void readData(Foam::Istream& is)
+    {
+        is >> optSwitch_;
+    }
+
+    virtual void writeData(Foam::Ostream& os) const
+    {
+        os << optSwitch_;
+    }
+};
+
+
+#define CONCAT(x, y) x ## y
+#define CONCAT2(x, y) CONCAT(x, y)
+#define FILE_UNIQUE(x) CONCAT2(x, __LINE__)
+
+#define registerOptSwitch(Name, Type, Switch)                                  \
+    static RegisterSwitch<Type> FILE_UNIQUE(_addToOpt_)                        \
+        (Foam::debug::addOptimisationObject, Name, Switch)
+
+
+#define registerInfoSwitch(Name, Type, Switch)                                 \
+    static RegisterSwitch<Type> FILE_UNIQUE(_addToOpt_)                        \
+        (Foam::debug::addInfoObject, Name, Switch)
+
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+#endif
+
+// ************************************************************************* //
-- 
GitLab