monotone

monotone Mtn Source Tree

Root/safe_map.hh

1#ifndef __SAFE_MAP_HH__
2#define __SAFE_MAP_HH__
3
4// Copyright (C) 2005 Nathaniel Smith <njs@pobox.com>
5//
6// This program is made available under the GNU GPL version 2.0 or
7// greater. See the accompanying file COPYING for details.
8//
9// This program is distributed WITHOUT ANY WARRANTY; without even the
10// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11// PURPOSE.
12
13// some helpers to safely use maps
14
15#include "sanity.hh"
16
17// errors out if the key does not exist
18template <typename T>
19void
20do_safe_erase(T & container, typename T::key_type const & key,
21 char const * container_name, char const * file, int line)
22{
23 if (!container.erase(key))
24 global_sanity.invariant_failure((F("erasing nonexistent key from %s")
25 % container_name).str().c_str(),
26 file, line);
27}
28#define safe_erase(CONT, KEY) \
29 do_safe_erase((CONT), (KEY), #CONT, __FILE__, __LINE__)
30
31
32// errors out if the key already exists
33template <typename T>
34typename T::iterator
35do_safe_insert(T & container, typename T::value_type const & val,
36 char const * container_name, char const * file, int line)
37{
38 std::pair<typename T::iterator, bool> r = container.insert(val);
39 if (!r.second)
40 global_sanity.invariant_failure((F("inserting duplicate entry into %s")
41 % container_name).str().c_str(),
42 file, line);
43 return r.first;
44}
45#define safe_insert(CONT, VAL) \
46 do_safe_insert((CONT), (VAL), #CONT, __FILE__, __LINE__)
47
48
49// errors out if the key already exists
50template <typename T>
51typename T::iterator
52do_safe_insert(T & container, typename T::iterator where,
53 typename T::value_type const & val,
54 char const * container_name, char const * file, int line)
55{
56 typename T::size_type pre_size = container.size();
57 typename T::iterator r = container.insert(where, val);
58 if (pre_size == container.size())
59 global_sanity.invariant_failure((F("inserting duplicate entry into %s")
60 % container_name).str().c_str(),
61 file, line);
62 return r;
63}
64#define hinted_safe_insert(CONT, HINT, VAL) \
65 do_safe_insert((CONT), (HINT), (VAL), #CONT, __FILE__, __LINE__)
66
67
68// errors out if the key does not exist
69template <typename T>
70typename T::mapped_type const &
71do_safe_get(T & container, typename T::key_type const & key,
72 char const * container_name, char const * file, int line)
73{
74 typename T::const_iterator i = container.find(key);
75 if (i == container.end())
76 global_sanity.invariant_failure((F("fetching nonexistent entry from %s")
77 % container_name).str().c_str(),
78 file, line);
79 return i->second;
80}
81#define safe_get(CONT, VAL) \
82 do_safe_get((CONT), (VAL), #CONT, __FILE__, __LINE__)
83
84// Local Variables:
85// mode: C++
86// fill-column: 76
87// c-file-style: "gnu"
88// indent-tabs-mode: nil
89// End:
90// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
91
92#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status