Skip to content

Commit

Permalink
COMMON: Add PtrMap template
Browse files Browse the repository at this point in the history
PtrMap is to boost::ptr_map what PtrList is to boost::ptr_list.
  • Loading branch information
DrMcCoy committed Oct 29, 2016
1 parent b4055cc commit e634fad
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/common/ptrmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* xoreos-tools - Tools to help with xoreos development
*
* xoreos-tools is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos-tools is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* xoreos-tools 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with xoreos-tools. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file
* A map storing pointer to objects, with automatic deletion.
*/

#ifndef COMMON_PTRMAP_H
#define COMMON_PTRMAP_H

#include <functional>
#include <map>

#include <boost/noncopyable.hpp>

#include "src/common/deallocator.h"

namespace Common {

/** A map of pointer to objects, with automatic deletion.
*
* A PtrMap expects to hold sole ownership of its contents.
*/
template<typename Key, typename T, class Compare = std::less<Key>, class Deallocator = DeallocatorDefault>
class PtrMap : boost::noncopyable, public std::map<Key, T *, Compare> {
public:
~PtrMap() {
clear();
}

void clear() {
for (typename std::map<Key, T *, Compare>::iterator it = std::map<Key, T *, Compare>::begin();
it != std::map<Key, T *, Compare>::end(); ++it)
Deallocator::destroy(it->second);

std::map<Key, T *, Compare>::clear();
}

void erase(typename std::map<Key, T *, Compare>::iterator position) {
Deallocator::destroy(position->second);
return std::map<Key, T *, Compare>::erase(position);
}

typename std::map<Key, T *, Compare>::size_type
erase(const typename std::map<Key, T *, Compare>::key_type &k) {

typename std::map<Key, T *, Compare>::iterator it(std::map<Key, T *, Compare>::find(k));
if (it == std::map<Key, T *, Compare>::end())
return 0;

erase(it);
return 1;
}

void erase(typename std::map<Key, T *, Compare>::iterator first,
typename std::map<Key, T *, Compare>::iterator last) {

for (typename std::map<Key, T *, Compare>::iterator it = std::map<Key, T *, Compare>::begin();
it != std::map<Key, T *, Compare>::end(); ++it)
Deallocator::destroy(it->second);

return std::map<Key, T *, Compare>::erase(first, last);
}
};

} // End of namespace Common

#endif // COMMON_PTRMAP_H
1 change: 1 addition & 0 deletions src/common/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ src_common_libcommon_la_SOURCES += \
src/common/disposableptr.h \
src/common/ptrlist.h \
src/common/ptrvector.h \
src/common/ptrmap.h \
src/common/maths.h \
src/common/singleton.h \
src/common/ustring.h \
Expand Down

0 comments on commit e634fad

Please sign in to comment.