/* Boost.Flyweight example of use of key-value flyweights. * * Copyright 2006-2008 Joaquin M Lopez Munoz. * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) * * See http://www.boost.org/libs/flyweight for library home page. */ #include #include #include #include #include #include #include using namespace boost::flyweights; /* A class simulating a texture resource loaded from file */ class texture { public: texture(const std::string& filename):filename(filename) { std::cout<<"loaded "< > texture_flyweight; int main() { /* texture filenames */ const char* texture_filenames[]={ "grass.texture","sand.texture","water.texture","wood.texture", "granite.texture","cotton.texture","concrete.texture","carpet.texture" }; const int num_texture_filenames= sizeof(texture_filenames)/sizeof(texture_filenames[0]); /* create a massive vector of textures */ std::cout<<"creating flyweights...\n"< textures; for(int i=0;i<50000;++i){ textures.push_back( texture_flyweight(texture_filenames[std::rand()%num_texture_filenames])); } /* Just for the sake of making use of the key extractor, * assign some flyweights with texture objects rather than strings. */ for(int j=0;j<50000;++j){ textures.push_back( texture_flyweight( textures[std::rand()%textures.size()].get())); } std::cout<<"\n"<