PageRenderTime 483ms CodeModel.GetById 152ms app.highlight 195ms RepoModel.GetById 115ms app.codeStats 0ms

/mordor/streams/singleplex.cpp

http://github.com/mozy/mordor
C++ | 98 lines | 82 code | 15 blank | 1 comment | 25 complexity | 7c25e4b959d1c094f11db07ad729a6a0 MD5 | raw file
 1// Copyright (c) 2010 - Mozy, Inc.
 2
 3#include "singleplex.h"
 4
 5#include "mordor/assert.h"
 6
 7namespace Mordor {
 8
 9SingleplexStream::SingleplexStream(Stream::ptr parent, Type type, bool own)
10    : FilterStream(parent, own),
11      m_type(type)
12{
13    MORDOR_ASSERT(type == READ || type == WRITE);
14    if (type == READ) MORDOR_ASSERT(parent->supportsRead());
15    if (type == WRITE) MORDOR_ASSERT(parent->supportsWrite());
16}
17
18void
19SingleplexStream::close(CloseType type)
20{
21    if (ownsParent()) {
22        if (m_type == READ && (type & Stream::READ)) {
23            parent()->close(parent()->supportsHalfClose() ?
24                Stream::READ : BOTH);
25        } else if (m_type == WRITE && (type & Stream::WRITE)) {
26            parent()->close(parent()->supportsHalfClose() ?
27                Stream::WRITE : BOTH);
28        }
29    }
30}
31
32size_t
33SingleplexStream::read(Buffer &buffer, size_t length)
34{
35    MORDOR_ASSERT(m_type == READ);
36    return parent()->read(buffer, length);
37}
38
39size_t
40SingleplexStream::read(void * buffer, size_t length)
41{
42    MORDOR_ASSERT(m_type == READ);
43    return parent()->read(buffer, length);
44}
45
46size_t
47SingleplexStream::write(const Buffer &buffer, size_t length)
48{
49    MORDOR_ASSERT(m_type == WRITE);
50    return parent()->write(buffer, length);
51}
52
53size_t
54SingleplexStream::write(const void *buffer, size_t length)
55{
56    MORDOR_ASSERT(m_type == WRITE);
57    return parent()->write(buffer, length);
58}
59
60void
61SingleplexStream::truncate(long long size)
62{
63    MORDOR_ASSERT(m_type == WRITE);
64    return parent()->truncate(size);
65}
66
67void
68SingleplexStream::flush(bool flushParent)
69{
70    if (m_type == READ)
71        return;
72    return parent()->flush(flushParent);
73}
74
75ptrdiff_t
76SingleplexStream::find(char delimiter, size_t sanitySize,
77        bool throwIfNotFound)
78{
79    MORDOR_ASSERT(m_type == READ);
80    return parent()->find(delimiter, sanitySize, throwIfNotFound);
81}
82
83ptrdiff_t
84SingleplexStream::find(const std::string &delimiter, size_t sanitySize,
85    bool throwIfNotFound)
86{
87    MORDOR_ASSERT(m_type == READ);
88    return parent()->find(delimiter, sanitySize, throwIfNotFound);
89}
90
91void
92SingleplexStream::unread(const Buffer &buffer, size_t length)
93{
94    MORDOR_ASSERT(m_type == READ);
95    return parent()->unread(buffer, length);
96}
97
98}