diff --git a/libs/main/containers/pideque.h b/libs/main/containers/pideque.h index dd62b501..82d00b45 100644 --- a/libs/main/containers/pideque.h +++ b/libs/main/containers/pideque.h @@ -209,7 +209,22 @@ public: inline T * data(size_t index = 0) {return &(pid_data[pid_start + index]);} inline const T * data(size_t index = 0) const {return &(pid_data[pid_start + index]);} - inline PIDeque & clear() {resize(0); return *this;} + template::value + , int>::type = 0> + inline PIDeque & clear() { + resize(0); + return *this; + } + template::value + , int>::type = 0> + inline PIDeque & clear() { + PIINTROSPECTION_CONTAINER_UNUSED(T, pid_size) + pid_size = 0; + return *this; + } + inline PIDeque & fill(const T & f = T()) { deleteT(pid_data + pid_start, pid_size); PIINTROSPECTION_CONTAINER_USED(T, pid_size) @@ -218,10 +233,20 @@ public: return *this; } inline PIDeque & assign(const T & f = T()) {return fill(f);} + template::value + , int>::type = 0> inline PIDeque & assign(size_t new_size, const T & f) { resize(new_size); return fill(f); } + template::value + , int>::type = 0> + inline PIDeque & assign(size_t new_size, const T & f) { + _resizeRaw(new_size); + return fill(f); + } inline PIDeque & resize(size_t new_size, const T & f = T()) { if (new_size < pid_size) { @@ -236,9 +261,18 @@ public: } return *this; } + + template::value + , int>::type = 0> inline PIDeque & _resizeRaw(size_t new_size) { - piCout << "Error, \"resizeRaw()\" only allowed for simple type declared with __PIDEQUE_SIMPLE_TYPE__ macro!"; - assert(0); + if (new_size > pid_size) { + PIINTROSPECTION_CONTAINER_USED(T, (new_size-pid_size)); + } + if (new_size < pid_size) { + PIINTROSPECTION_CONTAINER_UNUSED(T, (pid_size-new_size)); + } + alloc(new_size, true); return *this; } @@ -437,11 +471,24 @@ private: ++t; return (1 << t); } + template::value + , int>::type = 0> inline void newT(T * dst, const T * src, size_t s) { PIINTROSPECTION_CONTAINER_USED(T, s) for (size_t i = 0; i < s; ++i) elementNew(dst + i, src[i]); } + template::value + , int>::type = 0> + inline void newT(T * dst, const T * src, size_t s) { + PIINTROSPECTION_CONTAINER_USED(T, s) + memcpy((void*)(dst), (const void*)(src), s * sizeof(T)); + } + template::value + , int>::type = 0> inline void deleteT(T * d, size_t sz) { PIINTROSPECTION_CONTAINER_UNUSED(T, sz) if ((uchar*)d != 0) { @@ -449,9 +496,36 @@ private: elementDelete(d[i]); } } + template::value + , int>::type = 0> + inline void deleteT(T * d, size_t sz) { + PIINTROSPECTION_CONTAINER_UNUSED(T, sz) + } + template::value + , int>::type = 0> inline void elementNew(T * to, const T & from) {new(to)T(from);} + template::value + , int>::type = 0> inline void elementNew(T * to, T && from) {new(to)T(std::move(from));} + template::value + , int>::type = 0> + inline void elementNew(T1 * to, const T & from) {(*to) = from;} + template::value + , int>::type = 0> + inline void elementNew(T * to, T && from) {(*to) = std::move(from);} + template::value + , int>::type = 0> inline void elementDelete(T & from) {from.~T();} + template::value + , int>::type = 0> + inline void elementDelete(T & from) {} inline void dealloc() { if ((uchar*)pid_data != 0) free((uchar*)pid_data); pid_data = 0; @@ -520,41 +594,6 @@ private: ssize_t pid_start; }; -#define __PIDEQUE_SIMPLE_TYPE__(T) \ - template<> inline void PIDeque::newT(T * dst, const T * src, size_t s) {PIINTROSPECTION_CONTAINER_USED(T, s); memcpy((void*)(dst), (const void*)(src), s * sizeof(T));} \ - template<> inline void PIDeque::deleteT(T *, size_t sz) {PIINTROSPECTION_CONTAINER_UNUSED(T, sz);} \ - template<> inline void PIDeque::elementNew(T * to, const T & from) {(*to) = from;} \ - template<> inline void PIDeque::elementNew(T * to, T && from) {(*to) = std::move(from);} \ - template<> inline void PIDeque::elementDelete(T &) {;} \ - template<> inline PIDeque & PIDeque::_resizeRaw(size_t new_size) { \ - if (new_size > pid_size) { \ - PIINTROSPECTION_CONTAINER_USED(T, (new_size-pid_size)); \ - } \ - if (new_size < pid_size) { \ - PIINTROSPECTION_CONTAINER_UNUSED(T, (pid_size-new_size)); \ - } \ - alloc(new_size, true); \ - return *this; \ - } \ - template<> inline PIDeque & PIDeque::clear() {PIINTROSPECTION_CONTAINER_UNUSED(T, pid_size); pid_size = 0; return *this;} \ - template<> inline PIDeque & PIDeque::assign(size_t new_size, const T & f) {_resizeRaw(new_size); return fill(f);} - - -__PIDEQUE_SIMPLE_TYPE__(bool) -__PIDEQUE_SIMPLE_TYPE__(char) -__PIDEQUE_SIMPLE_TYPE__(uchar) -__PIDEQUE_SIMPLE_TYPE__(short) -__PIDEQUE_SIMPLE_TYPE__(ushort) -__PIDEQUE_SIMPLE_TYPE__(int) -__PIDEQUE_SIMPLE_TYPE__(uint) -__PIDEQUE_SIMPLE_TYPE__(long) -__PIDEQUE_SIMPLE_TYPE__(ulong) -__PIDEQUE_SIMPLE_TYPE__(llong) -__PIDEQUE_SIMPLE_TYPE__(ullong) -__PIDEQUE_SIMPLE_TYPE__(float) -__PIDEQUE_SIMPLE_TYPE__(double) -__PIDEQUE_SIMPLE_TYPE__(ldouble) - #ifdef PIP_STD_IOSTREAM template diff --git a/libs/main/containers/pimap.h b/libs/main/containers/pimap.h index 38339dd7..ab4c6007 100644 --- a/libs/main/containers/pimap.h +++ b/libs/main/containers/pimap.h @@ -28,9 +28,6 @@ #include "pivector.h" #include "pideque.h" #include "pipair.h" -# define __PICONTAINERS_SIMPLE_TYPE__(T) \ -__PIDEQUE_SIMPLE_TYPE__(T)\ -__PIVECTOR_SIMPLE_TYPE__(T) template diff --git a/libs/main/containers/pivector.h b/libs/main/containers/pivector.h index 73f4d572..e7a3bd48 100644 --- a/libs/main/containers/pivector.h +++ b/libs/main/containers/pivector.h @@ -210,7 +210,22 @@ public: inline T * data(size_t index = 0) {return &(piv_data[index]);} inline const T * data(size_t index = 0) const {return &(piv_data[index]);} - inline PIVector & clear() {resize(0); return *this;} + template::value + , int>::type = 0> + inline PIVector & clear() { + resize(0); + return *this; + } + template::value + , int>::type = 0> + inline PIVector & clear() { + PIINTROSPECTION_CONTAINER_UNUSED(T, piv_size) + piv_size = 0; + return *this; + } + inline PIVector & fill(const T & f = T()) { deleteT(piv_data, piv_size); PIINTROSPECTION_CONTAINER_USED(T, piv_size) @@ -219,10 +234,20 @@ public: return *this; } inline PIVector & assign(const T & f = T()) {return fill(f);} + template::value + , int>::type = 0> inline PIVector & assign(size_t new_size, const T & f) { resize(new_size); return fill(f); } + template::value + , int>::type = 0> + inline PIVector & assign(size_t new_size, const T & f) { + _resizeRaw(new_size); + return fill(f); + } inline PIVector & resize(size_t new_size, const T & f = T()) { if (new_size < piv_size) { @@ -239,9 +264,17 @@ public: } return *this; } + template::value + , int>::type = 0> inline PIVector & _resizeRaw(size_t new_size) { - piCout << "Error, \"resizeRaw()\" only allowed for simple type declared with __PIVECTOR_SIMPLE_TYPE__ macro!"; - assert(0); + if (new_size > piv_size) { + PIINTROSPECTION_CONTAINER_USED(T, (new_size-piv_size)); + } + if (new_size < piv_size) { + PIINTROSPECTION_CONTAINER_UNUSED(T, (piv_size-new_size)); + } + alloc(new_size); return *this; } inline void _copyRaw(T * dst, const T * src, size_t size) { @@ -425,11 +458,24 @@ private: while (s_ >> t) ++t; return (1 << t); } + template::value + , int>::type = 0> inline void newT(T * dst, const T * src, size_t s) { PIINTROSPECTION_CONTAINER_USED(T, s) for (size_t i = 0; i < s; ++i) elementNew(dst + i, src[i]); } + template::value + , int>::type = 0> + inline void newT(T * dst, const T * src, size_t s) { + PIINTROSPECTION_CONTAINER_USED(T, s) + memcpy((void*)(dst), (const void*)(src), s * sizeof(T)); + } + template::value + , int>::type = 0> inline void deleteT(T * d, size_t sz) { PIINTROSPECTION_CONTAINER_UNUSED(T, sz) if ((uchar*)d != 0) { @@ -437,9 +483,36 @@ private: elementDelete(d[i]); } } + template::value + , int>::type = 0> + inline void deleteT(T * d, size_t sz) { + PIINTROSPECTION_CONTAINER_UNUSED(T, sz) + } + template::value + , int>::type = 0> inline void elementNew(T * to, const T & from) {new(to)T(from);} + template::value + , int>::type = 0> inline void elementNew(T * to, T && from) {new(to)T(std::move(from));} + template::value + , int>::type = 0> + inline void elementNew(T1 * to, const T & from) {(*to) = from;} + template::value + , int>::type = 0> + inline void elementNew(T * to, T && from) {(*to) = std::move(from);} + template::value + , int>::type = 0> inline void elementDelete(T & from) {from.~T();} + template::value + , int>::type = 0> + inline void elementDelete(T & from) {} inline void dealloc() { if ((uchar*)piv_data != 0) free((uchar*)piv_data); piv_data = 0; @@ -464,41 +537,6 @@ private: }; -#define __PIVECTOR_SIMPLE_TYPE__(T) \ - template<> inline void PIVector::newT(T * dst, const T * src, size_t s) {PIINTROSPECTION_CONTAINER_USED(T, s); memcpy((void*)(dst), (const void*)(src), s * sizeof(T));} \ - template<> inline void PIVector::deleteT(T *, size_t sz) {PIINTROSPECTION_CONTAINER_UNUSED(T, sz);} \ - template<> inline void PIVector::elementNew(T * to, const T & from) {(*to) = from;} \ - template<> inline void PIVector::elementNew(T * to, T && from) {(*to) = std::move(from);} \ - template<> inline void PIVector::elementDelete(T &) {;} \ - template<> inline PIVector & PIVector::_resizeRaw(size_t new_size) { \ - if (new_size > piv_size) { \ - PIINTROSPECTION_CONTAINER_USED(T, (new_size-piv_size)); \ - } \ - if (new_size < piv_size) { \ - PIINTROSPECTION_CONTAINER_UNUSED(T, (piv_size-new_size)); \ - } \ - alloc(new_size); \ - return *this; \ - } \ - template<> inline PIVector & PIVector::clear() {PIINTROSPECTION_CONTAINER_UNUSED(T, piv_size); piv_size = 0; return *this;} \ - template<> inline PIVector & PIVector::assign(size_t new_size, const T & f) {_resizeRaw(new_size); return fill(f);} - - -__PIVECTOR_SIMPLE_TYPE__(bool) -__PIVECTOR_SIMPLE_TYPE__(char) -__PIVECTOR_SIMPLE_TYPE__(uchar) -__PIVECTOR_SIMPLE_TYPE__(short) -__PIVECTOR_SIMPLE_TYPE__(ushort) -__PIVECTOR_SIMPLE_TYPE__(int) -__PIVECTOR_SIMPLE_TYPE__(uint) -__PIVECTOR_SIMPLE_TYPE__(long) -__PIVECTOR_SIMPLE_TYPE__(ulong) -__PIVECTOR_SIMPLE_TYPE__(llong) -__PIVECTOR_SIMPLE_TYPE__(ullong) -__PIVECTOR_SIMPLE_TYPE__(float) -__PIVECTOR_SIMPLE_TYPE__(double) -__PIVECTOR_SIMPLE_TYPE__(ldouble) - #ifdef PIP_STD_IOSTREAM template diff --git a/libs/main/containers/pivector2d.h b/libs/main/containers/pivector2d.h index f573439f..ad9b1ea3 100644 --- a/libs/main/containers/pivector2d.h +++ b/libs/main/containers/pivector2d.h @@ -251,9 +251,13 @@ public: piSwap(cols_, other.cols_); } + template::value + , int>::type = 0> inline PIVector2D & _resizeRaw(size_t r, size_t c) { - piCout << "Error, \"resizeRaw()\" only allowed for simple type declared with __PIVECTOR_SIMPLE_TYPE__ macro!"; - assert(0); + rows_ = r; + cols_ = c; + mat._resizeRaw(r*c); return *this; } @@ -287,22 +291,5 @@ inline PICout operator <<(PICout s, const PIVector2D & v) { return s; } -#define __PIVECTOR2D_SIMPLE_TYPE__(T) \ - template<> inline PIVector2D & PIVector2D::_resizeRaw(size_t r, size_t c) {rows_ = r; cols_ = c; mat._resizeRaw(r*c); return *this;} - -__PIVECTOR2D_SIMPLE_TYPE__(bool) -__PIVECTOR2D_SIMPLE_TYPE__(char) -__PIVECTOR2D_SIMPLE_TYPE__(uchar) -__PIVECTOR2D_SIMPLE_TYPE__(short) -__PIVECTOR2D_SIMPLE_TYPE__(ushort) -__PIVECTOR2D_SIMPLE_TYPE__(int) -__PIVECTOR2D_SIMPLE_TYPE__(uint) -__PIVECTOR2D_SIMPLE_TYPE__(long) -__PIVECTOR2D_SIMPLE_TYPE__(ulong) -__PIVECTOR2D_SIMPLE_TYPE__(llong) -__PIVECTOR2D_SIMPLE_TYPE__(ullong) -__PIVECTOR2D_SIMPLE_TYPE__(float) -__PIVECTOR2D_SIMPLE_TYPE__(double) -__PIVECTOR2D_SIMPLE_TYPE__(ldouble) #endif // PIVECTOR2D_H diff --git a/libs/main/core/pibytearray.h b/libs/main/core/pibytearray.h index d3d532b4..9c6bb60e 100644 --- a/libs/main/core/pibytearray.h +++ b/libs/main/core/pibytearray.h @@ -28,7 +28,6 @@ #include "pimap.h" #include "pivector2d.h" -__PICONTAINERS_SIMPLE_TYPE__(PIChar) #define __PIBYTEARRAY_SIMPLE_TYPE__(T) \ template<> \ diff --git a/libs/main/math/pimathcomplex.h b/libs/main/math/pimathcomplex.h index 268e7daf..e633b5c4 100644 --- a/libs/main/math/pimathcomplex.h +++ b/libs/main/math/pimathcomplex.h @@ -47,16 +47,6 @@ const complexld complexld_i(0., 1.); const complexld complexld_0(0.); const complexld complexld_1(1.); -__PICONTAINERS_SIMPLE_TYPE__(complexi) -__PICONTAINERS_SIMPLE_TYPE__(complexs) -__PICONTAINERS_SIMPLE_TYPE__(complexf) -__PICONTAINERS_SIMPLE_TYPE__(complexd) -__PICONTAINERS_SIMPLE_TYPE__(complexld) -__PIVECTOR2D_SIMPLE_TYPE__(complexi) -__PIVECTOR2D_SIMPLE_TYPE__(complexs) -__PIVECTOR2D_SIMPLE_TYPE__(complexf) -__PIVECTOR2D_SIMPLE_TYPE__(complexd) -__PIVECTOR2D_SIMPLE_TYPE__(complexld) __PIBYTEARRAY_SIMPLE_TYPE__(complexi) __PIBYTEARRAY_SIMPLE_TYPE__(complexs) __PIBYTEARRAY_SIMPLE_TYPE__(complexf) diff --git a/main.cpp b/main.cpp index 1165af51..070f9cdc 100644 --- a/main.cpp +++ b/main.cpp @@ -1,79 +1,84 @@ #include "pip.h" - -#define REGISTER_CNT (__COUNTER__) -#define REGISTER_V_STREAM_INTERNAL(T, C) \ - class _VariantRegistrator_##C##__ { \ - public: \ - _VariantRegistrator_##C##__() { \ - __VariantFunctionsBase__ * f = __VariantFunctions__().instance(); \ - __VariantFunctionsBase__::registered()[f->hash()] = f; \ - } \ - }; \ - _VariantRegistrator_##C##__ __registrator_##C##__; - - -#define REGISTER_V_STREAM_INTERNAL_W(T, C) REGISTER_V_STREAM_INTERNAL(T, C) -#define REGISTER_V_STREAM(T) REGISTER_V_STREAM_INTERNAL_W(T, __COUNTER__) - - - - -class Send: public PIObject { - PIOBJECT(Send) -public: - Send() {piCout << "Send";} - ~Send() {piCout << "~Send";} - EVENT1(ev, PIObject * , o); +struct A { + double x1; + //double x2; }; +inline PIByteArray & operator <<(PIByteArray & s, const A & a) {s << a.x1/* << a.x2*/; return s;} +inline PIByteArray & operator >>(PIByteArray & s, A & a) {s >> a.x1/* >> a.x2*/; return s;} - -class Recv: public PIObject { - PIOBJECT(Recv) -public: - Recv() {piCout << "Recv";} - ~Recv() {piCout << "~Recv";} - EVENT_HANDLER1(void, eh, PIObject * , o) { - piCout << "eh ..." << o; - o->deleteLater(); - piMSleep(1000); - piCout << "eh ok"; +struct B { + B() { + x1=0; + //x2=0; } + B(const B & b) = default; + B & operator =(const B & b) {x1=b.x1; return *this;} + double x1; + //double x2; }; +inline PIByteArray & operator <<(PIByteArray & s, const B & a) {s << a.x1/* << a.x2*/; return s;} +inline PIByteArray & operator >>(PIByteArray & s, B & a) {s >> a.x1/* >> a.x2*/; return s;} - -Send * s = new Send(); -Recv * r = new Recv(); +//__PIVECTOR_SIMPLE_TYPE__(B) #include "piconditionvar.h" int main() { + PITimeMeasurer tm; + PIByteArray ba; ba.reserve(100*100*16); - CONNECTU(s, ev, r, eh); - s->ev(r); - r->deleteLater(); - s->deleteLater(); - piMSleep(1500); - //s->deleteLater(); - //delete o; - //eth.dump(); - //versionCompare("",""); - //PICloudServer s("127.0.0.1:10101"); - //s.startThreadedRead(); - piMSleep(10); + PIVector2D vx; + PIVector2D vd; + ba << vx; + ba >> vx; + vd.resize(100, 100); + vx = vd; + tm.reset(); + for(int i=0; i<1000; ++i) { + vx.clear(); + vx = vd; + ba << vx; + ba >> vd; + } + piCout << tm.elapsed_m(); - /*PIMutex m; - PIConditionVariable var; + PIVector2D ax; + PIVector2D ad; + ad.resize(100, 100); + ax = ad; + tm.reset(); + for(int i=0; i<1000; ++i) { + ax.clear(); + ax = ad; + ba << ax; + ba >> ad; + } + piCout << tm.elapsed_m(); - PIThread::runOnce([&](){ - piCout << "wait ..."; - m.lock(); - var.wait(m); - m.unlock(); - piCout << "wait done"; - }); - - piMSleep(100); - var.notifyAll();*/ + PIVector2D bx; + PIVector2D bd; + bd.resize(100, 100); + bx = bd; + tm.reset(); + for(int i=0; i<1000; ++i) { + bx.clear(); + bx = bd; + ba << bx; + ba >> bd; + } + piCout << tm.elapsed_m(); + PIVector2D cx; + PIVector2D cd; + cd.resize(100, 100); + cx = cd; + tm.reset(); + for(int i=0; i<1000; ++i) { + cx.clear(); + cx = cd; + ba << cx; + ba >> cd; + } + piCout << tm.elapsed_m(); return 0; }