|
yast2-storage
|
00001 /* 00002 * Copyright (c) [2004-2010] Novell, Inc. 00003 * 00004 * All Rights Reserved. 00005 * 00006 * This program is free software; you can redistribute it and/or modify it 00007 * under the terms of version 2 of the GNU General Public License as published 00008 * by the Free Software Foundation. 00009 * 00010 * This program is distributed in the hope that it will be useful, but WITHOUT 00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 00013 * more details. 00014 * 00015 * You should have received a copy of the GNU General Public License along 00016 * with this program; if not, contact Novell, Inc. 00017 * 00018 * To contact Novell about this file by physical or electronic mail, you may 00019 * find current contact information at www.novell.com. 00020 */ 00021 00022 00023 #ifndef REGION_H 00024 #define REGION_H 00025 00026 #include <algorithm> 00027 00028 #include "storage/XmlFile.h" 00029 00030 00031 namespace storage 00032 { 00033 00034 class Region 00035 { 00036 public: 00037 00038 Region() : s(0), l(0) {} 00039 Region(unsigned long long start, unsigned long long len) : s(start), l(len) {} 00040 00041 bool doIntersect( const Region& r ) const 00042 { return( r.start() <= end() && r.end() >= start() ); } 00043 Region intersect( const Region& r ) const 00044 { 00045 if (doIntersect(r)) 00046 { 00047 unsigned long long s = std::max(r.start(), start()); 00048 unsigned long long e = std::min(r.end(), end()); 00049 return Region(s, e - s + 1); 00050 } 00051 return Region(0, 0); 00052 } 00053 bool inside( const Region& r ) const 00054 { return( start()>=r.start() && end() <= r.end() ); } 00055 bool operator==(const Region& r) const 00056 { return( r.start()==s && r.len()==l ); } 00057 bool operator!=(const Region& r) const 00058 { return( ! (*this==r) ); } 00059 bool operator<(const Region& r) const 00060 { return( s < r.start() ); } 00061 bool operator>(const Region& r) const 00062 { return( s > r.start() ); } 00063 00064 unsigned long long start() const { return s; } 00065 unsigned long long len() const { return l; } 00066 unsigned long long end() const { return s + l - 1; } 00067 00068 bool empty() const { return l == 0; } 00069 00070 void setStart(unsigned long long start) { s = start; } 00071 void setLen(unsigned long long len) { l = len; } 00072 00073 template <typename Type> friend 00074 Region operator*(Type i, const Region& r) 00075 { 00076 static_assert(std::is_integral<Type>::value, "not integral"); 00077 return Region(i * r.s, i * r.l); 00078 } 00079 00080 template <typename Type> friend 00081 Region operator/(const Region& r, Type i) 00082 { 00083 static_assert(std::is_integral<Type>::value, "not integral"); 00084 return Region(r.s / i, r.l / i); 00085 } 00086 00087 friend std::ostream& operator<<(std::ostream& s, const Region& p); 00088 00089 friend bool getChildValue(const xmlNode* node, const char* name, Region& value); 00090 friend void setChildValue(xmlNode* node, const char* name, const Region& value); 00091 00092 protected: 00093 00094 unsigned long long s; 00095 unsigned long long l; 00096 00097 }; 00098 00099 } 00100 00101 #endif
1.7.3