// // Pool of shared objects // // Copyright (C) 2015-2018 Andreas Gustafsson. This file is part of // the Gaborator library source distribution. See the file LICENSE at // the top level of the distribution for license information. // #ifndef _GABORATOR_POOL_H #define _GABORATOR_POOL_H #include namespace gaborator { // The "pool" class is for sharing FFT objects so that we don't // create multiple FFTs of the same size. It could also be used // to share objects of some other class T where we don't want to // create multiple Ts with the same K. template struct pool { typedef std::map m_t; ~pool() { for (typename m_t::iterator it = m.begin(); it != m.end(); it++) { delete (*it).second; } } T *get(const K &k) { std::pair r = m.insert(std::make_pair(k, (T *)0)); if (r.second) { // New element was inserted assert((*(r.first)).second == 0); (*r.first).second = new T(k); } return (*r.first).second; } m_t m; static pool shared; }; template pool pool::shared; } // namespace #endif