GPU-SD  1.1.2
filter.cpp
00001 
00002 /* Copyright (c) 2011-2012, Stefan Eilemann <eile@eyescale.ch> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #include "filter.h"
00019 #include <gpusd/gpuInfo.h>
00020 
00021 #include <algorithm>
00022 
00023 namespace gpusd
00024 {
00026 typedef std::vector< FilterPtr > Filters;
00027 typedef Filters::const_iterator FiltersCIter;
00030 // Filter
00031 //-------
00032 namespace detail
00033 {
00034 class Filter
00035 {
00036 public:
00037     Filters next_;
00038 };
00039 }
00040 
00041 Filter::Filter() : impl_( new detail::Filter ) {}
00042 Filter::~Filter() { delete impl_; }
00043 
00044 bool Filter::operator() ( const GPUInfos& current, const GPUInfo& candidate )
00045 {
00046     for( FiltersCIter i = impl_->next_.begin(); i!=impl_->next_.end(); ++i)
00047     {
00048         FilterPtr filter = *i;
00049         if( !(*filter)( current, candidate ))
00050             return false;
00051     }
00052     return true;
00053 }
00054 
00055 FilterPtr Filter::operator | ( FilterPtr rhs )
00056 {
00057     impl_->next_.push_back( rhs );
00058     return rhs;
00059 }
00060 
00061 FilterPtr Filter::operator |= ( FilterPtr rhs )
00062 {
00063     impl_->next_.push_back( rhs );
00064     return rhs;
00065 }
00066 
00067 // DuplicateFilter
00068 //----------------
00069 bool DuplicateFilter::operator() ( const GPUInfos& current,
00070                                    const GPUInfo& candidate )
00071 {
00072     GPUInfosCIter i = std::find( current.begin(), current.end(), candidate );
00073     if( i == current.end())
00074         return Filter::operator()( current, candidate );
00075 
00076     // TODO: break API in 2.0, making current non-const?
00077     GPUInfo& info = const_cast< GPUInfo& >( *i );
00078     info.flags |= candidate.flags; // merge flags from dropped info
00079     return false;
00080 }
00081 
00082 // MirrorFilter
00083 //-------------
00084 bool MirrorFilter::operator() ( const GPUInfos& current,
00085                                 const GPUInfo& candidate )
00086 {
00087     for( GPUInfosCIter i = current.begin(); i != current.end(); ++i )
00088     {
00089         const GPUInfo& info = *i;
00090         if( info.hostname == candidate.hostname &&
00091             info.session == candidate.session &&
00092             info.device == candidate.device &&
00093             info.pvp[0] == candidate.pvp[0] && info.pvp[1] == candidate.pvp[1] )
00094         {
00095              return false;
00096         }
00097     }
00098 
00099     return Filter::operator()( current, candidate );
00100 }
00101 
00102 // SessionFilter
00103 //--------------
00104 namespace detail
00105 {
00106 class SessionFilter
00107 {
00108 public:
00109     SessionFilter( const std::string& name ) : name_( name ) {}
00110 
00111     const std::string& name_;
00112 };
00113 }
00114 
00115 SessionFilter::SessionFilter( const std::string& name )
00116         : impl_( new detail::SessionFilter( name ))
00117 {}
00118 
00119 SessionFilter::~SessionFilter() { delete impl_; }
00120 
00121 bool SessionFilter::operator() ( const GPUInfos& current,
00122                                  const GPUInfo& candidate )
00123 {
00124     if( candidate.session == impl_->name_ )
00125         return Filter::operator()( current, candidate );
00126     return false;
00127 }
00128 
00129 }
Generated on Thu Jun 14 2012 07:53:07 for GPU-SD 1.1.2 by  doxygen 1.8.0