source: src/Common/Utility.hpp @ 562ccf9

Last change on this file since 562ccf9 was c92bdcc, checked in by Andrew Beach <ajbeach@…>, 6 months ago

Updated the rest of the names in src/ (except for the generated files).

  • Property mode set to 100644
File size: 3.1 KB
Line 
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.hpp -- 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
26template<typename T>
27T copy( const T & x ) { return x; }
28
29/// Splice src onto the end of dst, clearing src
30template< typename T >
31void 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
38template< typename T >
39void 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.
45template<typename Container, typename Pred>
46void 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
52static 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
63template< typename T >
64struct ValueGuard {
65        T old;
66        T& ref;
67
68        ValueGuard(T& inRef) : old(inRef), ref(inRef) {}
69        ~ValueGuard() { ref = old; }
70};
71
72template< typename T >
73struct 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
83template< typename aT >
84struct 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
97template< typename bT, typename aT >
98FuncGuard<aT> makeFuncGuard( bT && before, aT && after ) {
99        return FuncGuard<aT>( std::forward<bT>(before), std::forward<aT>(after) );
100}
101
102template< typename T >
103struct 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: //
Note: See TracBrowser for help on using the repository browser.