void testWeakCache() { WeakCache<std::string, CacheTestRef> cache; debugAssert(CacheTest::count == 0); CacheTestRef x = new CacheTest(); debugAssert(CacheTest::count == 1); cache.set("x", x); debugAssert(CacheTest::count == 1); CacheTestRef y = new CacheTest(); CacheTestRef z = new CacheTest(); debugAssert(CacheTest::count == 3); cache.set("y", y); debugAssert(cache["x"] == x); debugAssert(cache["y"] == y); debugAssert(cache["q"].isNull()); x = NULL; debugAssert(CacheTest::count == 2); debugAssert(cache["x"].isNull()); cache.set("y", z); y = NULL; debugAssert(cache["y"] == z); cache.remove("y"); }
shared_ptr<IconSet> IconSet::fromFile(const std::string& filename) { shared_ptr<IconSet> set = cache[filename]; if (notNull(set)) { return set; } BinaryInput b(filename, G3D_LITTLE_ENDIAN); const std::string& header = b.readString(); alwaysAssertM(header == "ICON", "Corrupt icon file"); const float version = b.readFloat32(); alwaysAssertM(version == 1.0f, "Unsupported icon file version"); shared_ptr<IconSet> s(new IconSet()); s->m_icon.resize(b.readInt32()); for (int i = 0; i < s->m_icon.size(); ++i) { Entry& e = s->m_icon[i]; e.filename = b.readString32(); float x, y, w, h; x = b.readFloat32(); y = b.readFloat32(); w = b.readFloat32(); h = b.readFloat32(); e.rect = Rect2D::xywh(x, y, w, h); s->m_index.set(e.filename, i); } shared_ptr<Image> image = Image::fromBinaryInput(b); Texture::Settings settings; settings.wrapMode = WrapMode::CLAMP; settings.interpolateMode = Texture::BILINEAR_NO_MIPMAP; s->m_texture = Texture::fromPixelTransferBuffer(filename, image->toPixelTransferBuffer(), ImageFormat::AUTO(), Texture::DIM_2D_NPOT, settings); cache.set(filename, s); return s; }