TEST(SmartPointerTests, SharedPtr) { { std::shared_ptr<Foo> sh1; EXPECT_EQ(sh1.use_count(), 0); } { Foo* foo = new Foo(); std::shared_ptr<Foo> sh2(foo); std::shared_ptr<Foo> sh3(sh2); std::shared_ptr<Foo> sh4; std::shared_ptr<Foo> sh5; sh4 = sh2; sh5 = std::shared_ptr<Foo>(sh2); EXPECT_EQ(sh2.use_count(), 4); EXPECT_EQ(sh3.use_count(), 4); EXPECT_EQ(sh4.use_count(), 4); EXPECT_EQ(sh5.use_count(), 4); } }
int main() { { Foo *f =new Foo; std::shared_ptr<Foo> sh2(f); std::shared_ptr<Foo> sh3(f); std::cout << sh2.use_count() << '\n'; std::cout << sh3.use_count() << '\n'; } }
int main(void) { double r1, r2, r3, x; int i; x = 10.0*10.0*10.0; printf("10^i\tsh1\t\tsh2\t\tsh3\t\terr(sh2)\terr(sh3)\n"); for(i = 2; i > -N; i--) { x /= 10.0; r1 = sh1(x); r2 = sh2(x); r3 = sh3(x); printf("10^%d\t%e\t%e\t%e\t%e\t%e\n", i, r1, r2, r3, (r1-r2)/r1, (r1-r3)/r1); } return 0; }
int main() { { std::cout << "constructor with no managed object\n"; jle::shared_ptr<Foo> sh1; } { std::cout << "constructor with object\n"; jle::shared_ptr<Foo> sh2(new Foo); jle::shared_ptr<Foo> sh3(sh2); std::cout << sh2.use_count() << '\n'; std::cout << sh3.use_count() << '\n'; } { std::cout << "constructor with object and deleter\n"; jle::shared_ptr<Foo> sh4(new Foo, D()); } { std::cout << "valid pointer" << std::endl; auto ptr = jle::make_shared<int>(22); std::cout << "expired " << int(ptr.expired()) << std::endl; } { std::cout << "invalid pointer" << std::endl; auto ptr = jle::make_shared<int>(22); ptr.reset(); std::cout << "expired " << int(ptr.expired()) << std::endl; } { std::cout << "invalid pointer" << std::endl; jle::shared_ptr<int> ptr; std::cout << "expired " << int(ptr.expired()) << std::endl; } { jle::shared_ptr<int> foo = jle::make_shared<int> (10); // same as: jle::shared_ptr<int> foo2 (new int(10)); auto bar = jle::make_shared<int> (20); auto baz = jle::make_shared<std::pair<int,int>> (30,40); std::cout << "*foo: " << *foo << '\n'; std::cout << "*bar: " << *bar << '\n'; std::cout << "*baz: " << baz->first << ' ' << baz->second << '\n'; } { std::cout << "invalid pointer" << std::endl; auto ptr = jle::make_shared<int>(22); ptr = {}; std::cout << "expired " << int(ptr.expired()) << std::endl; } }
// TODO: the following function should go somewhere else, perhaps in ImageIO.cpp // Make sure the image has the smallest number of bands before writing. // That is, if it's 4 bands with full alpha, reduce to 3 bands. // If it's 3 bands with constant colors, make it 1-band. CByteImage removeRedundantBands(CByteImage img) { CShape sh = img.Shape(); int w = sh.width, h = sh.height, nB = sh.nBands; int x, y; if (nB < 3) return img; // check if full alpha if alpha channel present bool fullAlpha = true; if (nB == 4) { for (y = 0; y < h && fullAlpha; y++) { uchar *pix = &img.Pixel(0, y, 0); for (x = 0; x < w; x++) { if (pix[3] != 255) { fullAlpha = false; break; } pix += nB; } } } if (!fullAlpha) return img; // check for equal colors bool equalColors = true; for (y = 0; y < h && equalColors; y++) { uchar *pix = &img.Pixel(0, y, 0); for (x = 0; x < w; x++) { if (pix[0] != pix[1] || pix[0] != pix[2] || pix[1] != pix[2]) { equalColors = false; break; } pix += nB; } } // at this point, if nB == 4 we can reduce to at least 3 bands, // and if equalColors we can reduce to 1 band. if (! equalColors && nB < 4) return img; int newNB = equalColors ? 1 : 3; if (DEBUG_ImageIOpng) fprintf(stderr, "reducing from %d to %d bands\n", nB, newNB); CShape sh2(w, h, newNB); CByteImage img2(sh2); for (y = 0; y < h; y++) { uchar *pix = &img.Pixel(0, y, 0); uchar *pix2 = &img2.Pixel(0, y, 0); for (x = 0; x < w; x++) { for (int b = 0; b < newNB; b++) { pix2[b] = pix[b]; } pix += nB; pix2 += newNB; } } return img2; }