[yoda-svn] yoda: arbitary container reading using SFINAE

YODA Mercurial yoda at projects.hepforge.org
Tue Aug 4 18:00:01 BST 2015


details:   https://yoda.hepforge.org/hg/yoda/rev/c7ef9814dac4
branches:  
changeset: 1087:c7ef9814dac4
user:      Lukas Heinrich <lukas.heinrich at gmail.com>
date:      Tue Aug 04 17:45:39 2015 +0100
description:
arbitary container reading using SFINAE

diffs (truncated from 78 to 50 lines):

--- a/include/YODA/Reader.h	Mon Aug 03 13:50:39 2015 +0100
+++ b/include/YODA/Reader.h	Tue Aug 04 17:45:39 2015 +0100
@@ -10,6 +10,9 @@
 #include <string>
 #include <fstream>
 #include <vector>
+#include "boost/utility/enable_if.hpp"
+#include "YODA/Utils/Traits.h"
+#include <iostream>
 
 namespace YODA {
 
@@ -27,10 +30,28 @@
 
     /// @brief Read in a collection of objects @a objs from output stream @a stream.
     ///
+    /// This version fills (actually, appends to) a variable supplied container
+    /// Note: SFINAE is used to check for a void push_back(const AnalysisObject*) method
+    template<typename CONT>
+    typename boost::enable_if_c<YODA::Pushable<CONT,AnalysisObject*>::value>::type
+    read(std::istream& stream, CONT& aos){
+      //if CONT==std::vector<AnalysisObject*>, the compiler should select
+      //the virtual method below, since it prefers non-templated methods in the lookup
+      //otherwise we would enter a endless recursion. Check in case of problems.
+      std::vector<AnalysisObject*> v_aos;
+      read(stream,v_aos);
+      std::vector<AnalysisObject*>::const_iterator it = v_aos.begin();
+      for(it = v_aos.begin(); it!=v_aos.end(); ++it){
+        aos.push_back(*it);
+      }
+    }
+
+    /// @brief Read in a collection of objects @a objs from output stream @a stream.
+    ///
     /// This version fills (actually, appends to) a supplied vector, avoiding copying,
     /// and is hence CPU efficient.
     ///
-    /// @todo Use SFINAE magic to allow ~arbitrary collection<AnalysisObject*> (with push_back()?) to be passed
+    
     virtual void read(std::istream& stream, std::vector<AnalysisObject*>& aos) = 0;
 
     /// @brief Read in a collection of objects from output stream @a stream.
--- a/include/YODA/Utils/Traits.h	Mon Aug 03 13:50:39 2015 +0100
+++ b/include/YODA/Utils/Traits.h	Tue Aug 04 17:45:39 2015 +0100
@@ -9,18 +9,29 @@
 #include <boost/type_traits/has_dereference.hpp>
 
 namespace YODA{
+  namespace SFINAE{
+    typedef char yes[1]; typedef char no[2];


More information about the yoda-svn mailing list