Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Utility.hpp « detail « v2 « afio « boost « include « attic - github.com/windirstat/llfio.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ea8acdf223cbd9e4cc4d36163a1f690b3340c89f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* 
 * File:   Utility.hpp
 * Author: ned14
 *
 * Created on June 25, 2013, 12:43 PM


Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/


#if !ASIO_STANDALONE   
#include "boost/exception/diagnostic_information.hpp"  
#endif

//! \def BOOST_AFIO_TYPEALIGNMENT(bytes) The markup this compiler uses to mark a type as having some given alignment
#ifndef BOOST_AFIO_TYPEALIGNMENT
#ifdef __cpp_alignas
#define BOOST_AFIO_TYPEALIGNMENT(bytes) alignas(bytes)
#else
#ifdef _MSC_VER
#define BOOST_AFIO_TYPEALIGNMENT(bytes) __declspec(align(bytes))
#elif defined(__GNUC__)
#define BOOST_AFIO_TYPEALIGNMENT(bytes) __attribute__((aligned(bytes)))
#else
#define BOOST_AFIO_TYPEALIGNMENT(bytes) unknown_type_alignment_markup_for_this_compiler
#endif
#endif
#endif

BOOST_AFIO_V2_NAMESPACE_BEGIN
  namespace detail {
#ifdef _MSC_VER
            static inline int win32_exception_filter()
            {
                return EXCEPTION_EXECUTE_HANDLER;
            }
            static inline void set_threadname(const char *threadName)
            {
                const DWORD MS_VC_EXCEPTION=0x406D1388;

#pragma pack(push,8)
                typedef struct tagTHREADNAME_INFO
                {
                    DWORD dwType; // Must be 0x1000.
                    LPCSTR szName; // Pointer to name (in user addr space).
                    DWORD dwThreadID; // Thread ID (-1=caller thread).
                    DWORD dwFlags; // Reserved for stl_future use, must be zero.
                } THREADNAME_INFO;
#pragma pack(pop)
                THREADNAME_INFO info;
                info.dwType = 0x1000;
                info.szName = threadName;
                info.dwThreadID = (DWORD) -1;
                info.dwFlags = 0;

                __try
                {
                    RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*) &info);
                }
                __except(win32_exception_filter())
                {
                    int a=1;
                }
            }
#elif defined(__linux__)
            static inline void set_threadname(const char *threadName)
            {
                pthread_setname_np(pthread_self(), threadName);
            }
#else
            static inline void set_threadname(const char *threadName)
            {
            }
#endif

            BOOST_AFIO_HEADERS_ONLY_FUNC_SPEC void print_fatal_exception_message_to_stderr(const char *msg);
        }
BOOST_AFIO_V2_NAMESPACE_END

#ifndef BOOST_AFIO_THROW_FATAL
// Need some portable way of throwing a really absolutely definitely fatal exception
// If we guaranteed had noexcept, this would be easy, but for compilers without noexcept
// we'll bounce through extern "C" as well just to be sure
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4297) // function assumed not to throw an exception but does __declspec(nothrow) or throw() was specified on the function
#endif
#ifdef BOOST_AFIO_COMPILING_FOR_GCOV
#define BOOST_AFIO_THROW_FATAL(x) std::terminate()
#else
namespace boost { namespace afio { namespace fatal_exception_throw {
            template<class T> inline void do_throw_fatal_exception(const T &v) noexcept
            {
                BOOST_AFIO_V2_NAMESPACE::detail::print_fatal_exception_message_to_stderr(v.what());
                throw v;
            }
            extern "C" inline void boost_afio_do_throw_fatal_exception(std::function<void()> impl) noexcept{ impl(); }
            template<class T> inline void throw_fatal_exception(const T &v) noexcept
            {
                // In case the extern "C" fails to terminate, trap and terminate here
                try
                {
                    std::function<void()> doer=std::bind(&do_throw_fatal_exception<T>, std::ref(v));
                    boost_afio_do_throw_fatal_exception(doer);
                }
                catch(...)
                {
                    std::terminate(); // Sadly won't produce much of a useful error message
                }
            }
} } }
#define BOOST_AFIO_THROW_FATAL(x) boost::afio::fatal_exception_throw::throw_fatal_exception(x)
#endif
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#define BOOST_AFIO_THROW(x) throw x
#define BOOST_AFIO_RETHROW throw
#endif // BOOST_AFIO_THROW_FATAL


