// MANIPULATORS bsl::shared_ptr<char> SharedPtrUtil::createInplaceUninitializedBuffer( size_t bufferSize, bslma::Allocator *basicAllocator) { basicAllocator = bslma::Default::allocator(basicAllocator); // allocator is optional // We have alignment concerns here: there are no alignment issues with // 'bslstl::SharedPtrRep', but the buffer address (i.e., the address of // 'd_instance' in the 'bslstl::SharedPtrInplaceRep' object) must be at // least *naturally* *aligned* to 'bufferSize'. See the 'bslma' package- // level documentation for a definition of natural alignment.) We achieve // this in the simplest way by always maximally aligning the returned // pointer. typedef bslma::SharedPtrInplaceRep<bsls::AlignmentUtil::MaxAlignedType> Rep; enum { k_ALIGNMENT_MASK = ~(bsls::AlignmentUtil::BSLS_MAX_ALIGNMENT - 1) }; size_t repSize = (sizeof(Rep) + bufferSize - 1) & k_ALIGNMENT_MASK; Rep *rep = new (basicAllocator->allocate(repSize)) Rep(basicAllocator); return bsl::shared_ptr<char>(reinterpret_cast<char *>(rep->ptr()), rep); }
void wstring::alloc(size_t size, bool save) { if (! check_realloc(size)) return; Rep *p = Rep::create(size); if (save) { p->copy(0, data(), length()); p->len = length(); } else p->len = 0; repup(p); }
wstring& wstring::replace(size_t pos, size_t n1, const wchar_t* s, size_t n2) { const size_t len = length(); if (pos > len) throw out_of_range("pos > len"); if (n1 > len - pos) n1 = len - pos; if (len - n1 > max_size() - n2) throw length_error("len - n1 > max_size() - n2"); size_t newlen = len - n1 + n2; if (check_realloc(newlen)) { Rep *p = Rep::create(newlen); p->copy(0, data(), pos); p->copy(pos + n2, data() + pos + n1, len -(pos + n1)); p->copy(pos, s, n2); repup(p); } else { rep()->move(pos + n2, data() + pos + n1, len -(pos + n1)); rep()->copy(pos, s, n2); } rep()->len = newlen; return *this; }
wchar_t* wstring::Rep::clone() { Rep* p = Rep::create(len); p->copy(0, data(), len); p->len = len; return p->data(); }
void test_matrix_rep() { Rep matrix; matrix.set(1, 1, 1); matrix.reserve(3, 3); matrix.set(3, 2, 3); assert(matrix.get(3, 2) == 3); assert(matrix.get(4, 3) == 0); assert(matrix.is_null(4,3)); assert(!matrix.is_null(3,2)); matrix.set(4, 2, 2); matrix.reserve(4, 4); assert(matrix.get(3, 2) == 3); assert(matrix.get(4, 2) == 2); assert(!matrix.is_null(4, 2)); assert(matrix.is_null(10, 10)); }