1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
/*
Author: Shane Neph & Scott Kuehn
Date: Dec. 7, 2009
*/
//
// BEDOPS
// Copyright (C) 2011-2022 Shane Neph, Scott Kuehn and Alex Reynolds
//
// This program 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 2 of the License, or
// (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#ifndef _VISITOR_POST_PROCESSING_
#define _VISITOR_POST_PROCESSING_
#include <iostream>
#include <ostream>
#include <string>
#include "data/measurement/NaN.hpp"
#include "utility/PrintTypes.hpp"
namespace Visitors {
namespace Helpers {
//=============
// DoNothing()
//=============
struct DoNothing {
void operator()(...) const { /* */ }
};
//======
// Keep
//======
template <typename ValueType>
struct Keep {
Keep() : isNan_(false), value_(0)
{ }
template <typename T>
void operator()(T* t) {
value_ = static_cast<ValueType>(*t);
isNan_ = false;
}
template <typename T>
void operator()(const T& t) {
value_ = static_cast<ValueType>(t);
isNan_ = false;
}
void operator()(const Signal::NaN&) {
isNan_ = true;
}
bool isNan_;
ValueType value_;
};
//=========
// Print()
//=========
struct Print {
template <typename T>
void operator()(T* t) const {
PrintTypes::Print(*t);
}
template <typename T>
void operator()(const T& t) const {
PrintTypes::Print(t);
}
};
//===========
// Println()
//===========
struct Println {
template <typename T>
void operator()(T* t) const {
PrintTypes::Println(*t);
}
template <typename T>
void operator()(const T& t) const {
PrintTypes::Println(t);
}
};
//============
// PrintDelim
//============
struct PrintDelim {
explicit PrintDelim(const std::string& delim = "|")
: delim_(delim), isChar_(false) {
bool isaTab = (delim == "\t" || delim == "\\t" || delim == "'\t'");
bool isaNewline = (delim == "\n" || delim == "\\n" || delim == "'\n'");
if ( isaTab ) {
delim_ = "";
delim_ += '\t';
isChar_ = true;
}
else if ( isaNewline ) {
delim_ = "";
delim_ += '\n';
isChar_ = true;
} else if ( 1 == delim.size() ) {
delim_ = "";
delim_ += delim[0];
isChar_ = true;
}
}
explicit PrintDelim(char t) : delim_(""), isChar_(true) {
delim_ += t;
}
void operator()() const {
if ( isChar_ )
PrintTypes::Print(delim_[0]);
else
PrintTypes::Print(delim_.c_str());
}
private:
std::string delim_;
bool isChar_;
};
//==================
// PrintRangeDelim()
//===================
template <typename PrintType>
struct PrintRangeDelim : private PrintDelim {
typedef PrintDelim Base;
typedef PrintType PType;
explicit PrintRangeDelim(const PrintType& p = PrintType(),
const std::string& delim = ";",
const std::string& onEmpty = "")
: Base(delim), pt_(p), oe_(onEmpty)
{ /* */ }
template <typename Iter>
void operator()(Iter beg, Iter end) const {
if ( beg == end ) {
if ( !oe_.empty() )
PrintTypes::Print(oe_.c_str());
return;
}
pt_.operator()(*beg);
while ( ++beg != end ) {
Base::operator()();
pt_.operator()(*beg);
} // while
}
private:
PrintType pt_;
std::string oe_;
};
} // namespace Helpers
} // namespace Visitors
#endif // _VISITOR_POST_PROCESSING_
|