monotone

monotone Mtn Source Tree

Root/src/safe_map.hh

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

Archive Download this file

Branches

Tags

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