/** \file dynamic_set_transform.h */ // -*-c++-*- // Copyright (C) 2010 Daniel Burrows // // 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; see the file COPYING. If not, write to // the Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. #ifndef DYNAMIC_SET_TRANSFORM_H #define DYNAMIC_SET_TRANSFORM_H #include "dynamic_set.h" #include "enumerator_transform.h" #include #include #include #include namespace aptitude { namespace util { /** \brief A wrapper that passes each element in a dynamic set * through a function before passing it to client code. * * \tparam From The type of the values stored in the * underlying dynamic_set. * \tparam To The type of value produced by the transform. */ template class dynamic_set_transform : public dynamic_set { std::shared_ptr > wrapped_set; boost::function f; sigc::signal signal_inserted; sigc::signal signal_removed; void handle_inserted(const From &from); void handle_removed(const From &from); public: /** \warning Should only be used by create(). */ dynamic_set_transform(const std::shared_ptr > &_wrapped_set, const boost::function &_f); static std::shared_ptr create(const std::shared_ptr > &wrapped_set, const boost::function &f); std::size_t size(); std::shared_ptr > enumerate(); sigc::connection connect_inserted(const sigc::slot &slot); sigc::connection connect_removed(const sigc::slot &slot); }; template dynamic_set_transform::dynamic_set_transform(const std::shared_ptr > &_wrapped_set, const boost::function &_f) : wrapped_set(_wrapped_set), f(_f) { wrapped_set->connect_inserted(sigc::mem_fun(*this, &dynamic_set_transform::handle_inserted)); wrapped_set->connect_removed(sigc::mem_fun(*this, &dynamic_set_transform::handle_removed)); } template std::shared_ptr > dynamic_set_transform::create(const std::shared_ptr > &wrapped_set, const boost::function &f) { return std::make_shared >(wrapped_set, f); } template std::size_t dynamic_set_transform::size() { return wrapped_set->size(); } template std::shared_ptr > dynamic_set_transform::enumerate() { return enumerator_transform::create(wrapped_set->enumerate(), f); } template void dynamic_set_transform::handle_inserted(const From &from) { signal_inserted(f(from)); } template void dynamic_set_transform::handle_removed(const From &from) { signal_removed(f(from)); } template sigc::connection dynamic_set_transform::connect_inserted(const sigc::slot &slot) { return signal_inserted.connect(slot); } template sigc::connection dynamic_set_transform::connect_removed(const sigc::slot &slot) { return signal_removed.connect(slot); } } } #endif // DYNAMIC_SET_TRANSFORM_H