/***************************************************************************** * * This file is part of Mapnik (c++ mapping toolkit) * * Copyright (C) 2021 Artem Pavlenko * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * *****************************************************************************/ #include #include MAPNIK_DISABLE_WARNING_PUSH #include #include #include #include MAPNIK_DISABLE_WARNING_POP namespace mapnik { namespace proj_transform_cache { namespace { using key_type = std::pair; using compatible_key_type = std::pair; struct compatible_hash { template std::size_t operator() (KeyType const& key) const { using hash_type = boost::hash; std::size_t seed = hash_type{}(key.first); seed ^= hash_type{}(key.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2); return seed; } }; struct compatible_predicate { bool operator()(compatible_key_type const& k1, compatible_key_type const& k2) const { return k1 == k2; } }; using cache_type = boost::unordered_map, compatible_hash>; thread_local static cache_type cache_ = {}; } // namespace void init(std::string const& source, std::string const& dest) { compatible_key_type key = std::make_pair(source, dest); auto itr = cache_.find(key, compatible_hash{}, compatible_predicate{}); if (itr == cache_.end()) { mapnik::projection p0(source, true); mapnik::projection p1(dest, true); cache_.emplace(std::make_pair(source, dest), std::make_unique(p0, p1)); } } proj_transform const* get(std::string const& source, std::string const& dest) { compatible_key_type key = std::make_pair(source, dest); auto itr = cache_.find(key, compatible_hash{}, compatible_predicate{}); if (itr == cache_.end()) { mapnik::projection srs1(source, true); mapnik::projection srs2(dest, true); return cache_.emplace(std::make_pair(source, dest), std::make_unique(srs1, srs2)).first->second.get(); } return itr->second.get(); } } // namespace proj_transform_cache } // namespace mapnik