1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // utility.h -- General utilities used across the compiler. |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Wed Jan 17 14:40:00 2024 |
---|
13 | // Update Count : 54 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <cassert> |
---|
19 | #include <algorithm> |
---|
20 | #include <list> |
---|
21 | #include <string> |
---|
22 | #include <type_traits> |
---|
23 | #include <vector> |
---|
24 | |
---|
25 | /// partner to move that copies any copyable type |
---|
26 | template<typename T> |
---|
27 | T copy( const T & x ) { return x; } |
---|
28 | |
---|
29 | /// Splice src onto the end of dst, clearing src |
---|
30 | template< typename T > |
---|
31 | void splice( std::vector< T > & dst, std::vector< T > & src ) { |
---|
32 | dst.reserve( dst.size() + src.size() ); |
---|
33 | for ( T & x : src ) { dst.emplace_back( std::move( x ) ); } |
---|
34 | src.clear(); |
---|
35 | } |
---|
36 | |
---|
37 | /// Splice src onto the begining of dst, clearing src |
---|
38 | template< typename T > |
---|
39 | void spliceBegin( std::vector< T > & dst, std::vector< T > & src ) { |
---|
40 | splice( src, dst ); |
---|
41 | dst.swap( src ); |
---|
42 | } |
---|
43 | |
---|
44 | /// Remove elements that match pred from the container. |
---|
45 | template<typename Container, typename Pred> |
---|
46 | void erase_if( Container & cont, Pred && pred ) { |
---|
47 | auto keep_end = std::remove_if( cont.begin(), cont.end(), pred ); |
---|
48 | cont.erase( keep_end, cont.end() ); |
---|
49 | } |
---|
50 | |
---|
51 | // determines if pref is a prefix of str |
---|
52 | static inline bool isPrefix( const std::string & str, const std::string & pref, unsigned int start = 0 ) { |
---|
53 | if ( pref.size() > str.size() ) return false; |
---|
54 | return pref == str.substr(start, pref.size()); |
---|
55 | } |
---|
56 | |
---|
57 | // ----------------------------------------------------------------------------- |
---|
58 | // RAII object to regulate "save and restore" behaviour, e.g. |
---|
59 | // void Foo::bar() { |
---|
60 | // ValueGuard<int> guard(var); // var is a member of type Foo |
---|
61 | // var = ...; |
---|
62 | // } // var's original value is restored |
---|
63 | template< typename T > |
---|
64 | struct ValueGuard { |
---|
65 | T old; |
---|
66 | T& ref; |
---|
67 | |
---|
68 | ValueGuard(T& inRef) : old(inRef), ref(inRef) {} |
---|
69 | ~ValueGuard() { ref = old; } |
---|
70 | }; |
---|
71 | |
---|
72 | template< typename T > |
---|
73 | struct ValueGuardPtr { |
---|
74 | T old; |
---|
75 | T* ref; |
---|
76 | |
---|
77 | ValueGuardPtr(T * inRef) : old( inRef ? *inRef : T() ), ref(inRef) {} |
---|
78 | ValueGuardPtr(const ValueGuardPtr& other) = delete; |
---|
79 | ValueGuardPtr(ValueGuardPtr&& other) : old(other.old), ref(other.ref) { other.ref = nullptr; } |
---|
80 | ~ValueGuardPtr() { if( ref ) *ref = old; } |
---|
81 | }; |
---|
82 | |
---|
83 | template< typename aT > |
---|
84 | struct FuncGuard { |
---|
85 | aT m_after; |
---|
86 | |
---|
87 | template< typename bT > |
---|
88 | FuncGuard( bT before, aT after ) : m_after( after ) { |
---|
89 | before(); |
---|
90 | } |
---|
91 | |
---|
92 | ~FuncGuard() { |
---|
93 | m_after(); |
---|
94 | } |
---|
95 | }; |
---|
96 | |
---|
97 | template< typename bT, typename aT > |
---|
98 | FuncGuard<aT> makeFuncGuard( bT && before, aT && after ) { |
---|
99 | return FuncGuard<aT>( std::forward<bT>(before), std::forward<aT>(after) ); |
---|
100 | } |
---|
101 | |
---|
102 | template< typename T > |
---|
103 | struct ValueGuardPtr< std::list< T > > { |
---|
104 | std::list< T > old; |
---|
105 | std::list< T >* ref; |
---|
106 | |
---|
107 | ValueGuardPtr( std::list< T > * inRef) : old(), ref(inRef) { |
---|
108 | if( ref ) { swap( *ref, old ); } |
---|
109 | } |
---|
110 | ~ValueGuardPtr() { if( ref ) { swap( *ref, old ); } } |
---|
111 | }; |
---|
112 | |
---|
113 | // Local Variables: // |
---|
114 | // tab-width: 4 // |
---|
115 | // mode: c++ // |
---|
116 | // compile-command: "make install" // |
---|
117 | // End: // |
---|