version 2.8
ccfixes.hh
1 
2 #ifdef NEED_STRING_FIXES
3  #include <string>
4  #include <sstream>
5  #include <stdexcept>
6  #include <limits>
7  #include <cstdlib>
8 
9  namespace std {
10  template <typename T> inline std::string to_string(T value)
11  {
12  std::ostringstream os ;
13  os << value ;
14  return os.str() ;
15  }
16 
17  inline int stoi( const std::string& str, std::size_t* pos = 0, int base = 10 )
18  {
19  const char* begin = str.c_str() ;
20  char* end = nullptr ;
21  long value = std::strtol( begin, &end, base ) ;
22 
23  if( errno == ERANGE || value > std::numeric_limits<int>::max() )
24  throw std::out_of_range( "stoi: out ofrange" ) ;
25 
26  if( end == str.c_str() )
27  throw std::invalid_argument( "stoi: invalid argument" ) ;
28 
29  if(pos) *pos = end - begin ;
30 
31  return value ;
32  }
33  }
34 #endif
35 
36 #ifdef NEED_THREAD_FIXES
37 #ifndef WIN32STDTHREAD_H
38  #define WIN32STDTHREAD_H
39 
40  #include <windows.h>
41  #include <functional>
42  #include <memory>
43  #include <chrono>
44  #include <system_error>
45  #include <process.h>
46 
47  #define _STD_THREAD_INVALID_HANDLE 0
48  namespace std
49  {
50 
51 
52  class thread
53  {
54  public:
55  class id
56  {
57  DWORD mId;
58  void clear() {mId = 0;}
59  friend class thread;
60  public:
61  id(DWORD aId=0):mId(aId){}
62  bool operator==(const id& other) const {return mId == other.mId;}
63  };
64  protected:
65  HANDLE mHandle;
66  id mThreadId;
67  public:
68  typedef HANDLE native_handle_type;
69  id get_id() const noexcept {return mThreadId;}
70  native_handle_type native_handle() const {return mHandle;}
71  thread(): mHandle(_STD_THREAD_INVALID_HANDLE){}
72  thread(thread& other)
73  :mHandle(other.mHandle), mThreadId(other.mThreadId)
74  {
75  other.mHandle = _STD_THREAD_INVALID_HANDLE;
76  other.mThreadId.clear();
77  }
78  template<class Function, class... Args>
79  explicit thread(Function&& f, Args&&... args)
80  {
81  typedef decltype(std::bind(f, args...)) Call;
82  Call* call = new Call(std::bind(f, args...));
83  mHandle = (HANDLE)_beginthreadex(NULL, 0, threadfunc<Call>,
84  (LPVOID)call, 0, (unsigned*)&(mThreadId.mId));
85  }
86  template <class Call>
87  static unsigned int __stdcall threadfunc(void* arg)
88  {
89  std::unique_ptr<Call> upCall(static_cast<Call*>(arg));
90  (*upCall)();
91  return (unsigned long)0;
92  }
93  bool joinable() const {return mHandle != _STD_THREAD_INVALID_HANDLE;}
94  void join()
95  {
96  if (get_id() == GetCurrentThreadId())
97  throw system_error(EDEADLK, generic_category());
98  if (mHandle == _STD_THREAD_INVALID_HANDLE)
99  throw system_error(ESRCH, generic_category());
100  if (!joinable())
101  throw system_error(EINVAL, generic_category());
102  WaitForSingleObject(mHandle, INFINITE);
103  CloseHandle(mHandle);
104  mHandle = _STD_THREAD_INVALID_HANDLE;
105  mThreadId.clear();
106  }
107 
108  ~thread()
109  {
110  if (joinable())
111  std::terminate();
112  }
113  thread& operator=(const thread&) = delete;
114  thread& operator=(thread&& other) noexcept
115  {
116  if (joinable())
117  std::terminate();
118  swap(std::forward<thread>(other));
119  return *this;
120  }
121  void swap(thread&& other) noexcept
122  {
123  std::swap(mHandle, other.mHandle);
124  std::swap(mThreadId.mId, other.mThreadId.mId);
125  }
126  static unsigned int hardware_concurrency() noexcept {return 1;}
127  void detach()
128  {
129  if (!joinable())
130  throw system_error();
131  mHandle = _STD_THREAD_INVALID_HANDLE;
132  mThreadId.clear();
133  }
134  };
135  namespace this_thread
136  {
137  inline thread::id get_id() {return thread::id(GetCurrentThreadId());}
138  inline void yield() {Sleep(0);}
139  template< class Rep, class Period >
140  void sleep_for( const std::chrono::duration<Rep,Period>& sleep_duration)
141  {
142  Sleep(chrono::duration_cast<chrono::milliseconds>(sleep_duration).count());
143  }
144  template <class Clock, class Duration>
145  void sleep_until(const std::chrono::time_point<Clock,Duration>& sleep_time)
146  {
147  sleep_for(sleep_time-Clock::now());
148  }
149  }
150  }
151 #endif
152 #endif