[51587aa] | 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 | //
|
---|
[c92bdcc] | 7 | // Utility.hpp -- General utilities used across the compiler.
|
---|
[51587aa] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[298fe57] | 11 | // Last Modified By : Andrew Beach
|
---|
[5bf685f] | 12 | // Last Modified On : Wed Jan 17 14:40:00 2024
|
---|
| 13 | // Update Count : 54
|
---|
[51587aa] | 14 | //
|
---|
[01aeade] | 15 |
|
---|
[6b0b624] | 16 | #pragma once
|
---|
[51b73452] | 17 |
|
---|
[234b1cb] | 18 | #include <cassert>
|
---|
[940bba3] | 19 | #include <algorithm>
|
---|
[51b73452] | 20 | #include <list>
|
---|
[e491159] | 21 | #include <string>
|
---|
[55a68c3] | 22 | #include <type_traits>
|
---|
[234b1cb] | 23 | #include <vector>
|
---|
[c1ed2ee] | 24 |
|
---|
| 25 | /// partner to move that copies any copyable type
|
---|
| 26 | template<typename T>
|
---|
| 27 | T copy( const T & x ) { return x; }
|
---|
| 28 |
|
---|
[234b1cb] | 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 |
|
---|
[5bf685f] | 44 | /// Remove elements that match pred from the container.
|
---|
[298fe57] | 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 |
|
---|
[8abca06] | 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 ) {
|
---|
[bc3127d] | 53 | if ( pref.size() > str.size() ) return false;
|
---|
[8f06277] | 54 | return pref == str.substr(start, pref.size());
|
---|
[bc3127d] | 55 | }
|
---|
| 56 |
|
---|
[940bba3] | 57 | // -----------------------------------------------------------------------------
|
---|
[c8dfcd3] | 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 |
|
---|
[134322e] | 72 | template< typename T >
|
---|
| 73 | struct ValueGuardPtr {
|
---|
| 74 | T old;
|
---|
| 75 | T* ref;
|
---|
| 76 |
|
---|
| 77 | ValueGuardPtr(T * inRef) : old( inRef ? *inRef : T() ), ref(inRef) {}
|
---|
[1b65595] | 78 | ValueGuardPtr(const ValueGuardPtr& other) = delete;
|
---|
| 79 | ValueGuardPtr(ValueGuardPtr&& other) : old(other.old), ref(other.ref) { other.ref = nullptr; }
|
---|
[134322e] | 80 | ~ValueGuardPtr() { if( ref ) *ref = old; }
|
---|
| 81 | };
|
---|
| 82 |
|
---|
[234223f] | 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 |
|
---|
[134322e] | 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 |
|
---|
[51587aa] | 113 | // Local Variables: //
|
---|
| 114 | // tab-width: 4 //
|
---|
| 115 | // mode: c++ //
|
---|
| 116 | // compile-command: "make install" //
|
---|
| 117 | // End: //
|
---|