📄 object.cxx
字号:
BOOL PObject::IsClass(const char * clsName) const{ return strcmp(clsName, Class()) == 0;}BOOL PObject::IsDescendant(const char * clsName) const{ return strcmp(clsName, Class()) == 0;}PObject::Comparison PObject::CompareObjectMemoryDirect(const PObject&obj) const{ int retval = memcmp(this, &obj, sizeof(PObject)); if (retval < 0) return LessThan; if (retval > 0) return GreaterThan; return EqualTo;}PObject * PObject::Clone() const{ PAssertAlways(PUnimplementedFunction); return NULL;}PObject::Comparison PObject::Compare(const PObject & obj) const{ return (Comparison)CompareObjectMemoryDirect(obj);}void PObject::PrintOn(ostream & strm) const{ strm << GetClass();}void PObject::ReadFrom(istream &){}PINDEX PObject::PreSerialise(PSerialiser &){ return 0;}void PObject::Serialise(PSerialiser &){}void PObject::UnSerialise(PUnSerialiser &){}PINDEX PObject::HashFunction() const{ return 0;}///////////////////////////////////////////////////////////////////////////////// Serialisation supportPSerialRegistration * PSerialRegistration::creatorHashTable[ PSerialRegistration::HashTableSize];PINDEX PSerialRegistration::HashFunction(const char * className){ PINDEX hash = (BYTE)className[0]; if (className[0] != '\0') { hash += (BYTE)className[1]; if (className[1] != '\0') hash += (BYTE)className[2]; } return hash%HashTableSize;}PSerialRegistration::PSerialRegistration(const char * clsNam, CreatorFunction func) : className(clsNam), creator(func){ clash = NULL; PINDEX hash = HashFunction(className); if (creatorHashTable[hash] != NULL) creatorHashTable[hash]->clash = this; creatorHashTable[hash] = this;}PSerialRegistration::CreatorFunction PSerialRegistration::GetCreator(const char * clsNam){ PINDEX hash = HashFunction(clsNam); PSerialRegistration * reg = creatorHashTable[hash]; while (reg != NULL) { if (strcmp(reg->className, clsNam) == 0) return reg->creator; } return NULL;}PSerialiser::PSerialiser(ostream & strm) : stream(strm){}PSerialiser & PSerialiser::operator<<(PObject & obj){ obj.Serialise(*this); return *this;}PTextSerialiser::PTextSerialiser(ostream & strm, PObject & data) : PSerialiser(strm){ data.Serialise(*this);}PSerialiser & PTextSerialiser::operator<<(char) { return *this; }PSerialiser & PTextSerialiser::operator<<(unsigned char) { return *this; }PSerialiser & PTextSerialiser::operator<<(signed char) { return *this; }PSerialiser & PTextSerialiser::operator<<(short) { return *this; }PSerialiser & PTextSerialiser::operator<<(unsigned short) { return *this; }PSerialiser & PTextSerialiser::operator<<(int) { return *this; }PSerialiser & PTextSerialiser::operator<<(unsigned int) { return *this; }PSerialiser & PTextSerialiser::operator<<(long) { return *this; }PSerialiser & PTextSerialiser::operator<<(unsigned long) { return *this; }PSerialiser & PTextSerialiser::operator<<(float) { return *this; }PSerialiser & PTextSerialiser::operator<<(double) { return *this; }PSerialiser & PTextSerialiser::operator<<(long double) { return *this; }PSerialiser & PTextSerialiser::operator<<(const char *) { return *this; }PSerialiser & PTextSerialiser::operator<<(const unsigned char *) { return *this; }PSerialiser & PTextSerialiser::operator<<(const signed char *) { return *this; }PSerialiser & PTextSerialiser::operator<<(PObject & obj) { return PSerialiser::operator<<(obj); }PBinarySerialiser::PBinarySerialiser(ostream & strm, PObject & data) : PSerialiser(strm){ classesUsed = new PSortedStringList; data.PreSerialise(*this); stream << *classesUsed; data.Serialise(*this);}PBinarySerialiser::~PBinarySerialiser(){ delete classesUsed;}PSerialiser & PBinarySerialiser::operator<<(char) { return *this; }PSerialiser & PBinarySerialiser::operator<<(unsigned char) { return *this; }PSerialiser & PBinarySerialiser::operator<<(signed char) { return *this; }PSerialiser & PBinarySerialiser::operator<<(short) { return *this; }PSerialiser & PBinarySerialiser::operator<<(unsigned short) { return *this; }PSerialiser & PBinarySerialiser::operator<<(int) { return *this; }PSerialiser & PBinarySerialiser::operator<<(unsigned int) { return *this; }PSerialiser & PBinarySerialiser::operator<<(long) { return *this; }PSerialiser & PBinarySerialiser::operator<<(unsigned long) { return *this; }PSerialiser & PBinarySerialiser::operator<<(float) { return *this; }PSerialiser & PBinarySerialiser::operator<<(double) { return *this; }PSerialiser & PBinarySerialiser::operator<<(long double) { return *this; }PSerialiser & PBinarySerialiser::operator<<(const char *) { return *this; }PSerialiser & PBinarySerialiser::operator<<(const unsigned char *) { return *this; }PSerialiser & PBinarySerialiser::operator<<(const signed char *) { return *this; }PSerialiser & PBinarySerialiser::operator<<(PObject & obj) { return PSerialiser::operator<<(obj); }PUnSerialiser::PUnSerialiser(istream & strm) : stream(strm){}PTextUnSerialiser::PTextUnSerialiser(istream & strm) : PUnSerialiser(strm){}PUnSerialiser & PTextUnSerialiser::operator>>(char &) { return *this; }PUnSerialiser & PTextUnSerialiser::operator>>(unsigned char &) { return *this; }PUnSerialiser & PTextUnSerialiser::operator>>(signed char &) { return *this; }PUnSerialiser & PTextUnSerialiser::operator>>(short &) { return *this; }PUnSerialiser & PTextUnSerialiser::operator>>(unsigned short &) { return *this; }PUnSerialiser & PTextUnSerialiser::operator>>(int &) { return *this; }PUnSerialiser & PTextUnSerialiser::operator>>(unsigned int &) { return *this; }PUnSerialiser & PTextUnSerialiser::operator>>(long &) { return *this; }PUnSerialiser & PTextUnSerialiser::operator>>(unsigned long &) { return *this; }PUnSerialiser & PTextUnSerialiser::operator>>(float &) { return *this; }PUnSerialiser & PTextUnSerialiser::operator>>(double &) { return *this; }PUnSerialiser & PTextUnSerialiser::operator>>(long double &) { return *this; }PUnSerialiser & PTextUnSerialiser::operator>>(char *) { return *this; }PUnSerialiser & PTextUnSerialiser::operator>>(unsigned char *) { return *this; }PUnSerialiser & PTextUnSerialiser::operator>>(signed char *) { return *this; }PUnSerialiser & PTextUnSerialiser::operator>>(PObject &) { return *this; }PBinaryUnSerialiser::PBinaryUnSerialiser(istream & strm) : PUnSerialiser(strm){ classesUsed = new PStringArray;}PBinaryUnSerialiser::~PBinaryUnSerialiser(){ delete classesUsed;}PUnSerialiser & PBinaryUnSerialiser::operator>>(char &) { return *this; }PUnSerialiser & PBinaryUnSerialiser::operator>>(unsigned char &) { return *this; }PUnSerialiser & PBinaryUnSerialiser::operator>>(signed char &) { return *this; }PUnSerialiser & PBinaryUnSerialiser::operator>>(short &) { return *this; }PUnSerialiser & PBinaryUnSerialiser::operator>>(unsigned short &) { return *this; }PUnSerialiser & PBinaryUnSerialiser::operator>>(int &) { return *this; }PUnSerialiser & PBinaryUnSerialiser::operator>>(unsigned int &) { return *this; }PUnSerialiser & PBinaryUnSerialiser::operator>>(long &) { return *this; }PUnSerialiser & PBinaryUnSerialiser::operator>>(unsigned long &) { return *this; }PUnSerialiser & PBinaryUnSerialiser::operator>>(float &) { return *this; }PUnSerialiser & PBinaryUnSerialiser::operator>>(double &) { return *this; }PUnSerialiser & PBinaryUnSerialiser::operator>>(long double &) { return *this; }PUnSerialiser & PBinaryUnSerialiser::operator>>(char *) { return *this; }PUnSerialiser & PBinaryUnSerialiser::operator>>(unsigned char *) { return *this; }PUnSerialiser & PBinaryUnSerialiser::operator>>(signed char *) { return *this; }PUnSerialiser & PBinaryUnSerialiser::operator>>(PObject &) { return *this; }///////////////////////////////////////////////////////////////////////////////// General reference counting supportPSmartPointer::PSmartPointer(const PSmartPointer & ptr){ object = ptr.object; if (object != NULL) ++object->referenceCount;}PSmartPointer & PSmartPointer::operator=(const PSmartPointer & ptr){ if (object == ptr.object) return *this; if (object != NULL && --object->referenceCount == 0) delete object; object = ptr.object; if (object != NULL) ++object->referenceCount; return *this;}PSmartPointer::~PSmartPointer(){ if (object != NULL && --object->referenceCount == 0) delete object;}PObject::Comparison PSmartPointer::Compare(const PObject & obj) const{ PAssert(obj.IsDescendant(PSmartPointer::Class()), PInvalidCast); PSmartObject * other = ((const PSmartPointer &)obj).object; if (object == other) return EqualTo; return object < other ? LessThan : GreaterThan;}///////////////////////////////////////////////////////////////////////////////// Large integer support#ifdef P_NEEDS_INT64void PInt64__::Add(const PInt64__ & v){ unsigned long old = low; high += v.high; low += v.low; if (low < old) high++;}void PInt64__::Sub(const PInt64__ & v){ unsigned long old = low; high -= v.high; low -= v.low; if (low > old) high--;}void PInt64__::Mul(const PInt64__ & v){ DWORD p1 = (low&0xffff)*(v.low&0xffff); DWORD p2 = (low >> 16)*(v.low >> 16); DWORD p3 = (high&0xffff)*(v.high&0xffff); DWORD p4 = (high >> 16)*(v.high >> 16); low = p1 + (p2 << 16); high = (p2 >> 16) + p3 + (p4 << 16);}void PInt64__::Div(const PInt64__ & v){ long double dividend = high; dividend *= 4294967296.0; dividend += low; long double divisor = high; divisor *= 4294967296.0; divisor += low; long double quotient = dividend/divisor; low = quotient; high = quotient/4294967296.0;}void PInt64__::Mod(const PInt64__ & v){ PInt64__ t = *this; t.Div(v); t.Mul(t); Sub(t);}void PInt64__::ShiftLeft(int bits){ if (bits >= 32) { high = low << (bits - 32); low = 0; } else { high <<= bits; high |= low >> (32 - bits); low <<= bits; }}void PInt64__::ShiftRight(int bits){ if (bits >= 32) { low = high >> (bits - 32); high = 0; } else { low >>= bits; low |= high << (32 - bits); high >>= bits; }}BOOL PInt64::Lt(const PInt64 & v) const{ if ((long)high < (long)v.high) return TRUE; if ((long)high > (long)v.high) return FALSE; if ((long)high < 0) return (long)low > (long)v.low; return (long)low < (long)v.low;}BOOL PInt64::Gt(const PInt64 & v) const{ if ((long)high > (long)v.high) return TRUE; if ((long)high < (long)v.high) return FALSE; if ((long)high < 0) return (long)low < (long)v.low; return (long)low > (long)v.low;}BOOL PUInt64::Lt(const PUInt64 & v) const{ if (high < v.high) return TRUE; if (high > v.high) return FALSE; return low < high;}BOOL PUInt64::Gt(const PUInt64 & v) const{ if (high > v.high) return TRUE; if (high < v.high) return FALSE; return low > high;}static void Out64(ostream & stream, PUInt64 num){ char buf[25]; char * p = &buf[sizeof(buf)]; *--p = '\0'; switch (stream.flags()&ios::basefield) { case ios::oct : while (num != 0) { *--p = (num&7) + '0'; num >>= 3; } break; case ios::hex : while (num != 0) { *--p = (num&15) + '0'; if (*p > '9') *p += 7; num >>= 4; } break; default : while (num != 0) { *--p = num%10 + '0'; num /= 10; } } if (*p == '\0') *--p = '0'; stream << p;}ostream & operator<<(ostream & stream, const PInt64 & v){ if (v >= 0) Out64(stream, v); else { int w = stream.width(); stream.put('-'); if (w > 0) stream.width(w-1); Out64(stream, -v); } return stream;}ostream & operator<<(ostream & stream, const PUInt64 & v){ Out64(stream, v); return stream;}static PUInt64 Inp64(istream & stream){ int base; switch (stream.flags()&ios::basefield) { case ios::oct : base = 8; break; case ios::hex : base = 16; break; default : base = 10; } if (isspace(stream.peek())) stream.get(); PInt64 num = 0; while (isxdigit(stream.peek())) { int c = stream.get() - '0'; if (c > 9) c -= 7; if (c > 9) c -= 32; num = num*base + c; } return num;}istream & operator>>(istream & stream, PInt64 & v){ if (isspace(stream.peek())) stream.get(); switch (stream.peek()) { case '-' : stream.ignore(); v = -(PInt64)Inp64(stream); break; case '+' : stream.ignore(); default : v = (PInt64)Inp64(stream); } return stream;}istream & operator>>(istream & stream, PUInt64 & v){ v = Inp64(stream); return stream;}#endif// End Of File ///////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -