AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
Pipe.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13
   14#include <AUI/Traits/values.h>
   15#include <AUI/IO/IInputStream.h>
   16#include <AUI/IO/IOutputStream.h>
   17
   18#if AUI_PLATFORM_WIN
   19#include <Windows.h>
   20#else
   21#include <unistd.h>
   22#include <sys/types.h>
   23#include <sys/wait.h>
   24#include <cassert>
   25#include <spawn.h>
   26#endif
   27
   28
   37class API_AUI_CORE Pipe final: public aui::noncopyable, public IInputStream, public IOutputStream {
   38public:
   39#if AUI_PLATFORM_WIN
   40    using pipe_t = HANDLE;
   41#else
   42    using pipe_t = int;
   43#endif
   44
   45    Pipe();
   46    Pipe(Pipe&& rhs) noexcept {
   47        operator=(std::move(rhs));
   48    }
   49    ~Pipe();
   50
   51    Pipe& operator=(Pipe&& rhs) noexcept {
   52        mIn = rhs.mIn;
   53        mOut = rhs.mOut;
   54        rhs.mIn = rhs.mOut = static_cast<pipe_t>(0);
   55        return *this;
   56    }
   57
   58
   62    [[nodiscard]]
   63    pipe_t out() const noexcept {
   64        return mOut;
   65    }
   66
   70    [[nodiscard]]
   71    pipe_t in() const noexcept {
   72        return mIn;
   73    }
   74
   78    void closeOut() noexcept;
   79
   83    void closeIn() noexcept;
   84
   90    [[nodiscard]]
   91    pipe_t stealOut() noexcept {
   92        auto copy = mOut;
   93        mOut = 0;
   94        return copy;
   95    }
   96
  102    [[nodiscard]]
  103    pipe_t stealIn() noexcept {
  104        auto copy = mIn;
  105        mIn = 0;
  106        return copy;
  107    }
  108    size_t read(char* dst, size_t size) override;
  109    void write(const char* src, size_t size) override;
  110
  111private:
  115    pipe_t mOut;
  116
  120    pipe_t mIn;
  121};
Represents an input stream.
Definition IInputStream.h:26
Definition IOutputStream.h:18
void write(const char *src, size_t size) override
Writes exact size bytes to stream. Blocking (waiting for write all data) is allowed.
pipe_t stealIn() noexcept
Steals ownership of the in pipe outside of the Pipe class.
Definition Pipe.h:103
pipe_t stealOut() noexcept
Steals ownership of the out pipe outside of the Pipe class.
Definition Pipe.h:91
size_t read(char *dst, size_t size) override
Reads up to size bytes from stream. Blocking (waiting for new data) is allowed.
void closeOut() noexcept
Close out. Also known as close(pipe[0])
pipe_t in() const noexcept
In pipe. Also known as pipe[1].
Definition Pipe.h:71
pipe_t out() const noexcept
Out pipe. Also known as pipe[0].
Definition Pipe.h:63
void closeIn() noexcept
Close in. Also known as close(pipe[1])
Forbids copy of your class.
Definition values.h:45