faketwowaystream.h

来自「一个语言识别引擎」· C头文件 代码 · 共 111 行

H
111
字号
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-

/*
 * Copyright (C) 2006 Paul Fitzpatrick
 * CopyPolicy: Released under the terms of the GNU GPL v2.0.
 *
 */

#ifndef _YARP2_FAKETWOWAYSTREAM_
#define _YARP2_FAKETWOWAYSTREAM_

#include <yarp/TwoWayStream.h>
#include <yarp/StringInputStream.h>
#include <yarp/StringOutputStream.h>

namespace yarp {
    class FakeTwoWayStream;
}


/**
 * A dummy two way stream for testing purposes.
 */
class yarp::FakeTwoWayStream : public TwoWayStream {
public:
    FakeTwoWayStream(StringInputStream *target = NULL) : out(this) {
        this->target = target;
    }

    void setTarget(StringInputStream& target) {
        this->target = &target;
    }

    virtual InputStream& getInputStream() {
        return in;
    }

    virtual StringInputStream& getStringInputStream() {
        return in;
    }

    virtual OutputStream& getOutputStream() {
        return out;
    }

    virtual const Address& getLocalAddress() {
        return local;
    }

    virtual const Address& getRemoteAddress() {
        return remote;
    }

    virtual void close() {
        in.close();
        out.close();
    }

    virtual void apply(const Bytes& b) {
        if (target!=NULL) {
            target->add(b);
        }
    }

    void addInputText(const String& str) {
        in.add(str.c_str());
    }

    String getOutputText() {
        return out.toString();
    }

    String getInputText() {
        return in.toString();
    }

    virtual bool isOk() {
        return true;
    }

    virtual void reset() {
    }

    virtual void beginPacket() { }

    virtual void endPacket() { }

private:

    class ActiveStringOutputStream : public StringOutputStream {
    public:
        ActiveStringOutputStream(FakeTwoWayStream *who) : owner(*who) {
        }

        virtual void write(const Bytes& b) {
            StringOutputStream::write(b);
            owner.apply(b);
        }
    private:
        FakeTwoWayStream& owner;
    };

    StringInputStream in;
    ActiveStringOutputStream out;
    Address local;
    Address remote;
    StringInputStream *target;
};

#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?