GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/util/utility.h Lines: 15 15 100.0 %
Date: 2021-03-23 Branches: 18 51 35.3 %

Line Exec Source
1
/*********************                                                        */
2
/*! \file utility.h
3
 ** \verbatim
4
 ** Top contributors (to current version):
5
 **   Andres Noetzli, Morgan Deters, Aina Niemetz
6
 ** This file is part of the CVC4 project.
7
 ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 ** in the top-level source directory and their institutional affiliations.
9
 ** All rights reserved.  See the file COPYING in the top-level source
10
 ** directory for licensing information.\endverbatim
11
 **
12
 ** \brief Some standard STL-related utility functions for CVC4
13
 **
14
 ** Some standard STL-related utility functions for CVC4.
15
 **/
16
17
#include "cvc4_private.h"
18
19
#ifndef CVC4__UTILITY_H
20
#define CVC4__UTILITY_H
21
22
#include <algorithm>
23
#include <fstream>
24
#include <memory>
25
#include <string>
26
27
namespace CVC4 {
28
29
30
/**
31
 * Using std::find_if(), finds the first iterator in [first,last)
32
 * range that satisfies predicate.  If none, return last; otherwise,
33
 * search for a second one.  If there IS a second one, return last,
34
 * otherwise return the first (and unique) iterator satisfying pred().
35
 */
36
template <class InputIterator, class Predicate>
37
474322
inline InputIterator find_if_unique(InputIterator first, InputIterator last, Predicate pred) {
38
474322
  InputIterator match = std::find_if(first, last, pred);
39
474322
  if(match == last) {
40
2
    return last;
41
  }
42
43
474320
  InputIterator match2 = match;
44
474320
  match2 = std::find_if(++match2, last, pred);
45
474320
  return (match2 == last) ? match : last;
46
}
47
48
template <typename T>
49
12
void container_to_stream(std::ostream& out,
50
                         const T& container,
51
                         const char* prefix = "[",
52
                         const char* postfix = "]",
53
                         const char* sep = ", ")
54
{
55
12
  out << prefix;
56
12
  bool is_first = true;
57
22
  for (const auto& item : container)
58
  {
59
10
    out << (!is_first ? sep : "") << item;
60
10
    is_first = false;
61
  }
62
12
  out << postfix;
63
12
}
64
65
/**
66
 * Opens a new temporary file with a given filename pattern and returns an
67
 * fstream to it. The directory that the file is created in is either TMPDIR or
68
 * /tmp/ if TMPDIR is not set.
69
 *
70
 * @param pattern The filename pattern. This string is modified to contain the
71
 * name of the temporary file.
72
 *
73
 * @return A unique pointer to the filestream for the temporary file.
74
 *
75
 * Note: We use `std::unique_ptr<std::fstream>` instead of `std::fstream`
76
 * because GCC < 5 does not support the move constructor of `std::fstream`. See
77
 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316 for details.
78
 */
79
std::unique_ptr<std::fstream> openTmpFile(std::string* pattern);
80
81
}/* CVC4 namespace */
82
83
#endif /* CVC4__UTILITY_H */