Page Menu
Home
HEPForge
Search
Configure Global Search
Log In
Files
F7877369
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
10 KB
Subscribers
None
View Options
diff --git a/include/YODA/Reader.h b/include/YODA/Reader.h
--- a/include/YODA/Reader.h
+++ b/include/YODA/Reader.h
@@ -1,81 +1,102 @@
// -*- C++ -*-
//
// This file is part of YODA -- Yet more Objects for Data Analysis
// Copyright (C) 2008-2015 The YODA collaboration (see AUTHORS for details)
//
#ifndef YODA_Reader_h
#define YODA_Reader_h
#include "YODA/AnalysisObject.h"
#include <string>
#include <fstream>
#include <vector>
+#include "boost/utility/enable_if.hpp"
+#include "YODA/Utils/Traits.h"
+#include <iostream>
namespace YODA {
/// Pure virtual base class for various output writers.
class Reader {
public:
/// Virtual destructor
virtual ~Reader() {}
/// @name Reading multiple analysis objects,
//@{
/// @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.
///
/// This version returns a vector by value, involving copying, and is hence less
/// CPU efficient than the alternative version where a vector is filled by reference.
std::vector<AnalysisObject*> read(std::istream& stream) {
std::vector<AnalysisObject*> rtn;
read(stream, rtn);
return rtn;
}
/// @brief Read in a collection of objects @a objs from file @a filename.
///
/// 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
void read(const std::string& filename, std::vector<AnalysisObject*>& aos) {
std::ifstream instream;
instream.open(filename.c_str());
read(instream, aos);
instream.close();
}
/// @brief Read in a collection of objects from output stream @a stream.
///
/// This version returns a vector by value, involving copying, and is hence less
/// CPU efficient than the alternative version where a vector is filled by reference.
std::vector<AnalysisObject*> read(const std::string& filename) {
std::vector<AnalysisObject*> rtn;
read(filename, rtn);
return rtn;
}
//@}
};
/// Factory function to make a writer object by format name or a filename
Reader& mkReader(const std::string& format_name);
}
#endif
diff --git a/include/YODA/Utils/Traits.h b/include/YODA/Utils/Traits.h
--- a/include/YODA/Utils/Traits.h
+++ b/include/YODA/Utils/Traits.h
@@ -1,19 +1,37 @@
// -*- C++ -*-
//
// This file is part of YODA -- Yet more Objects for Data Analysis
// Copyright (C) 2008-2015 The YODA collaboration (see AUTHORS for details)
//
#ifndef YODA_TRAITS_H
#define YODA_TRAITS_H
+#include <boost/type_traits/has_dereference.hpp>
+
namespace YODA{
+ namespace SFINAE{
+ typedef char yes[1]; typedef char no[2];
+ }
+
//SFINAE struct to check for iterator concept at compile time
template<typename T>
struct Iterable{
- typedef char yes[1]; typedef char no[2];
- template <typename C> static yes& test(typename C::const_iterator* c);
- template <typename> static no& test(...);
- static const bool value = sizeof(test<T>(0)) == sizeof(yes);
+ template <typename C> static SFINAE::yes& test(typename C::const_iterator* c);
+ template <typename> static SFINAE::no& test(...);
+ static const bool value = sizeof(test<T>(0)) == sizeof(SFINAE::yes);
+ };
+
+ template<typename T>
+ struct DerefAO{
+ static const bool value = boost::has_dereference<T,const YODA::AnalysisObject&>::value;
+ };
+
+ template<typename T,typename VAL>
+ struct Pushable{
+ template <typename SIG,SIG> struct has_sig;
+ template<typename C> static SFINAE::yes& test(has_sig<void (C::*) (const VAL&),&C::push_back>*);
+ template<typename C> static SFINAE::no& test(...);
+ static const bool value = sizeof(test<T>(0)) == sizeof(SFINAE::yes);
};
}
#endif
diff --git a/include/YODA/Writer.h b/include/YODA/Writer.h
--- a/include/YODA/Writer.h
+++ b/include/YODA/Writer.h
@@ -1,143 +1,153 @@
// -*- C++ -*-
//
// This file is part of YODA -- Yet more Objects for Data Analysis
// Copyright (C) 2008-2015 The YODA collaboration (see AUTHORS for details)
//
#ifndef YODA_Writer_h
#define YODA_Writer_h
#include "YODA/AnalysisObject.h"
#include "YODA/Counter.h"
#include "YODA/Histo1D.h"
#include "YODA/Histo2D.h"
#include "YODA/Profile1D.h"
#include "YODA/Profile2D.h"
#include "YODA/Scatter1D.h"
#include "YODA/Scatter2D.h"
#include "YODA/Scatter3D.h"
#include "YODA/Utils/Traits.h"
#include "boost/range.hpp"
#include "boost/utility/enable_if.hpp"
#include <string>
#include <fstream>
namespace YODA {
/// Pure virtual base class for various output writers.
class Writer {
public:
/// Virtual destructor
virtual ~Writer() {}
/// @name Writing a single analysis object.
//@{
/// Write out object @a ao to output stream @a stream.
void write(std::ostream& stream, const AnalysisObject& ao);
/// Write out object @a ao to file @a filename.
void write(const std::string& filename, const AnalysisObject& ao);
//@}
/// @name Writing multiple analysis objects by collection.
//@{
/// Write out a collection of objects @a objs to output stream @a stream.
/// Note: the enable_if call checks whether RANGE is const_iterable, if yes the return
/// type is void. If not, this template will not be a candidate in the lookup
template <typename RANGE>
typename boost::enable_if_c<Iterable<RANGE>::value>::type
write(std::ostream& stream, const RANGE& aos) {
//typedef typename boost::range_iterator<const RANGE>::type const_iterator;
write(stream, boost::begin(aos), boost::end(aos));
}
/// Write out a collection of objects @a objs to file @a filename.
template <typename RANGE>
typename boost::enable_if_c<Iterable<RANGE>::value>::type
write(const std::string& filename, const RANGE& aos) {
//typedef typename boost::range_iterator<const RANGE>::type const_iterator;
write(filename, boost::begin(aos), boost::end(aos));
}
//@}
/// @name Writing multiple analysis objects by iterator range.
//@{
/// Write out the objects specified by start iterator @a begin and end
/// iterator @a end to output stream @a stream.
template <typename AOITER>
void write(std::ostream& stream, const AOITER& begin, const AOITER& end) {
writeHeader(stream);
for (AOITER ipao = begin; ipao != end; ++ipao) {
writeBody(stream, *ipao);
}
writeFooter(stream);
}
/// Write out the objects specified by start iterator @a begin and end
/// iterator @a end to file @a filename.
template <typename AOITER>
void write(const std::string& filename,
const AOITER& begin,
const AOITER& end) {
std::ofstream outstream;
outstream.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
try{
outstream.open(filename.c_str());
write(outstream, begin, end);
outstream.close();
} catch(std::ifstream::failure e) {
throw WriteError("writing to filename " + filename + " failed: " + e.what());
}
}
+
//@}
/// Set precision of numerical quantities in this writer's output.
void setPrecision(int precision) {
_precision = precision;
}
protected:
/// Main writer elements
virtual void writeHeader(std::ostream& stream) = 0;
+
+
+ //note: DerefAO<T> is a trait that's true if T has a derefence operator
+ // that is convertible to an AnalysisObject
+ template<typename T>
+ typename boost::enable_if_c<DerefAO<T>::value>::type writeBody(std::ostream& stream, const T& ao){
+ writeBody(stream,*ao);
+ }
+
virtual void writeBody(std::ostream& stream, const AnalysisObject* ao);
virtual void writeBody(std::ostream& stream, const AnalysisObject& ao);
virtual void writeFooter(std::ostream& stream) = 0;
/// Specific AO type writer implementations
virtual void writeCounter(std::ostream& stream, const Counter& c) = 0;
virtual void writeHisto1D(std::ostream& os, const Histo1D& h) = 0;
virtual void writeHisto2D(std::ostream& os, const Histo2D& h) = 0;
virtual void writeProfile1D(std::ostream& os, const Profile1D& p) = 0;
virtual void writeProfile2D(std::ostream& os, const Profile2D& p) = 0;
virtual void writeScatter1D(std::ostream& os, const Scatter1D& s) = 0;
virtual void writeScatter2D(std::ostream& os, const Scatter2D& s) = 0;
virtual void writeScatter3D(std::ostream& os, const Scatter3D& s) = 0;
/// Output precision
int _precision;
};
/// Factory function to make a writer object by format name or a filename
Writer& mkWriter(const std::string& format_name);
}
#endif
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Nov 19, 3:29 PM (1 d, 17 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3804951
Default Alt Text
(10 KB)
Attached To
rYODAHG yodahg
Event Timeline
Log In to Comment