diff options
-rw-r--r-- | CHANGELOG.txt | 7 | ||||
-rw-r--r-- | test.cc | 2 | ||||
-rw-r--r-- | zp/include/zp/det/mem.ii | 30 | ||||
-rw-r--r-- | zp/include/zp/det/zp.ii | 90 | ||||
-rw-r--r-- | zp/include/zp/mem | 52 | ||||
-rw-r--r-- | zp/include/zp/zp | 48 |
6 files changed, 160 insertions, 69 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 32bdabe..a174207 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -20,6 +20,13 @@ * Add equivalent to C++ move: fwd; * Inline some opt and res methods; +* Add namespace for experimental features: dev; +* Move arr to this namespace; +* Don't define methods in class definitions; +* Require C++11 for opt, res, and arr; +* Rework opt and res; +* Make opt and res constexpr (except for exp); + # 1.0.0 * Update API-BREAK file; @@ -469,7 +469,7 @@ int main() { [&] { tst("arrays"); - ::zp::arr<::zp::i8,::std::allocator<::zp::i8>> arr(0x8u); + ::zp::dev::arr<::zp::i8,::std::allocator<::zp::i8>> arr(0x8u); chkeq(arr.len(),0x8u); diff --git a/zp/include/zp/det/mem.ii b/zp/include/zp/det/mem.ii index 02aca0e..9a8fdb3 100644 --- a/zp/include/zp/det/mem.ii +++ b/zp/include/zp/det/mem.ii @@ -4,30 +4,30 @@ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>. */ -template<typename typ,typename alctyp> zp::arr<typ,alctyp>::arr() : _alc(alctyp()) {} +template<typename typ,typename alctyp> zp::dev::arr<typ,alctyp>::arr() : _alc(alctyp()) {} -template<typename typ,typename alctyp> zp::arr<typ,alctyp>::arr(::zp::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::arr<typ,alctyp>::arr(::zp::siz const len) : _alc(alctyp()),_buf(this->_alc.allocate(len)),_len(len) {} +template<typename typ,typename alctyp> zp::dev::arr<typ,alctyp>::arr(::zp::siz const len) : _alc(alctyp()),_buf(this->_alc.allocate(len)),_len(len) {} #if zp_std_cxx11 -template<typename typ,typename alctyp> zp::arr<typ,alctyp>::arr(::zp::arr<typ,alctyp> && oth) : _alc(oth._alc),_buf(oth._buf),_len(oth._len) {oth._buf = zp_nulptr;} +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;} #endif -template<typename typ,typename alctyp> zp_nothw zp::arr<typ,alctyp>::~arr() { +template<typename typ,typename alctyp> zp_nothw zp::dev::arr<typ,alctyp>::~arr() { zp_unlik (this->_buf == zp_nulptr) {return;} this->_alc.deallocate(this->_buf,this->_len); } -template<typename typ,typename alctyp> zp_nothw bool zp::arr<typ,alctyp>::oky() const {return this->_buf == zp_nulptr;} +template<typename typ,typename alctyp> zp_nothw bool zp::dev::arr<typ,alctyp>::oky() const {return this->_buf == zp_nulptr;} -template<typename typ,typename alctyp> zp_nothw ::zp::siz zp::arr<typ,alctyp>::len() const {return this->_len;} +template<typename typ,typename alctyp> zp_nothw ::zp::siz zp::dev::arr<typ,alctyp>::len() const {return this->_len;} -template<typename typ,typename alctyp> bool zp::arr<typ,alctyp>::alc(::zp::siz const len) { +template<typename typ,typename alctyp> bool zp::dev::arr<typ,alctyp>::alc(::zp::siz const len) { this->_len = len; this->_buf = this->_alc(len); @@ -35,15 +35,15 @@ template<typename typ,typename alctyp> bool zp::arr<typ,alctyp>::alc(::zp::siz c return false; } -template<typename typ,typename alctyp> template<typename valtyp> ::zp::siz zp::arr<typ,alctyp>::fil(valtyp const& val) {return static_cast<::zp::siz>(::zp::fil(this->_buf,val,this->_len)-this->_buf);} +template<typename typ,typename alctyp> template<typename valtyp> ::zp::siz zp::dev::arr<typ,alctyp>::fil(valtyp const& val) {return static_cast<::zp::siz>(::zp::fil(this->_buf,val,this->_len)-this->_buf);} -template<typename typ,typename alctyp> typ * zp::arr<typ,alctyp>::begin() {return this->_buf;} -template<typename typ,typename alctyp> typ const* zp::arr<typ,alctyp>::begin() const {return this->_buf;} -template<typename typ,typename alctyp> typ * zp::arr<typ,alctyp>::end() {return this->_buf+this->_len;} -template<typename typ,typename alctyp> typ const* zp::arr<typ,alctyp>::end() const {return this->_buf+this->_len;} +template<typename typ,typename alctyp> typ * zp::dev::arr<typ,alctyp>::begin() {return this->_buf;} +template<typename typ,typename alctyp> typ const* zp::dev::arr<typ,alctyp>::begin() const {return this->_buf;} +template<typename typ,typename alctyp> typ * zp::dev::arr<typ,alctyp>::end() {return this->_buf+this->_len;} +template<typename typ,typename alctyp> typ const* zp::dev::arr<typ,alctyp>::end() const {return this->_buf+this->_len;} -template<typename typ,typename alctyp> typ & zp::arr<typ,alctyp>::operator [](::zp::ptrdif const off) {return this->_buf[off];} -template<typename typ,typename alctyp> typ const& zp::arr<typ,alctyp>::operator [](::zp::ptrdif const off) const {return this->_buf[off];} +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];} 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 2a40cc3..57643c1 100644 --- a/zp/include/zp/det/zp.ii +++ b/zp/include/zp/det/zp.ii @@ -17,7 +17,24 @@ constexpr auto ::zp::iscstevl() noexcept -> bool { } #endif -template<typename typ> template<typename funtyp,typename msgtyp> zp_nothw typ const& zp::opt<typ>::exp(funtyp const& fun,msgtyp const& msg) const { +#if zp_std_cxx11 +template<typename typ> constexpr zp::opt<typ>::opt(::zp::det::nulopttyp) noexcept {this->_hasval = false;} + +template<typename typ> constexpr zp::opt<typ>::opt(typ const& val) { + this->_hasval = true; + this->_val = val; +} + +template<typename typ> constexpr 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> constexpr 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(); @@ -26,25 +43,80 @@ template<typename typ> template<typename funtyp,typename msgtyp> zp_nothw typ co return this->_val; } -template<typename okytyp,typename errtyp> ::zp::res<okytyp,errtyp> zp::res<okytyp,errtyp>::err(errtyp const& err) { +template<typename typ> constexpr 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> { + this->_hasval = true; + this->_val = val; + + return *this; +} + +template<typename typ> constexpr auto zp::opt<typ>::operator =(typ && val) -> ::zp::opt<typ> { + this->_hasval = true; + this->_val = val; + + return *this; +} +#endif + +#if zp_std_cxx11 +template<typename okytyp,typename errtyp> constexpr 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) { + 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> { ::zp::res<okytyp,errtyp> res; - res._hasoky = false; - res._val.err = err; + res._hasoky = true; + res._okyval = oky; return res; } -template<typename okytyp,typename errtyp> ::zp::res<okytyp,errtyp> zp::res<okytyp,errtyp>::oky(okytyp const& oky) { +template<typename okytyp,typename errtyp> constexpr auto zp::res<okytyp,errtyp>::err(errtyp && err) -> ::zp::res<okytyp,errtyp> { ::zp::res<okytyp,errtyp> res; - res._hasoky = true; - res._val.oky = oky; + res._hasoky = false; + res._errval = err; return res; } -template<typename okytyp,typename errtyp> template<typename funtyp,typename msgtyp> okytyp const& zp::res<okytyp,errtyp>::exp(funtyp const& fun,msgtyp const& msg) const { +template<typename okytyp,typename errtyp> constexpr 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> 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(); } - return this->_val.oky; + 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> { + this->_hasoky = oth._hasoky; + zp_lik (oth._hasoky) {this->_okyval = oth._okyval;} + else {this->_errval = oth._errval;} + + return *this; } + +template<typename okytyp,typename errtyp> constexpr auto zp::res<okytyp,errtyp>::operator =(::zp::res<okytyp,errtyp> && oth) -> ::zp::res<okytyp,errtyp> { + this->_hasoky = oth._hasoky; + zp_lik (oth._hasoky) {this->_okyval = oth._okyval;} + else {this->_errval = oth._errval;} + + return *this; +} +#endif diff --git a/zp/include/zp/mem b/zp/include/zp/mem index d486773..5475152 100644 --- a/zp/include/zp/mem +++ b/zp/include/zp/mem @@ -16,40 +16,44 @@ namespace zp { srctyp * src; }; - template<typename typ,typename alctyp> class arr { - public: - zp_nothw arr(); - arr(::zp::arr<typ,alctyp> & oth); - explicit arr(::zp::siz len); #if zp_std_cxx11 - zp_nothw arr(::zp::arr<typ,alctyp> && oth); + namespace dev { + template<typename typ,typename alctyp> class arr { + public: + zp_nothw arr(); + arr(::zp::dev::arr<typ,alctyp> & oth); + explicit arr(::zp::siz len); +#if zp_std_cxx11 + zp_nothw arr(::zp::dev::arr<typ,alctyp> && oth); #endif - zp_nothw ~arr(); + zp_nothw ~arr(); - zp_nothw bool oky() const; + zp_nothw bool oky() const; - zp_nothw typ * ptr(); - zp_nothw typ const* ptr() const; - zp_nothw ::zp::siz len() const; + zp_nothw typ * ptr(); + zp_nothw typ const* ptr() const; + zp_nothw ::zp::siz len() const; - bool alc(::zp::siz len); + bool alc(::zp::siz len); - template<typename valtyp> ::zp::siz fil(valtyp const& val); + template<typename valtyp> ::zp::siz fil(valtyp const& val); - zp_nothw typ * begin(); - zp_nothw typ const* begin() const; - zp_nothw typ * end(); - zp_nothw typ const* end() const; + zp_nothw typ * begin(); + zp_nothw typ const* begin() const; + zp_nothw typ * end(); + zp_nothw typ const* end() const; - zp_nothw typ & operator [](::zp::ptrdif off); - zp_nothw typ const& operator [](::zp::ptrdif off) const; + zp_nothw typ & operator [](::zp::ptrdif off); + zp_nothw typ const& operator [](::zp::ptrdif off) const; - private: - alctyp _alc; - typ * _buf; - ::zp::siz _len; - }; + private: + alctyp _alc; + typ * _buf; + ::zp::siz _len; + }; + } +#endif template<typename dsttyp,typename srctyp> zp_iln zp_nothw inline ::zp::cpyres<dsttyp,srctyp> memcpy(dsttyp * zp_prv_rsr dst, srctyp const* zp_prv_rsr src, ::zp::siz const num); // Implement in seperate file. template<typename ltyp, typename rtyp> zp_useres zp_iln zp_nothw inline bool memequ(ltyp const* zp_prv_rsr lbuf,rtyp const* zp_prv_rsr rbuf,::zp::siz const num) {return ::zp_memequ(lbuf,rbuf,num);} diff --git a/zp/include/zp/zp b/zp/include/zp/zp index 468ea82..9b654f9 100644 --- a/zp/include/zp/zp +++ b/zp/include/zp/zp @@ -309,6 +309,7 @@ namespace zp { template<typename typ> concept inttyp = ::zp::isinttyp<typ>::val; #endif +#if zp_std_cxx11 namespace det { struct nulopttyp {}; } @@ -317,48 +318,55 @@ namespace zp { template<typename typ> class opt { public: - zp_iln zp_nothw inline opt(::zp::det::nulopttyp) {this->_hasval = false;} - zp_iln inline opt(typ const& val) {this->_hasval = true; this->_val = val;} + constexpr opt(::zp::det::nulopttyp) noexcept; + constexpr opt(typ const& val); + constexpr opt(typ && val); - zp_iln zp_nothw inline bool chk() const {return this->_hasval;} + constexpr auto chk() const noexcept -> bool; - zp_iln zp_nothw inline typ const& val() const {return this->_val;} + constexpr auto val() const noexcept -> typ const&; - template<typename funtyp,typename msgtyp> zp_nothw typ const& exp(funtyp const & fun,msgtyp const & msg) const; + template<typename funtyp,typename msgtyp> auto exp(funtyp const & fun,msgtyp const & msg) const noexcept -> typ const&; - zp_iln inline ::zp::opt<typ> operator =(typ const& val) {this->_hasval = true;this->_val = val;return *this;} + 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>; private: bool _hasval; typ _val; }; +#endif +#if zp_std_cxx11 template<typename okytyp,typename errtyp> class res { public: - zp_iln zp_nothw inline res() {} - zp_iln zp_nothw inline res(::zp::res<okytyp,errtyp> const& oth) {this->_hasoky = oth._hasoky;this->_val = oth._val;} + constexpr res(::zp::res<okytyp,errtyp> const& oth); + constexpr res(::zp::res<okytyp,errtyp> && oth); - zp_iln zp_nothw inline static ::zp::res<okytyp,errtyp> oky(okytyp const& oky); - zp_iln zp_nothw inline static ::zp::res<okytyp,errtyp> err(errtyp const& err); + constexpr static auto oky(okytyp && oky) -> ::zp::res<okytyp,errtyp>; + constexpr static auto err(errtyp && err) -> ::zp::res<okytyp,errtyp>; - zp_iln zp_nothw inline bool chk() const {return this->_hasoky;} + constexpr auto chk() const noexcept -> bool {return this->_hasoky;} - zp_iln zp_nothw inline okytyp & getoky() {return this->_val.oky;} - zp_iln zp_nothw inline okytyp const& getoky() const {return this->_val.oky;} - zp_iln zp_nothw inline errtyp & geterr() {return this->_val.err;} - zp_iln zp_nothw inline errtyp const& geterr() const {return this->_val.err;} + constexpr auto getoky() const noexcept -> okytyp const&; + constexpr auto geterr() const noexcept -> errtyp const&; - template<typename funtyp,typename msgtyp> zp_nothw okytyp const& exp(funtyp const& fun,msgtyp const& msg) const; // UB if fun doesn't return. + 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. - zp_nothw ::zp::res<okytyp,errtyp> operator =(::zp::res<okytyp,errtyp> const& oth) {this->_hasoky = oth._hasoky;this->_val = oth._val;return *this;} + 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>; private: + constexpr res() noexcept = default; + bool _hasoky : 0x1; union { - okytyp oky; - errtyp err; - } _val; + okytyp _okyval; + errtyp _errval; + }; }; +#endif namespace det { template<typename typ> struct vertyp { |