libstorage-ng
Region.h
1 /*
2  * Copyright (c) [2004-2015] Novell, Inc.
3  * Copyright (c) [2016-2017] SUSE LLC
4  *
5  * All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License as published
9  * by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, contact Novell, Inc.
18  *
19  * To contact Novell about this file by physical or electronic mail, you may
20  * find current contact information at www.novell.com.
21  */
22 
23 
24 #ifndef STORAGE_REGION_H
25 #define STORAGE_REGION_H
26 
27 
28 #include <libxml/tree.h>
29 #include <memory>
30 #include <vector>
31 
32 #include "storage/Utils/Exception.h"
33 
34 
35 namespace storage
36 {
37 
38  class InvalidBlockSize : public Exception
39  {
40  public:
41  InvalidBlockSize(unsigned int block_size);
42  };
43 
44 
46  {
47  public:
48  DifferentBlockSizes(unsigned int seen, unsigned int expected);
49  };
50 
51 
52  class NoIntersection : public Exception
53  {
54  public:
56  };
57 
58 
59  class NotInside : public Exception
60  {
61  public:
62  NotInside();
63  };
64 
65 
73  class Region
74  {
75  public:
76 
77  Region();
78  Region(unsigned long long start, unsigned long long length, unsigned int block_size);
79  Region(const Region& region);
80  Region(Region&& region) = default;
81  ~Region();
82 
83  Region& operator=(const Region& region);
84  Region& operator=(Region&& region) = default;
85 
86  bool empty() const;
87 
91  unsigned long long get_start() const;
92 
96  unsigned long long get_length() const;
97 
104  unsigned long long get_end() const;
105 
109  void set_start(unsigned long long start);
110 
114  void set_length(unsigned long long length);
115 
121  void adjust_start(long long delta);
122 
128  void adjust_length(long long delta);
129 
130  unsigned int get_block_size() const;
131  void set_block_size(unsigned int block_size);
132 
133  unsigned long long to_bytes(unsigned long long blocks) const;
134  unsigned long long to_blocks(unsigned long long bytes) const;
135 
141  bool operator==(const Region& rhs) const;
142 
148  bool operator!=(const Region& rhs) const;
149 
155  bool operator<(const Region& rhs) const;
156 
162  bool operator>(const Region& rhs) const;
163 
169  bool operator<=(const Region& rhs) const;
170 
176  bool operator>=(const Region& rhs) const;
177 
183  bool inside(const Region& rhs) const;
184 
190  bool intersect(const Region& rhs) const;
191 
192  Region intersection(const Region& rhs) const;
193 
200  std::vector<Region> unused_regions(const std::vector<Region>& used_regions) const;
201 
202  friend std::ostream& operator<<(std::ostream& s, const Region& region);
203 
204  public:
205 
206  class Impl;
207 
208  Impl& get_impl();
209  const Impl& get_impl() const;
210 
211  friend bool getChildValue(const xmlNode* node, const char* name, Region& value);
212  friend void setChildValue(xmlNode* node, const char* name, const Region& value);
213 
214  private:
215 
216  const std::unique_ptr<Impl> impl;
217 
218  };
219 
220 }
221 
222 #endif
std::vector< Region > unused_regions(const std::vector< Region > &used_regions) const
Returns all regions not included in used_regions.
bool operator>(const Region &rhs) const
Compare start of two regions.
unsigned long long get_start() const
Returns the start of the region.
bool operator>=(const Region &rhs) const
Compare start of two regions.
A start/length pair with a block size.
Definition: Region.h:73
bool operator<(const Region &rhs) const
Compare start of two regions.
bool inside(const Region &rhs) const
Check whether the region is contained inside other.
unsigned long long get_length() const
Returns the length of the region.
unsigned long long get_end() const
Returns the end of the region.
Definition: Region.h:59
bool operator!=(const Region &rhs) const
Compare start and length of two regions.
bool operator<=(const Region &rhs) const
Compare start of two regions.
void adjust_start(long long delta)
Adjusts the start while keeping the length.
Definition: Region.h:45
void adjust_length(long long delta)
Adjusts the length while keeping the start.
void set_length(unsigned long long length)
Sets the length while keeping the start.
Base class for storage exceptions.
Definition: Exception.h:113
Definition: Region.h:52
The storage namespace.
Definition: Actiongraph.h:37
bool intersect(const Region &rhs) const
Check whether the region intersects with other.
bool operator==(const Region &rhs) const
Compare start and length of two regions.
Definition: Region.h:38
void set_start(unsigned long long start)
Sets the start while keeping the length.