From fe55da9a5a83f9d89c7d6e1acfe5f82fc50c0895 Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 1 Oct 2021 19:01:44 +0100 Subject: [PATCH] Improved ergonomics for aligned_ptr pointer handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (This is mostly a test to see if overloading `operator&()` is ever something worth doing. Since this is a rather lax smart-pointer container and I can"t see any reason we"d need to pass around `aligned_ptr*` instances, it might be worth doing.) Fortune for cpprng's current commit: Blessing − 吉 --- include/mem.h | 6 ++++++ src/rng/crand.cpp | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/mem.h b/include/mem.h index 5e68557..5fcde1c 100644 --- a/include/mem.h +++ b/include/mem.h @@ -73,12 +73,18 @@ namespace mem inline const T * operator->() const { return get_ptr(); } inline T * operator->() { return get_ptr(); } + inline const T * operator&() const { return get_ptr(); } + inline T * operator&() { return get_ptr(); } + inline const T & operator*() const { return get(); } inline T & operator*() { return get(); } inline operator const T*() const { return get_ptr(); } inline operator T*() { return get_ptr(); } + inline operator const T&() const { return get(); } + inline operator T&() { return get(); } + inline aligned_ptr& operator=(aligned_ptr&& other) { if(ptr) aligned_ptr_util::dealloc(ptr); diff --git a/src/rng/crand.cpp b/src/rng/crand.cpp index c75b1bf..aca200b 100644 --- a/src/rng/crand.cpp +++ b/src/rng/crand.cpp @@ -26,8 +26,8 @@ namespace rng crand::crand(u64 seed) : crand(reinterpret_cast<_opaque*>(_jr_new(seed))){} crand::crand() : crand(0xabad1dea){} - i64 crand::_sample_int() { return _jr_proc(reinterpret_cast(_state.get_ptr())); } //RANGE_MIN..=RANGE_MAX - f64 crand::_sample() { return _jr_procf(reinterpret_cast(_state.get_ptr())); } //0..1 + i64 crand::_sample_int() { return _jr_proc(reinterpret_cast(&_state)); } //RANGE_MIN..=RANGE_MAX + f64 crand::_sample() { return _jr_procf(reinterpret_cast(&_state)); } //0..1 // Overrides // u64 crand::next_u64() { return std::bit_cast(std::abs(_sample_int()) & INT32_MAX); }