resource_region.hpp
00001 #if !defined(__RESOURCE_REGION_HPP__)
00002 #define __RESOURCE_REGION_HPP__
00003
00004
00005
00006
00007 #include "core/common.hpp"
00008
00009 namespace detail
00010 {
00015 template <typename AddressT>
00016 struct resource_region
00017 {
00018 typedef AddressT address_type;
00019
00023 AddressT base;
00024
00028 AddressT size;
00029
00030
00031
00032
00033 inline resource_region &operator ()(const AddressT base, const AddressT size=AddressT(1))
00034 {
00035 this->base=base;
00036 this->size=size;
00037 return *this;
00038 }
00039
00040
00041
00042
00046 inline bool is_empty(void) const
00047 {
00048 return size == 0;
00049 }
00050
00054 inline bool equals(const AddressT base, const AddressT size=AddressT(1)) const
00055 {
00056 return (base == this->base) && (size == this->size);
00057 }
00058
00062 friend inline bool operator ==(const resource_region &r1, const resource_region &r2)
00063 {
00064 return r1.equals(r2.base, r2.size);
00065 }
00066
00070 inline bool contains(const AddressT base, const AddressT size=AddressT(1)) const
00071 {
00072 return (base >= this->base) && (base + size <= this->base + this->size);
00073 }
00074
00078 inline bool contains(const resource_region &other) const
00079 {
00080 return contains(other.base, other.size);
00081 }
00082
00086 inline bool overlaps(const AddressT base, const AddressT size=AddressT(1)) const
00087 {
00088 return contains(base) || ((base < this->base) && ((base + size) > this->base));
00089 }
00090
00094 inline bool overlaps(const resource_region &other) const
00095 {
00096 return overlaps(other.base, other.size);
00097 }
00098
00103 inline const resource_region disjunct(const AddressT base, const AddressT size=AddressT(1)) const
00104 {
00105 const AddressT start=min(base, this->base),
00106 end=max(base + size, this->base + this->size);
00107 return (resource_region){base: start, size: end - start};
00108 }
00109
00114 friend inline const resource_region operator |(const resource_region &r1, const resource_region &r2)
00115 {
00116 return r1.disjunct(r2.base, r2.size);
00117 }
00118
00123 inline const resource_region conjunct(const AddressT base, const AddressT size=AddressT(1)) const
00124 {
00125 const AddressT start=max(base, this->base),
00126 end=min(base + size, this->base + this->size);
00127 return (resource_region){base: start, size: (start < end) ? end - start : 0};
00128 }
00129
00134 friend inline const resource_region operator &(const resource_region &r1, const resource_region &r2)
00135 {
00136 return r1.conjunct(r2.base, r2.size);
00137 }
00138
00139
00140
00141
00145 template <typename FormatT>
00146 inline int print(const char *prefix="", const char *suffix="") const
00147 {
00148 return log::print("%s%s-%s [%s]%s", prefix,
00149 format_hex<FormatT>(base).c_str(),
00150 format_hex<FormatT>(base + size - 1).c_str(),
00151 format_hex<FormatT>(size).c_str(), suffix);
00152 }
00153
00157 inline int print(const char *prefix="", const char *suffix="") const
00158 {
00159 return print<AddressT>(prefix, suffix);
00160 }
00161 };
00162 }
00163
00168 template <typename AddressT>
00169 struct resource_region : public detail::resource_region<AddressT>
00170 {
00171 typedef detail::resource_region<AddressT> super_type;
00172
00173
00174
00175
00176 inline resource_region(const AddressT base=AddressT(), const AddressT size=AddressT(1))
00177 {
00178 (*this)(base, size);
00179 }
00180
00181 inline resource_region(const super_type &other)
00182 {
00183 (*this)(other.base, other.size);
00184 }
00185 };
00186
00187 #endif
00188
00189
00190