Developer documentation
Version 3.0.3-105-gd3941f44
base.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __image_io_base_h__
18#define __image_io_base_h__
19
20#include <cassert>
21#include <cstdint>
22#include <unistd.h>
23
24#include "memory.h"
25#include "mrtrix.h"
26#include "types.h"
27#include "file/entry.h"
28
29#define MAX_FILES_PER_IMAGE 256U
30
31namespace MR
32{
33
34 class Header;
35
37
41 namespace ImageIO
42 {
43
44 class Base
46 public:
47 Base (const Header& header);
48 Base (Base&&) noexcept = default;
49 Base (const Base&) = delete;
50 Base& operator=(const Base&) = delete;
51
52 virtual ~Base ();
53
54 virtual bool is_file_backed () const;
55
56 // buffer_size is only used for scratch data; it is ignored in all
57 // other (file-backed) handlers, where the buffer size is determined
58 // from the information in the header
59 void open (const Header& header, size_t buffer_size = 0);
60 void close (const Header& header);
61
62 bool is_image_new () const { return is_new; }
63 bool is_image_readwrite () const { return writable; }
64
65 void set_readwrite (bool readwrite) {
66 writable = readwrite;
67 }
68 void set_image_is_new (bool image_is_new) {
69 is_new = image_is_new;
70 }
71 void set_readwrite_if_existing (bool readwrite) {
72 if (!is_new)
73 writable = readwrite;
74 }
75
76 uint8_t* segment (size_t n) const {
77 assert (n < addresses.size());
78 return addresses[n].get();
79 }
80 size_t nsegments () const {
81 return addresses.size();
82 }
83 size_t segment_size () const {
84 check();
85 return segsize;
86 }
87
89
90 void merge (const Base& B) {
91 assert (addresses.empty());
92 for (size_t n = 0; n < B.files.size(); ++n)
93 files.push_back (B.files[n]);
94 segsize += B.segsize;
95 }
96
97 friend std::ostream& operator<< (std::ostream& stream, const Base& B) {
98 stream << str (B.files.size()) << " files, segsize " << str(B.segsize)
99 << ", is " << (B.is_new ? "" : "NOT ") << "new, " << (B.writable ? "read/write" : "read-only");
100 return stream;
101 }
102
103 protected:
104 size_t segsize;
107
108 void check () const {
109 assert (addresses.size());
110 }
111 virtual void load (const Header& header, size_t buffer_size) = 0;
112 virtual void unload (const Header& header) = 0;
113 };
114
115 }
116
117}
118
119#endif
120
vector< std::unique_ptr< uint8_t[]> > addresses
Definition: base.h:105
bool writable
Definition: base.h:106
void open(const Header &header, size_t buffer_size=0)
vector< File::Entry > files
Definition: base.h:88
void close(const Header &header)
void check() const
Definition: base.h:108
Base(const Header &header)
bool is_image_readwrite() const
Definition: base.h:63
size_t segsize
Definition: base.h:104
bool is_image_new() const
Definition: base.h:62
Base(Base &&) noexcept=default
size_t segment_size() const
Definition: base.h:83
friend std::ostream & operator<<(std::ostream &stream, const Base &B)
Definition: base.h:97
void set_image_is_new(bool image_is_new)
Definition: base.h:68
void set_readwrite_if_existing(bool readwrite)
Definition: base.h:71
virtual void load(const Header &header, size_t buffer_size)=0
void set_readwrite(bool readwrite)
Definition: base.h:65
uint8_t * segment(size_t n) const
Definition: base.h:76
size_t nsegments() const
Definition: base.h:80
void merge(const Base &B)
Definition: base.h:90
virtual void unload(const Header &header)=0
virtual bool is_file_backed() const
#define NOMEMALIGN
Definition: memory.h:22
Definition: base.h:24
std::string str(const T &value, int precision=0)
Definition: mrtrix.h:247