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
|
/*
FILE: MultiVisitor.hpp
AUTHOR: Scott Kuehn, Shane Neph
CREATE DATE: Thu Sep 20 15:00:33 PDT 2007
PROJECT: utility
ID: $Id$
*/
//
// 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 MULTIVISITOR_HPP
#define MULTIVISITOR_HPP
#include <algorithm>
#include <functional>
#include <vector>
namespace Visitors {
template
<
typename ProcessFields, // belongs to MultiVisitor only; not contained visitors
typename ProcessRows, // belongs to MultiVisitor only; not contained visitors
typename BaseVisitor
>
struct MultiVisitor : BaseVisitor {
typedef BaseVisitor BaseClass;
typedef ProcessFields ProcessFieldType;
typedef ProcessRows ProcessRowType;
typedef typename BaseClass::RefType RefType;
typedef typename BaseClass::MapType MapType;
typedef std::vector<BaseClass*> GroupType;
explicit MultiVisitor(GroupType& visitors,
const ProcessFieldType& pFields = ProcessFieldType(),
const ProcessRowType& pRows = ProcessRowType(),
bool processAll = true)
: BaseVisitor(), t_(visitors), pFields_(pFields), pRows_(pRows),
pAll_(processAll), cnt_(0)
{ /* */ }
template <typename BaseDistType>
explicit MultiVisitor(GroupType& visitors,
const BaseDistType& dist,
const ProcessFieldType& pFields = ProcessFieldType(),
const ProcessRowType& pRows = ProcessRowType(),
bool processAll = true)
: BaseVisitor(dist), t_(visitors), pFields_(pFields), pRows_(pRows),
pAll_(processAll), cnt_(0)
{ /* */ }
void Add(MapType* u) {
void (BaseClass::*memberFuncPtr)(MapType*) = &BaseClass::Add;
// std::for_each(t_.begin(), t_.end(), std::bind2nd(std::mem_fun(memberFuncPtr), u)); # deprecated in C++11
std::for_each(t_.begin(), t_.end(), std::bind(std::mem_fn(memberFuncPtr), std::placeholders::_1, u));
++cnt_;
}
void Delete(MapType* u) {
void (BaseClass::*memberFuncPtr)(MapType*) = &BaseClass::Delete;
// std::for_each(t_.begin(), t_.end(), std::bind2nd(std::mem_fun(memberFuncPtr), u)); # deprecated in C++11
std::for_each(t_.begin(), t_.end(), std::bind(std::mem_fn(memberFuncPtr), std::placeholders::_1, u));
--cnt_;
}
void DoneReference() {
if ( !pAll_ && cnt_ == 0 )
return;
typename GroupType::const_iterator i = t_.begin();
if ( i != t_.end() )
(*i)->DoneReference();
else
return;
for ( ++i; i != t_.end(); ++i ) {
pFields_.operator()();
(*i)->DoneReference();
} // for
pRows_.operator()();
}
void SetReference(RefType* t) {
void (BaseClass::*memberFuncPtr)(RefType*) = &BaseClass::SetReference;
// std::for_each(t_.begin(), t_.end(), std::bind2nd(std::mem_fun(memberFuncPtr), t)); # deprecated in C++11
std::for_each(t_.begin(), t_.end(), std::bind(std::mem_fn(memberFuncPtr), std::placeholders::_1, t));
}
/* not used by BED visitors to date. Pain if it is with nested elements. */
/*
void Purge() {
std::for_each(t_.begin(), t_.end(), std::mem_fun(&BaseClass::Purge));
}
*/
void End() {
// std::for_each(t_.begin(), t_.end(), std::mem_fun(&BaseClass::End));
std::for_each(t_.begin(), t_.end(), std::mem_fn(&BaseClass::End));
}
virtual ~MultiVisitor() {
for ( cGtI gi = t_.begin(); gi != t_.end(); ++gi )
delete *gi;
}
protected:
typedef typename GroupType::const_iterator cGtI;
GroupType& t_;
ProcessFields pFields_;
ProcessRows pRows_;
const bool pAll_;
long cnt_;
};
} // namespace Visitors
#endif // MULTIVISITOR_HPP
|