FINAL CUT
pipedata.h
1 /***********************************************************************
2 * pipedata.h - Provides a pipe array to hold the file descriptors *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2023-2026 Markus Gans *
7 * *
8 * FINAL CUT is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation; either version 3 of *
11 * the License, or (at your option) any later version. *
12 * *
13 * FINAL CUT is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this program. If not, see *
20 * <http://www.gnu.org/licenses/>. *
21 ***********************************************************************/
22 
23 /* Standalone class
24  * ════════════════
25  *
26  * ▕▔▔▔▔▔▔▔▔▔▔▏
27  * ▕ PipeData ▏
28  * ▕▁▁▁▁▁▁▁▁▁▁▏
29  */
30 
31 #ifndef PIPEDATA_H
32 #define PIPEDATA_H
33 
34 #include <array>
35 #include <type_traits>
36 #include <stdexcept>
37 
38 namespace finalcut
39 {
40 
41 // class forward declaration
42 class FString;
43 
44 //----------------------------------------------------------------------
45 // class PipeData
46 //----------------------------------------------------------------------
47 
48 class PipeData final
49 {
50  public:
51  // Constant
52  static constexpr int NO_FILE_DESCRIPTOR{-1};
53 
54  // Constructor
55  PipeData() noexcept
56  : pipe_fd{{NO_FILE_DESCRIPTOR, NO_FILE_DESCRIPTOR}}
57  { }
58 
59  explicit PipeData (int read_fd, int write_fd) noexcept
60  : pipe_fd{{read_fd, write_fd}}
61  { }
62 
63  // Copy constructor
64  PipeData (const PipeData&) = default;
65 
66  // Move constructor
67  PipeData (PipeData&&) noexcept = default;
68 
69  // Destructor
70  ~PipeData() = default;
71 
72  // Copy assignment operator
73  auto operator = (const PipeData&) -> PipeData& = default;
74 
75  // Move assignment operator
76  auto operator = (PipeData&&) noexcept -> PipeData& = default;
77 
78  // Accessors
79  auto getClassName() const -> FString;
80  auto getArrayData() const noexcept -> const int*;
81  auto getArrayData() noexcept -> int*;
82  auto getReadFd() const noexcept -> int;
83  auto getWriteFd() const noexcept -> int;
84 
85  // Mutators
86  void setReadFd (int) noexcept;
87  void setWriteFd (int) noexcept;
88  void setPipe (int, int) noexcept;
89  void reset() noexcept;
90  void swap (PipeData&) noexcept;
91 
92  private:
93  // Enumeration
94  enum class Array : unsigned char
95  {
96  Read = 0, // Read end of pipe
97  Write = 1, // Write end of pipe
98  Size = 2 // Size of pipe array
99  };
100 
101  // Using-declaration
102  using ArrayT = std::underlying_type_t<Array>;
103  static constexpr auto PIPE_FD_COUNT = static_cast<ArrayT>(Array::Size);
104  using PipeDataType = std::array<int, PIPE_FD_COUNT>;
105 
106  // Data member
107  PipeDataType pipe_fd{};
108 };
109 
110 //----------------------------------------------------------------------
111 inline auto PipeData::getArrayData() const noexcept -> const int*
112 {
113  return pipe_fd.data();
114 }
115 
116 //----------------------------------------------------------------------
117 inline auto PipeData::getArrayData() noexcept -> int*
118 {
119  return pipe_fd.data();
120 }
121 
122 //----------------------------------------------------------------------
123 inline auto PipeData::getReadFd() const noexcept -> int
124 {
125  return pipe_fd[static_cast<ArrayT>(Array::Read)];
126 }
127 
128 //----------------------------------------------------------------------
129 inline auto PipeData::getWriteFd() const noexcept -> int
130 {
131  return pipe_fd[static_cast<ArrayT>(Array::Write)];
132 }
133 //----------------------------------------------------------------------
134 inline void PipeData::setReadFd(int fd) noexcept
135 {
136  pipe_fd[static_cast<ArrayT>(Array::Read)] = fd;
137 }
138 
139 //----------------------------------------------------------------------
140 inline void PipeData::setWriteFd(int fd) noexcept
141 {
142  pipe_fd[static_cast<ArrayT>(Array::Write)] = fd;
143 }
144 
145 //----------------------------------------------------------------------
146 inline void PipeData::setPipe(int read_fd, int write_fd) noexcept
147 {
148  pipe_fd[static_cast<ArrayT>(Array::Read)] = read_fd;
149  pipe_fd[static_cast<ArrayT>(Array::Write)] = write_fd;
150 }
151 
152 //----------------------------------------------------------------------
153 inline void PipeData::reset() noexcept
154 {
155  pipe_fd[static_cast<ArrayT>(Array::Read)] = NO_FILE_DESCRIPTOR;
156  pipe_fd[static_cast<ArrayT>(Array::Write)] = NO_FILE_DESCRIPTOR;
157 }
158 
159 //----------------------------------------------------------------------
160 inline void PipeData::swap (PipeData& other) noexcept
161 { pipe_fd.swap(other.pipe_fd); }
162 
163 } // namespace finalcut
164 
165 #endif // PIPEDATA_H
Definition: class_template.cpp:25
Definition: fstring.h:82
Definition: pipedata.h:48