monotone

monotone Mtn Source Tree

Root/src/vector.hh

1// Copyright (C) 2002 Graydon Hoare <graydon@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 __VECTOR_HH__
11#define __VECTOR_HH__
12
13// we're interested in trapping index overflows early and precisely,
14// because they usually represent *very significant* logic errors. we use
15// an inline template function because the idx(...) needs to be used as an
16// expression, not as a statement.
17
18// to ensure that our index checkers are always visible when std::vector
19// is in use, this header is the only file that should include <vector>;
20// all others should include this file instead.
21
22#include <vector>
23#include "sanity.hh"
24#include "quick_alloc.hh" // to get the QA() macro
25
26template <typename T>
27inline T & checked_index(std::vector<T> & v,
28 typename std::vector<T>::size_type i,
29 char const * vec,
30 char const * index,
31 char const * file,
32 int line)
33{
34 if (UNLIKELY(i >= v.size()))
35 global_sanity.index_failure(vec, index, v.size(), i, file, line);
36 return v[i];
37}
38
39template <typename T>
40inline T const & checked_index(std::vector<T> const & v,
41 typename std::vector<T>::size_type i,
42 char const * vec,
43 char const * index,
44 char const * file,
45 int line)
46{
47 if (UNLIKELY(i >= v.size()))
48 global_sanity.index_failure(vec, index, v.size(), i, file, line);
49 return v[i];
50}
51
52#ifdef QA_SUPPORTED
53template <typename T>
54inline T & checked_index(std::vector<T, QA(T)> & v,
55 typename std::vector<T>::size_type i,
56 char const * vec,
57 char const * index,
58 char const * file,
59 int line)
60{
61 if (UNLIKELY(i >= v.size()))
62 global_sanity.index_failure(vec, index, v.size(), i, file, line);
63 return v[i];
64}
65
66template <typename T>
67inline T const & checked_index(std::vector<T, QA(T)> const & v,
68 typename std::vector<T>::size_type i,
69 char const * vec,
70 char const * index,
71 char const * file,
72 int line)
73{
74 if (UNLIKELY(i >= v.size()))
75 global_sanity.index_failure(vec, index, v.size(), i, file, line);
76 return v[i];
77}
78#endif // QA_SUPPORTED
79
80#define idx(v, i) checked_index((v), (i), #v, #i, __FILE__, __LINE__)
81
82template <typename T> void
83dump(std::vector<T> const & vec, std::string & out)
84{
85 for (size_t i = 0; i < vec.size(); ++i)
86 {
87 T const & val = vec[i];
88 std::string msg;
89 dump(val, msg);
90 out += msg;
91 }
92};
93
94#endif // __VECTOR_HH__
95
96// Local Variables:
97// mode: C++
98// fill-column: 76
99// c-file-style: "gnu"
100// indent-tabs-mode: nil
101// End:
102// 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