• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MikkelSchubert / adapterremoval / #38

07 Aug 2024 07:47PM UTC coverage: 83.365% (-3.5%) from 86.839%
#38

push

travis-ci

MikkelSchubert
additional tests

2190 of 2627 relevant lines covered (83.37%)

15528.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/src/managed_io.hpp
1
/*************************************************************************\
2
 * AdapterRemoval - cleaning next-generation sequencing reads            *
3
 *                                                                       *
4
 * Copyright (C) 2015 by Mikkel Schubert - mikkelsch@gmail.com           *
5
 *                                                                       *
6
 * This program is free software: you can redistribute it and/or modify  *
7
 * it under the terms of the GNU General Public License as published by  *
8
 * the Free Software Foundation, either version 3 of the License, or     *
9
 * (at your option) any later version.                                   *
10
 *                                                                       *
11
 * This program is distributed in the hope that it will be useful,       *
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 * GNU General Public License for more details.                          *
15
 *                                                                       *
16
 * You should have received a copy of the GNU General Public License     *
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
18
\*************************************************************************/
19
#pragma once
20

21
#include "buffer.hpp" // for buffer_vec
22
#include <array>      // for array
23
#include <cstdio>     // for FILE
24
#include <string>     // for string
25

26
namespace adapterremoval {
27

28
/** Indicates if the writes should be flushed */
29
enum class flush
30
{
31
  on,
32
  off
33
};
34

35
/** Reader that closes unused writer handles if open files exceeds ulimits. */
36
class managed_reader
37
{
38
public:
39
  /** Take ownership of an existing file handle */
40
  explicit managed_reader(FILE* handle);
41
  /*
42
   * Opens a managed reader. If too many handles are used, this function will
43
   * close writers until the file can be successfully opened.
44
   */
45
  explicit managed_reader(std::string filename);
46

47
  /** Closes the handle if it has not been closed already */
48
  ~managed_reader();
49

50
  /** Closes the handle; no-op if the handle has been closed already */
51
  void close();
52

53
  /** Returns the filename of the (previously opened) file */
54
  const std::string& filename() const { return m_filename; }
34✔
55

56
  /** Reads `size` bytes into the destination buffer */
57
  size_t read(void* buffer, size_t size);
58

59
  managed_reader(const managed_reader&) = delete;
60
  managed_reader(managed_reader&& other) = delete;
61
  managed_reader& operator=(managed_reader&& other) = delete;
62
  managed_reader& operator=(const managed_reader&) = delete;
63

64
private:
65
  //! Source filename
66
  std::string m_filename{};
67
  //! File handle or nullptr if the file has been closed
68
  FILE* m_file = nullptr;
69

70
  friend class io_manager;
71
};
72

73
/**
74
 * Writer that manages open handles if open files exceeds ulimits.
75
 *
76
 * The file is lazily opened the first time a write is performed;
77
 * if the file cannot be opened due to the number of already open
78
 * files, the writer will close the least recently used handle and
79
 * retry.
80
 */
81
class managed_writer
82
{
83
public:
84
  /** Create lazy writer; does not open filename immediately */
85
  explicit managed_writer(std::string filename);
86
  /** Checks that the file handle has been closed */
87
  ~managed_writer();
88

89
  /** Write buffers, opening/creating the file as needed */
90
  void write(const buffer& buf, flush mode = flush::off);
91
  void write(const buffer_vec& buffers, flush mode = flush::off);
92
  void write(const std::string& buffer, flush mode = flush::off);
93

94
  /** Closes the handle; no-op if the handle has been closed already */
95
  void close();
96

97
  /** Returns the filename of the (previously opened) file */
98
  const std::string& filename() const { return m_filename; }
×
99

100
  managed_writer(const managed_writer&) = delete;
101
  managed_writer(managed_writer&& other) = delete;
102
  managed_writer& operator=(managed_writer&& other) = delete;
103
  managed_writer& operator=(const managed_writer&) = delete;
104

105
private:
106
  //! Destination filename; is created lazily.
107
  std::string m_filename{};
108
  //! Indicates if the file has been created/truncated
109
  bool m_created = false;
110
  //! Lazily opened, managed handle; may be closed to free up handles.
111
  FILE* m_file = nullptr;
112
  //! Indicates if the handle is a stream and can't be closed
113
  bool m_stream = false;
114

115
  //! Managed writer used more recently than this writer.
116
  managed_writer* m_prev = nullptr;
117
  //! Managed writer used prior to this writer.
118
  managed_writer* m_next = nullptr;
119

120
  friend class io_manager;
121
  friend class writer_lock;
122
};
123

124
} // namespace adapterremoval
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc