source: NonGTP/Boost/boost/iostreams/detail/counted_array.hpp @ 857

Revision 857, 2.0 KB checked in by igarcia, 18 years ago (diff)
Line 
1// (C) Copyright Jonathan Turkanis 2005.
2// Distributed under the Boost Software License, Version 1.0. (See accompanying
3// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
4
5// See http://www.boost.org/libs/iostreams for documentation.
6
7#ifndef BOOST_IOSTREAMS_DETAIL_COUNTED_ARRAY_HPP_INCLUDED
8#define BOOST_IOSTREAMS_DETAIL_COUNTED_ARRAY_HPP_INCLUDED
9
10#include <algorithm>                               // min.
11#include <boost/iostreams/categories.hpp>
12#include <boost/iostreams/detail/char_traits.hpp>
13#include <boost/iostreams/detail/ios.hpp>          // streamsize.
14
15namespace boost { namespace iostreams { namespace detail {
16
17template<typename Ch>
18class counted_array_source {
19public:
20    typedef Ch          char_type;
21    typedef source_tag  category;
22    counted_array_source(const Ch* buf, std::streamsize size)
23        : buf_(buf), ptr_(0), end_(size)
24        { }
25    std::streamsize read(Ch* s, std::streamsize n)
26    {
27        std::streamsize result = (std::min)(n, end_ - ptr_);
28        BOOST_IOSTREAMS_CHAR_TRAITS(char_type)::copy
29            (s, buf_ + ptr_, result);
30        ptr_ += result;
31        return result;
32    }
33    std::streamsize count() const { return ptr_; }
34private:
35    const Ch*        buf_;
36    std::streamsize  ptr_, end_;
37};
38
39template<typename Ch>
40struct counted_array_sink {
41public:
42    typedef Ch        char_type;
43    typedef sink_tag  category;
44    counted_array_sink(Ch* buf, std::streamsize size)
45        : buf_(buf), ptr_(0), end_(size)
46        { }
47        std::streamsize write(const Ch* s, std::streamsize n)
48    {
49        std::streamsize result = (std::min)(n, end_ - ptr_);
50        BOOST_IOSTREAMS_CHAR_TRAITS(char_type)::copy
51            (buf_ + ptr_, s, result);
52        ptr_ += result;
53        return result;
54    }
55    std::streamsize count() const { return ptr_; }
56private:
57    Ch*              buf_;
58    std::streamsize  ptr_, end_;
59};
60
61} } } // End namespaces iostreams, boost.
62
63#endif // #ifndef BOOST_IOSTREAMS_DETAIL_COUNTED_ARRAY_HPP_INCLUDED
Note: See TracBrowser for help on using the repository browser.