Remove code duplication in trajana/modules/trajectory
authorRoland Schulz <roland.schulz@intel.com>
Mon, 13 Aug 2018 01:27:37 +0000 (18:27 -0700)
committerRoland Schulz <roland.schulz@intel.com>
Mon, 13 Aug 2018 16:37:31 +0000 (18:37 +0200)
Change-Id: If5f48695930c61bbed69932b6cab267fc8ac110c

src/gromacs/trajectoryanalysis/modules/trajectory.cpp

index b83d1886d5316b4d769f69e5bf10ed2e5c97c7bd..0d6b02e00602203d52f92ef195134478c95af757 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2016, by the GROMACS development team, led by
+ * Copyright (c) 2016,2018, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -238,68 +238,43 @@ Trajectory::initAnalysis(const TrajectoryAnalysisSettings &settings,
 }
 
 
-void
-Trajectory::analyzeFrame(int frnr, const t_trxframe &fr, t_pbc * /* pbc */,
-                         TrajectoryAnalysisModuleData *pdata)
+//! Helper function for Trajectory::analyzeFrame
+template<typename T> void
+analyzeFrameImpl(int frnr, const t_trxframe &fr,
+                 AnalysisDataHandle* dh, const SelectionList &sel, T getField)
 {
-    const SelectionList &sel = pdata->parallelSelections(sel_);
-
-    // There is some duplication here, but cppcheck cannot handle function
-    // types that return rvec references, and MSVC also apparently segfaults
-    // when using std::function with a member function here...
+    if (dh->isValid())
     {
-        AnalysisDataHandle dh = pdata->dataHandle(xdata_);
-        if (dh.isValid())
+        dh->startFrame(frnr, fr.time);
+        for (size_t g = 0; g < sel.size(); ++g)
         {
-            dh.startFrame(frnr, fr.time);
-            for (size_t g = 0; g < sel.size(); ++g)
+            dh->selectDataSet(g);
+            for (int i = 0; i < sel[g].posCount(); ++i)
             {
-                dh.selectDataSet(g);
-                for (int i = 0; i < sel[g].posCount(); ++i)
-                {
-                    const SelectionPosition &pos = sel[g].position(i);
-                    dh.setPoints(i*3, 3, pos.x(), pos.selected());
-                }
+                const SelectionPosition &pos = sel[g].position(i);
+                dh->setPoints(i*3, 3, getField(pos), pos.selected());
             }
-            dh.finishFrame();
         }
+        dh->finishFrame();
     }
+}
+
+void
+Trajectory::analyzeFrame(int frnr, const t_trxframe &fr, t_pbc * /* pbc */,
+                         TrajectoryAnalysisModuleData *pdata)
+{
+    AnalysisDataHandle   dh  = pdata->dataHandle(xdata_);
+    const SelectionList &sel = pdata->parallelSelections(sel_);
+    analyzeFrameImpl(frnr, fr, &dh, sel, [](const SelectionPosition &pos) { return pos.x(); });
     if (fr.bV)
     {
-        AnalysisDataHandle dh = pdata->dataHandle(xdata_);
-        if (dh.isValid())
-        {
-            dh.startFrame(frnr, fr.time);
-            for (size_t g = 0; g < sel.size(); ++g)
-            {
-                dh.selectDataSet(g);
-                for (int i = 0; i < sel[g].posCount(); ++i)
-                {
-                    const SelectionPosition &pos = sel[g].position(i);
-                    dh.setPoints(i*3, 3, pos.v(), pos.selected());
-                }
-            }
-            dh.finishFrame();
-        }
+        analyzeFrameImpl(frnr, fr, &dh, sel, [](const SelectionPosition &pos) { return pos.v(); });
     }
     if (fr.bF)
     {
-        AnalysisDataHandle dh = pdata->dataHandle(xdata_);
-        if (dh.isValid())
-        {
-            dh.startFrame(frnr, fr.time);
-            for (size_t g = 0; g < sel.size(); ++g)
-            {
-                dh.selectDataSet(g);
-                for (int i = 0; i < sel[g].posCount(); ++i)
-                {
-                    const SelectionPosition &pos = sel[g].position(i);
-                    dh.setPoints(i*3, 3, pos.f(), pos.selected());
-                }
-            }
-            dh.finishFrame();
-        }
+        analyzeFrameImpl(frnr, fr, &dh, sel, [](const SelectionPosition &pos) { return pos.f(); });
     }
+
 }