Drizzled Public API Documentation

ioutil.h

00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #pragma once
00021 
00022 #include <iosfwd>
00023 #include <string>
00024 
00025 namespace drizzled
00026 {
00027 namespace message
00028 {
00029 namespace ioutil
00030 {
00031 
00037   template <class FwdIter> class joiner {
00038     friend std::ostream& operator<<(std::ostream& out, const joiner& j) {
00039       j.write(out);
00040       return out;
00041     }
00042 
00043   public:
00044     explicit joiner(const std::string& separator, FwdIter start, FwdIter finish)
00045       : m_sep(separator), m_start(start), m_finish(finish)
00046     { }
00047 
00048 
00049   private:
00050     std::string m_sep;
00051     FwdIter m_start, m_finish;
00052 
00053     void write(std::ostream& out) const {
00054       if (m_start == m_finish)
00055         return;
00056 
00057       FwdIter fi = m_start;
00058       while (true) {
00059         out << *fi;
00060         if (++fi == m_finish)
00061           break;
00062         out << m_sep;
00063       }
00064     }
00065   };
00066 
00067 
00078   template <class FwdIter>
00079   joiner<FwdIter>
00080   join(const std::string& delim, FwdIter start, FwdIter finish) {
00081     return joiner<FwdIter>(delim, start, finish);
00082   }
00083 
00085   template <class Container>
00086   joiner<typename Container::const_iterator>
00087   join(const std::string& delim, Container seq) {
00088     typedef typename Container::const_iterator FwdIter;
00089     return joiner<FwdIter>(delim, seq.begin(), seq.end());
00090   }
00091 
00092 } /* namespace ioutil */
00093 } /* namespace message */
00094 } /* namespace drizzled */
00095