По мотивам - Скорее "филосовское". Почему синтаксический сахар в виде парадигмы property не востребован в классическом C++?
Пока приведу лишь код:
Что такое NSStringHolder? Очень просто - это замена std::string для Objective-C.
Пока приведу лишь код:
//#define UseMutable template<typename DataType> class ObjCObjectHolder { protected: DataType rData = nil; protected: #ifdef UseMutable virtual #endif void setData (DataType aData) { ASSIGN(rData, aData); } public: ObjCObjectHolder () : rData(nil) { } ObjCObjectHolder (DataType aData) : rData(nil) { setData(aData); } ObjCObjectHolder (const ObjCObjectHolder & aCopy) : rData(nil) { setData(aCopy.rData); } const ObjCObjectHolder & operator = (const ObjCObjectHolder & aCopy) { setData(aCopy.rData); return *this; } const ObjCObjectHolder & operator = (DataType aCopy) { setData(aCopy); return *this; } virtual ~ObjCObjectHolder () { DESTROY(rData); } }; // ObjCObjectHolder ; // template<typename DataType> #ifdef UseMutable typedef NSMutableString NSStringHolderString; #else typedef NSString NSStringHolderString; #endif typedef ObjCObjectHolder<NSStringHolderString *> NSStringHolderPrim; class NSStringHolder : public NSStringHolderPrim { #ifdef UseMutable protected: virtual void setData (NSMutableString * aData) override { if (aData) ASSIGN(rData, [NSMutableString stringWithString: aData]); else ASSIGN(rData, [NSMutableString stringWithCapacity: 1024]); } void setData (NSString * aData) { assert(aData); ASSIGN(rData, [NSMutableString stringWithString: aData]); } #endif public: NSStringHolder () { } NSStringHolder (NSString * aString) { setData(aString); } NSStringHolder (const std::string & aValue) { NSStringHolderString *vStr = [[NSStringHolderString alloc] initWithBytes: aValue.c_str() length: aValue.length() encoding: NSWindowsCP1251StringEncoding]; @try { setData(vStr); } @finally { DESTROY(vStr); } } NSStringHolder (const char * aValue) { NSStringHolderString *vStr = [[NSStringHolderString alloc] initWithBytes: aValue length: strlen(aValue) encoding: NSWindowsCP1251StringEncoding]; @try { setData(vStr); } @finally { DESTROY(vStr); } } size_t length () const { return [rData length]; } __strong const char * c_str() const { if (rData) return [rData cStringUsingEncoding: NSWindowsCP1251StringEncoding]; else return ""; } operator NSString * () const { return rData; } operator std::string () const { if (rData) return std::string([rData cStringUsingEncoding: NSWindowsCP1251StringEncoding]); else return std::string(""); } const NSStringHolder & operator = (NSString * aValue) { setData(aValue); return *this; } const NSStringHolder & operator = (const char * aValue) { NSStringHolderString *vStr = [[NSStringHolderString alloc] initWithBytes: aValue length: strlen(aValue) encoding: NSWindowsCP1251StringEncoding]; @try { setData(vStr); } @finally { DESTROY(vStr); } return *this; } const NSStringHolder & operator = (const std::string & aValue) { NSStringHolderString *vStr = [[NSStringHolderString alloc] initWithBytes: aValue.c_str() length: aValue.length() encoding: NSWindowsCP1251StringEncoding]; @try { setData(vStr); } @finally { DESTROY(vStr); } return *this; } void append (NSString * aValue) { if (aValue) { if (rData) { #ifdef UseMutable [rData appendString: aValue]; #else setData([rData stringByAppendingString: aValue]); #endif } else setData(aValue); } } void append (const NSStringHolder & aValue) { append(aValue.rData); } const NSStringHolder & operator += (NSString * aValue) { append(aValue); return *this; } const NSStringHolder & operator += (const NSStringHolder & aValue) { append(aValue); return *this; } void trim () { assert(rData); setData([rData stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]]); } NSStringHolder substringToIndex (size_t aPos) const { assert(rData); NSStringHolder vSubstr ([rData substringToIndex: aPos]); return vSubstr; } NSStringHolder substringFromIndex (size_t aPos) const { assert(rData); NSStringHolder vSubstr ([rData substringFromIndex: aPos]); return vSubstr; } void insert (size_t aPos, const NSStringHolder & aValue) { if (aValue.rData) { if (rData) { if (aPos == 0) { setData([(NSString *)aValue stringByAppendingString: rData]); } else if (aPos == rData.length) { setData([rData stringByAppendingString: aValue]); } else { NSString *vStr = [rData substringToIndex: aPos]; vStr = [vStr stringByAppendingString: aValue]; vStr = [vStr stringByAppendingString: [rData substringFromIndex: aPos]]; setData(vStr); } } else setData(aValue.rData); } } const NSStringHolder & operator += (const char * aValue) { append(aValue); return *this; } const NSStringHolder & operator += (const std::string & aValue) { append(aValue); return *this; } void resize (size_t aNewLength) { if (aNewLength < [rData length]) setData([rData substringToIndex: aNewLength/* + 1*/]); else if (aNewLength > [rData length]) assert(false); } size_t find_last (NSString * aWhatFind, size_t aPos) const { NSRange vRange = [rData rangeOfString: aWhatFind options: NSBackwardsSearch range: NSMakeRange(0, aPos + 1)]; if (vRange.location == NSNotFound) return std::string::npos; else return vRange.location; } size_t find_first (NSString * aWhatFind, size_t aPos) const { if (aPos == std::string::npos) return std::string::npos; NSRange vRange = [rData rangeOfString: aWhatFind options: 0 range: NSMakeRange(aPos, rData.length - aPos)]; if (vRange.location == NSNotFound) return std::string::npos; else return vRange.location; } size_t find_first (const NSStringHolder & aWhatFind, size_t aPos) const { if (aPos == std::string::npos) return std::string::npos; NSRange vRange = [rData rangeOfString: aWhatFind options: 0 range: NSMakeRange(aPos, rData.length - aPos)]; if (vRange.location == NSNotFound) return std::string::npos; else return vRange.location; } size_t find (const NSStringHolder & aWhatFind) const { NSRange vRange = [rData rangeOfString: aWhatFind]; if (vRange.location == NSNotFound) return std::string::npos; else return vRange.location; } bool operator != (const NSStringHolder & aValue) const { return ![rData isEqualToString: aValue.rData]; } bool operator != (NSString * aValue) const { return ![rData isEqualToString: aValue]; } bool operator == (const NSStringHolder & aValue) const { return [rData isEqualToString: aValue.rData]; } bool operator == (NSString * aValue) const { return [rData isEqualToString: aValue]; } const unichar operator [] (size_t anIndex) const { return [rData characterAtIndex: anIndex]; } const NSStringHolder operator + (const NSStringHolder & anOther) const { NSStringHolder vOther (*this); vOther += anOther; return vOther; } const NSStringHolder operator + (const char * anOther) const { NSStringHolder vOther (*this); vOther += anOther; return vOther; } void appendLong (long aValue) { append([NSString stringWithFormat: @"%ld", aValue]); } void appendLongLong (long long aValue) { append([NSString stringWithFormat: @"%lld", aValue]); } void appendUnsignedLong (unsigned long aValue) { append([NSString stringWithFormat: @"%ld", aValue]); } void appendUnsignedLongLong (unsigned long long aValue) { append([NSString stringWithFormat: @"%lld", aValue]); } }; // NSStringHolder- всё.
Что такое NSStringHolder? Очень просто - это замена std::string для Objective-C.
Комментариев нет:
Отправить комментарий