BOOST_AFIO_V2_NAMESPACE_BEGIN

  namespace detail {
    // Support for make_unique. I keep wishing it was already here!
    template<class T, class... Args>
    std::unique_ptr<T> make_unique(Args &&... args){
        return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
    }

    // Support for combining hashes.
    template <class T>
    inline void hash_combine(std::size_t& seed, const T& v)
    {
        std::hash<T> hasher;
        seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }

    template<class T> struct decay_preserving_cv {
      typedef typename std::remove_reference<T>::type U;
      typedef typename std::conditional<
        std::is_array<U>::value,
        typename std::remove_extent<U>::type*,
        typename std::conditional< 
          std::is_function<U>::value,
          typename std::add_pointer<U>::type,
          U
        >::type
      >::type type;
    };
    // Support for SFINAE detection of iterator/pointer ranges (Can it dereference? Can it increment?)
//    template<class T, typename = void> struct is_rangeable : std::false_type { };
//    template<class T> struct is_rangeable<T, decltype(*std::declval<T&>(), ++std::declval<T&>(), void())> : std::true_type { };
    // Support for SFINAE detection of containers (does it have begin() and end()?), made considerably more complex by needing MSVC to work.
    template<class T> inline auto is_container_impl(T) ->  decltype(*std::begin(std::declval<T>()), *std::end(std::declval<T>()), bool()) { return true; }
    inline int is_container_impl(...) { return 0; }
    template<class T, typename=decltype(is_container_impl(std::declval<T>()))> struct is_container : std::false_type { };
    template<class T> struct is_container<T, bool> : std::true_type
    {
      typedef decltype(*std::begin(*((typename std::remove_reference<T>::type *) nullptr))) raw_type;  //!< The raw type (probably a (const) lvalue ref) returned by *it
      typedef typename decay_preserving_cv<raw_type>::type type;  //!< The type held by the container, still potentially const if container does not permit write access
    };

    // Debug printing of exception info
    inline std::ostream &output_exception_info(std::ostream &os, const std::exception &e)
    {
        return os << "Exception: '" << e.what() << "'";
    }
#if !ASIO_STANDALONE
    inline std::ostream &output_exception_info(std::ostream &os, const boost::exception &e)
    {
        return os << "Exception: '" << boost::current_exception_diagnostic_information() << "'";
    }
#endif
    inline std::ostream &output_exception_info(std::ostream &os)
    {
        try { throw; }
        catch(const std::exception &e) { return output_exception_info(os, e); }
#if !ASIO_STANDALONE
        catch(const boost::exception &e) { return output_exception_info(os, e); }
#endif
        catch(...) { return os << "Exception : 'unknown type'"; }
    }
    
  } // namespace

#if BOOST_AFIO_USE_BOOST_THREAD
    typedef boost::exception_ptr exception_ptr;
    using boost::current_exception;
    using boost::rethrow_exception;
#else
    typedef std::exception_ptr exception_ptr;
    using std::current_exception;
    using std::rethrow_exception;
#endif
    // Get an exception ptr from a stl_future
    template<typename T> inline exception_ptr get_exception_ptr(stl_future<T> &f)
    {
#if BOOST_AFIO_USE_BOOST_THREAD && BOOST_VERSION>=105500
        // Thanks to Vicente for adding this to Boost.Thread
        return f.get_exception_ptr();
#else
        // This seems excessive but I don't see any other legal way to extract the exception ...
        bool success=false;
        try
        {
            f.get();
            success=true;
        }
        catch(...)
        {
            exception_ptr e(current_exception());
            assert(e);
            return e;
        }
        return exception_ptr();
#endif
    }
    template<typename T> inline exception_ptr get_exception_ptr(const shared_future<T> &f)
    {
#if BOOST_AFIO_USE_BOOST_THREAD && BOOST_VERSION>=105500
        // Thanks to Vicente for adding this to Boost.Thread
        return const_cast<shared_future<T> &>(f).get_exception_ptr();
#else
        // This seems excessive but I don't see any other legal way to extract the exception ...
        bool success=false;
        try
        {
            // std::shared_future in older libstdc++ does not have a const get().
            const_cast<shared_future<T> &>(f).get();
            success=true;
        }
        catch(...)
        {
            exception_ptr e(current_exception());
            assert(e);
            return e;
        }
        return exception_ptr();
#endif
    }
    // Is a stl_future ready?
    template<typename T> inline bool is_ready(const stl_future<T> &f)
    {
#if BOOST_AFIO_USE_BOOST_THREAD
        return f.is_ready();
#else
        return f.wait_for(chrono::seconds(0))==future_status::ready;
#endif
    }
    template<typename T> inline bool is_ready(const shared_future<T> &f)
    {
#if BOOST_AFIO_USE_BOOST_THREAD
        return f.is_ready();
#else
        return f.wait_for(chrono::seconds(0))==future_status::ready;
#endif
    }

    struct filesystem_hash
    {
        std::hash<filesystem::path::string_type> hasher;
    public:
        size_t operator()(const filesystem::path& p) const
        {
            return hasher(p.native());
        }
    };

BOOST_AFIO_V2_NAMESPACE_END