diff options
author | David Shah <dave@ds0.me> | 2021-01-28 12:53:03 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-28 12:53:03 +0000 |
commit | b671d8f59d425b6faf07167b7360cecd12366151 (patch) | |
tree | 552ddbb947435eb5e3c44940a16d3733697766b2 /common | |
parent | a95d6678b1d353a1606e377e1833dec54ad4ee65 (diff) | |
parent | b87ab0ee9de093b5302de7997e94e10dae6655b4 (diff) | |
download | nextpnr-b671d8f59d425b6faf07167b7360cecd12366151.tar.gz nextpnr-b671d8f59d425b6faf07167b7360cecd12366151.tar.bz2 nextpnr-b671d8f59d425b6faf07167b7360cecd12366151.zip |
Merge pull request #553 from YosysHQ/rel-slice
Switch from RelPtr to RelSlice
Diffstat (limited to 'common')
-rw-r--r-- | common/relptr.h | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/common/relptr.h b/common/relptr.h new file mode 100644 index 00000000..035d61fb --- /dev/null +++ b/common/relptr.h @@ -0,0 +1,42 @@ +// This is intended to be included inside arch.h only. + +template <typename T> struct RelPtr +{ + int32_t offset; + + const T *get() const { return reinterpret_cast<const T *>(reinterpret_cast<const char *>(this) + offset); } + + const T &operator[](size_t index) const { return get()[index]; } + + const T &operator*() const { return *(get()); } + + const T *operator->() const { return get(); } + + RelPtr(const RelPtr &) = delete; + RelPtr &operator=(const RelPtr &) = delete; +}; + +NPNR_PACKED_STRUCT(template <typename T> struct RelSlice { + int32_t offset; + uint32_t length; + + const T *get() const { return reinterpret_cast<const T *>(reinterpret_cast<const char *>(this) + offset); } + + const T &operator[](size_t index) const + { + NPNR_ASSERT(index < length); + return get()[index]; + } + + const T *begin() const { return get(); } + const T *end() const { return get() + length; } + + const size_t size() const { return length; } + + const T &operator*() const { return *(get()); } + + const T *operator->() const { return get(); } + + RelSlice(const RelSlice &) = delete; + RelSlice &operator=(const RelSlice &) = delete; +}); |