diff options
-rw-r--r-- | CHANGELOG.txt | 4 | ||||
-rw-r--r-- | test.cc | 34 | ||||
-rw-r--r-- | zp/include/zp/det/mem.ii | 14 | ||||
-rw-r--r-- | zp/include/zp/det/zp.ii | 73 | ||||
-rw-r--r-- | zp/include/zp/zp | 44 |
5 files changed, 125 insertions, 44 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt index a174207..ec351fb 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -27,6 +27,10 @@ * Rework opt and res; * Make opt and res constexpr (except for exp); +* res: Overload oky and err with const-reference parameters; +* Don't make res and opt constexpr; +* Define destructors for opt and res; + # 1.0.0 * Update API-BREAK file; @@ -215,14 +215,14 @@ template<typename ltyp,typename rtyp> static auto cmp(int unsigned & numerr,int ::std::cout << " " << ln << ". " << ltypnm << " (" << lval << ") " << mthstr << " " << rtypnm << " (" << rval << ")... "; if (!cmp(lval,rval)) { - ::std::cout << "\x1B[38;5;161merror\x1B[0m\n"; + ::std::cout << "\x1B[38;5;161merror\x1B[0m" << ::std::endl; //throw ::std::exception {}; ++numerr; return; } - ::std::cout <<"\x1B[38;5;77mokay\x1B[0m\n"; + ::std::cout <<"\x1B[38;5;77mokay\x1B[0m" << ::std::endl; } #define chkeq(lval,rval) (::cmp(numerr,__LINE__,0x0u,(lval),(rval))) @@ -279,6 +279,36 @@ int main() { chkeq(res.chk(),true); chkeq(res.exp(fun,"expectation failed"),-0x45); + + class nopod0 { + public: + char val; + + nopod0() noexcept : val('\x00') {} + nopod0(char const* const str) noexcept : val(str[0x0]) {} + + ~nopod0() noexcept(false) {} + }; + + class nopod1 { + public: + int val; + + nopod1() noexcept : val(0x0) {} + nopod1(int const val) noexcept : val(0x0-val) {} + + ~nopod1() noexcept(false) {} + }; + + auto resnopod = ::zp::res<nopod0,nopod1>::oky("Hallo"); + + chkeq(resnopod.chk(),true); + chkeq(resnopod.getoky().val,'H'); + + resnopod = ::zp::res<nopod0,nopod1>::err(0x4); + + chkeq(resnopod.chk(),false); + chkeq(resnopod.geterr().val,-0x4); }(); [&] { diff --git a/zp/include/zp/det/mem.ii b/zp/include/zp/det/mem.ii index 9a8fdb3..589d1f0 100644 --- a/zp/include/zp/det/mem.ii +++ b/zp/include/zp/det/mem.ii @@ -4,15 +4,22 @@ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. */ +#if zp_std_cxx11 template<typename typ,typename alctyp> zp::dev::arr<typ,alctyp>::arr() : _alc(alctyp()) {} -template<typename typ,typename alctyp> zp::dev::arr<typ,alctyp>::arr(::zp::dev::arr<typ,alctyp> & oth) : _alc(alctyp()),_buf(this->_alc.allocate(len)),_len(oth._len) { +/*template<typename typ,typename alctyp> zp::dev::arr<typ,alctyp>::arr(::zp::dev::arr<typ,alctyp> & oth) : _alc(alctyp()),_buf(this->_alc.allocate(len)),_len(oth._len) { + zp_unlik (this->_buf == zp_nulptr) {return;} // Allocation failed. ::zp::cpy(this->_buf,oth._buf,oth._len); -} +}*/ + +template<typename typ,typename alctyp> zp::dev::arr<typ,alctyp>::arr(::zp::siz const len) { + this->_alc = alctyp {}; -template<typename typ,typename alctyp> zp::dev::arr<typ,alctyp>::arr(::zp::siz const len) : _alc(alctyp()),_buf(this->_alc.allocate(len)),_len(len) {} + this->_buf = this->_alc.allocate(len); + this->_len = len; +} #if zp_std_cxx11 template<typename typ,typename alctyp> zp::dev::arr<typ,alctyp>::arr(::zp::dev::arr<typ,alctyp> && oth) : _alc(oth._alc),_buf(oth._buf),_len(oth._len) {oth._buf = zp_nulptr;} @@ -44,6 +51,7 @@ template<typename typ,typename alctyp> typ const* zp::dev::arr<typ,alctyp>::end( template<typename typ,typename alctyp> typ & zp::dev::arr<typ,alctyp>::operator [](::zp::ptrdif const off) {return this->_buf[off];} template<typename typ,typename alctyp> typ const& zp::dev::arr<typ,alctyp>::operator [](::zp::ptrdif const off) const {return this->_buf[off];} +#endif template<typename dsttyp,typename srctyp> zp_nothw inline ::zp::cpyres<dsttyp,srctyp> zp::memcpy(dsttyp * dst,srctyp const* src,::zp::siz const num) { ::zp_cpyres const cpyres = ::zp_memcpy(dst,src,num); diff --git a/zp/include/zp/det/zp.ii b/zp/include/zp/det/zp.ii index 57643c1..3d764bc 100644 --- a/zp/include/zp/det/zp.ii +++ b/zp/include/zp/det/zp.ii @@ -18,23 +18,25 @@ constexpr auto ::zp::iscstevl() noexcept -> bool { #endif #if zp_std_cxx11 -template<typename typ> constexpr zp::opt<typ>::opt(::zp::det::nulopttyp) noexcept {this->_hasval = false;} +template<typename typ> ::zp::opt<typ>::opt(::zp::det::nulopttyp) noexcept {this->_hasval = false;} -template<typename typ> constexpr zp::opt<typ>::opt(typ const& val) { +template<typename typ> ::zp::opt<typ>::opt(typ const& val) { this->_hasval = true; this->_val = val; } -template<typename typ> constexpr zp::opt<typ>::opt(typ && val) { +template<typename typ> ::zp::opt<typ>::opt(typ && val) { this->_hasval = true; this->_val = val; } -template<typename typ> constexpr auto zp::opt<typ>::chk() const noexcept -> bool {return this->_hasval;} +template<typename typ> ::zp::opt<typ>::~opt() noexcept(false) {} -template<typename typ> constexpr auto zp::opt<typ>::val() const noexcept -> typ const& {return this->_val;} +template<typename typ> auto ::zp::opt<typ>::chk() const noexcept -> bool {return this->_hasval;} -template<typename typ> template<typename funtyp,typename msgtyp> auto zp::opt<typ>::exp(funtyp const& fun,msgtyp const& msg) const noexcept -> typ const& { +template<typename typ> auto ::zp::opt<typ>::val() const noexcept -> typ const& {return this->_val;} + +template<typename typ> template<typename funtyp,typename msgtyp> auto ::zp::opt<typ>::exp(funtyp const& fun,msgtyp const& msg) const noexcept -> typ const& { zp_unlik (!this->_hasval) { fun(msg); ::zp::unrch(); @@ -43,20 +45,20 @@ template<typename typ> template<typename funtyp,typename msgtyp> auto zp::opt<ty return this->_val; } -template<typename typ> constexpr auto zp::opt<typ>::operator =(::zp::det::nulopttyp) noexcept -> ::zp::opt<typ> { +template<typename typ> auto ::zp::opt<typ>::operator =(::zp::det::nulopttyp) noexcept -> ::zp::opt<typ> { this->_hasval = false; return *this; } -template<typename typ> constexpr auto zp::opt<typ>::operator =(typ const& val) -> ::zp::opt<typ> { +template<typename typ> auto ::zp::opt<typ>::operator =(typ const& val) -> ::zp::opt<typ> { this->_hasval = true; this->_val = val; return *this; } -template<typename typ> constexpr auto zp::opt<typ>::operator =(typ && val) -> ::zp::opt<typ> { +template<typename typ> auto ::zp::opt<typ>::operator =(typ && val) -> ::zp::opt<typ> { this->_hasval = true; this->_val = val; @@ -65,37 +67,64 @@ template<typename typ> constexpr auto zp::opt<typ>::operator =(typ && val) -> :: #endif #if zp_std_cxx11 -template<typename okytyp,typename errtyp> constexpr zp::res<okytyp,errtyp>::res(::zp::res<okytyp,errtyp> const& oth) { +template<typename okytyp,typename errtyp> zp::res<okytyp,errtyp>::res(::zp::res<okytyp,errtyp> const& oth) { this->_hasoky = oth._hasoky; zp_lik (oth._hasoky) {this->_okyval = oth._okyval;} else {this->_errval = oth._errval;} } -template<typename okytyp,typename errtyp> constexpr zp::res<okytyp,errtyp>::res(::zp::res<okytyp,errtyp> && oth) { +template<typename okytyp,typename errtyp> zp::res<okytyp,errtyp>::res(::zp::res<okytyp,errtyp> && oth) { this->_hasoky = oth._hasoky; zp_lik (oth._hasoky) {this->_okyval = oth._okyval;} else {this->_errval = oth._errval;} } -template<typename okytyp,typename errtyp> constexpr auto zp::res<okytyp,errtyp>::oky(okytyp && oky) -> ::zp::res<okytyp,errtyp> { +template<typename okytyp,typename errtyp> zp::res<okytyp,errtyp>::~res() noexcept(false) { + zp_lik (this->_hasoky) {this->_okyval.~okytyp();} + else {this->_errval.~errtyp();} +} + +template<typename okytyp,typename errtyp> auto ::zp::res<okytyp,errtyp>::oky(okytyp const& oky) -> ::zp::res<okytyp,errtyp> { ::zp::res<okytyp,errtyp> res; - res._hasoky = true; + + res._hasoky = true; res._okyval = oky; + return res; } -template<typename okytyp,typename errtyp> constexpr auto zp::res<okytyp,errtyp>::err(errtyp && err) -> ::zp::res<okytyp,errtyp> { +template<typename okytyp,typename errtyp> auto ::zp::res<okytyp,errtyp>::oky(okytyp && oky) -> ::zp::res<okytyp,errtyp> { ::zp::res<okytyp,errtyp> res; - res._hasoky = false; + + res._hasoky = true; + res._okyval = oky; + + return res; +} + +template<typename okytyp,typename errtyp> auto ::zp::res<okytyp,errtyp>::err(errtyp const& err) -> ::zp::res<okytyp,errtyp> { + ::zp::res<okytyp,errtyp> res; + + res._hasoky = false; + res._errval = err; + + return res; +} + +template<typename okytyp,typename errtyp> auto ::zp::res<okytyp,errtyp>::err(errtyp && err) -> ::zp::res<okytyp,errtyp> { + ::zp::res<okytyp,errtyp> res; + + res._hasoky = false; res._errval = err; + return res; } -template<typename okytyp,typename errtyp> constexpr auto zp::res<okytyp,errtyp>::getoky() const noexcept -> okytyp const& {return this->_okyval;} +template<typename okytyp,typename errtyp> auto ::zp::res<okytyp,errtyp>::getoky() const noexcept -> okytyp const& {return this->_okyval;} -template<typename okytyp,typename errtyp> constexpr auto zp::res<okytyp,errtyp>::geterr() const noexcept -> errtyp const& {return this->_errval;} +template<typename okytyp,typename errtyp> auto ::zp::res<okytyp,errtyp>::geterr() const noexcept -> errtyp const& {return this->_errval;} -template<typename okytyp,typename errtyp> template<typename funtyp,typename msgtyp> auto zp::res<okytyp,errtyp>::exp(funtyp const& fun,msgtyp const& msg) const noexcept -> okytyp const& { +template<typename okytyp,typename errtyp> template<typename funtyp,typename msgtyp> auto ::zp::res<okytyp,errtyp>::exp(funtyp const& fun,msgtyp const& msg) const noexcept -> okytyp const& { zp_unlik (!this->_hasoky) { fun(msg); ::zp::unrch(); @@ -104,7 +133,9 @@ template<typename okytyp,typename errtyp> template<typename funtyp,typename msgt return this->_okyval; } -template<typename okytyp,typename errtyp> constexpr auto zp::res<okytyp,errtyp>::operator =(::zp::res<okytyp,errtyp> const& oth) -> ::zp::res<okytyp,errtyp> { +template<typename okytyp,typename errtyp> auto ::zp::res<okytyp,errtyp>::operator =(::zp::res<okytyp,errtyp> const& oth) -> ::zp::res<okytyp,errtyp> { + this->~res(); + this->_hasoky = oth._hasoky; zp_lik (oth._hasoky) {this->_okyval = oth._okyval;} else {this->_errval = oth._errval;} @@ -112,7 +143,9 @@ template<typename okytyp,typename errtyp> constexpr auto zp::res<okytyp,errtyp>: return *this; } -template<typename okytyp,typename errtyp> constexpr auto zp::res<okytyp,errtyp>::operator =(::zp::res<okytyp,errtyp> && oth) -> ::zp::res<okytyp,errtyp> { +template<typename okytyp,typename errtyp> auto ::zp::res<okytyp,errtyp>::operator =(::zp::res<okytyp,errtyp> && oth) -> ::zp::res<okytyp,errtyp> { + this->~res(); + this->_hasoky = oth._hasoky; zp_lik (oth._hasoky) {this->_okyval = oth._okyval;} else {this->_errval = oth._errval;} diff --git a/zp/include/zp/zp b/zp/include/zp/zp index 9b654f9..ef3dc93 100644 --- a/zp/include/zp/zp +++ b/zp/include/zp/zp @@ -314,23 +314,25 @@ namespace zp { struct nulopttyp {}; } - zp_prv_cstval (::zp::det::nulopttyp) nulopt; + constexpr ::zp::det::nulopttyp nulopt = {}; template<typename typ> class opt { public: - constexpr opt(::zp::det::nulopttyp) noexcept; - constexpr opt(typ const& val); - constexpr opt(typ && val); + opt(::zp::det::nulopttyp) noexcept; + opt(typ const& val); + opt(typ && val); - constexpr auto chk() const noexcept -> bool; + ~opt() noexcept(false); - constexpr auto val() const noexcept -> typ const&; + auto chk() const noexcept -> bool; + + auto val() const noexcept -> typ const&; template<typename funtyp,typename msgtyp> auto exp(funtyp const & fun,msgtyp const & msg) const noexcept -> typ const&; - constexpr auto operator =(::zp::det::nulopttyp) noexcept -> ::zp::opt<typ>; - constexpr auto operator =(typ const& val) -> ::zp::opt<typ>; - constexpr auto operator =(typ && val) -> ::zp::opt<typ>; + auto operator =(::zp::det::nulopttyp) noexcept -> ::zp::opt<typ>; + auto operator =(typ const& val) -> ::zp::opt<typ>; + auto operator =(typ && val) -> ::zp::opt<typ>; private: bool _hasval; @@ -341,24 +343,28 @@ namespace zp { #if zp_std_cxx11 template<typename okytyp,typename errtyp> class res { public: - constexpr res(::zp::res<okytyp,errtyp> const& oth); - constexpr res(::zp::res<okytyp,errtyp> && oth); + res(::zp::res<okytyp,errtyp> const& oth); + res(::zp::res<okytyp,errtyp> && oth); + + ~res() noexcept(false); - constexpr static auto oky(okytyp && oky) -> ::zp::res<okytyp,errtyp>; - constexpr static auto err(errtyp && err) -> ::zp::res<okytyp,errtyp>; + static auto oky(okytyp const& oky) -> ::zp::res<okytyp,errtyp>; + static auto oky(okytyp && oky) -> ::zp::res<okytyp,errtyp>; + static auto err(errtyp const& err) -> ::zp::res<okytyp,errtyp>; + static auto err(errtyp && err) -> ::zp::res<okytyp,errtyp>; - constexpr auto chk() const noexcept -> bool {return this->_hasoky;} + auto chk() const noexcept -> bool {return this->_hasoky;} - constexpr auto getoky() const noexcept -> okytyp const&; - constexpr auto geterr() const noexcept -> errtyp const&; + auto getoky() const noexcept -> okytyp const&; + auto geterr() const noexcept -> errtyp const&; template<typename funtyp,typename msgtyp> auto exp(funtyp const& fun,msgtyp const& msg) const noexcept -> okytyp const&; // UB if fun returns or throws an uncaught exception. - constexpr auto operator =(::zp::res<okytyp,errtyp> const& oth) -> ::zp::res<okytyp,errtyp>; - constexpr auto operator =(::zp::res<okytyp,errtyp> && oth) -> ::zp::res<okytyp,errtyp>; + auto operator =(::zp::res<okytyp,errtyp> const& oth) -> ::zp::res<okytyp,errtyp>; + auto operator =(::zp::res<okytyp,errtyp> && oth) -> ::zp::res<okytyp,errtyp>; private: - constexpr res() noexcept = default; + constexpr res() noexcept {}; bool _hasoky : 0x1; union { |