diff options
author | Julian Andres Klode <julian.klode@canonical.com> | 2020-02-24 17:46:10 +0100 |
---|---|---|
committer | Julian Andres Klode <julian.klode@canonical.com> | 2020-02-24 18:29:07 +0100 |
commit | 4fad7262291a8af1415fb9a3693678bd9610f0d6 (patch) | |
tree | 39226545753b5f0910547747fb5c691398c7341a /apt-pkg/pkgcache.h | |
parent | 1f4e2ab7462f5e05e452fb8505185895d91651c2 (diff) |
Make map_pointer<T> typesafe
Instead of just using uint32_t, which would allow you to
assign e.g. a map_pointer<Version> to a map_pointer<Package>,
use our own smarter struct that has strict type checking.
We allow creating a map_pointer from a nullptr, and we allow
comparing map_pointer to nullptr, which also deals with comparisons
against 0 which are often used, as 0 will be implictly converted
to nullptr.
Diffstat (limited to 'apt-pkg/pkgcache.h')
-rw-r--r-- | apt-pkg/pkgcache.h | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/apt-pkg/pkgcache.h b/apt-pkg/pkgcache.h index e5a1a81eb..ef22a78a0 100644 --- a/apt-pkg/pkgcache.h +++ b/apt-pkg/pkgcache.h @@ -77,6 +77,7 @@ #include <apt-pkg/macros.h> #include <apt-pkg/mmap.h> +#include <cstddef> // required for nullptr_t #include <string> #include <stdint.h> #include <time.h> @@ -92,10 +93,29 @@ typedef uint32_t map_filesize_small_t; typedef uint32_t map_id_t; // some files get an id, too, but in far less absolute numbers typedef uint16_t map_fileid_t; + // relative pointer from cache start -template <typename T> using map_pointer = uint32_t; +template <typename T> class map_pointer { + uint32_t val; +public: + map_pointer() noexcept : val(0) {} + map_pointer(nullptr_t) noexcept : val(0) {} + explicit map_pointer(uint32_t n) noexcept : val(n) {} + explicit operator uint32_t() noexcept { return val; } +}; + +template<typename T> inline T *operator +(T *p, map_pointer<T> m) { return p + uint32_t(m); } +template<typename T> inline bool operator ==(map_pointer<T> u, map_pointer<T> m) { return uint32_t(u) == uint32_t(m); } +template<typename T> inline bool operator !=(map_pointer<T> u, map_pointer<T> m) { return uint32_t(u) != uint32_t(m); } +template<typename T> inline bool operator <(map_pointer<T> u, map_pointer<T> m) { return uint32_t(u) < uint32_t(m); } +template<typename T> inline bool operator >(map_pointer<T> u, map_pointer<T> m) { return uint32_t(u) > uint32_t(m); } +template<typename T> inline uint32_t operator -(map_pointer<T> u, map_pointer<T> m) { return uint32_t(u) - uint32_t(m); } +template<typename T> bool operator ==(map_pointer<T> m, nullptr_t) { return uint32_t(m) == 0; } +template<typename T> bool operator !=(map_pointer<T> m, nullptr_t) { return uint32_t(m) != 0; } + // same as the previous, but documented to be to a string item typedef map_pointer<char> map_stringitem_t; + // we have only a small amount of flags for each item typedef uint8_t map_flags_t; typedef uint8_t map_number_t; |