Electroneum
misc_language.h
Go to the documentation of this file.
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 
28 
29 #pragma once
30 
31 #include <limits>
32 #include <boost/thread.hpp>
33 #include <boost/utility/value_init.hpp>
34 namespace epee
35 {
36 #define STD_TRY_BEGIN() try {
37 
38 #define STD_TRY_CATCH(where_, ret_val) \
39  } \
40  catch (const std::exception &e) \
41  { \
42  LOG_ERROR("EXCEPTION: " << where_ << ", mes: "<< e.what()); \
43  return ret_val; \
44  } \
45  catch (...) \
46  { \
47  LOG_ERROR("EXCEPTION: " << where_ ); \
48  return ret_val; \
49  }
50 
51 
52 
53 #define AUTO_VAL_INIT(v) boost::value_initialized<decltype(v)>()
54 
55 namespace misc_utils
56 {
57  template<typename t_type>
58  t_type get_max_t_val(t_type t)
59  {
60  return (std::numeric_limits<t_type>::max)();
61  }
62 
63 
64  template<typename t_iterator>
65  t_iterator move_it_forward(t_iterator it, size_t count)
66  {
67  while(count--)
68  it++;
69  return it;
70  }
71 
72  template<typename t_iterator>
73  t_iterator move_it_backward(t_iterator it, size_t count)
74  {
75  while(count--)
76  it--;
77  return it;
78  }
79 
80 
81  // TEMPLATE STRUCT less
82  template<class _Ty>
83  struct less_as_pod
84  : public std::binary_function<_Ty, _Ty, bool>
85  { // functor for operator<
86  bool operator()(const _Ty& _Left, const _Ty& _Right) const
87  { // apply operator< to operands
88  return memcmp(&_Left, &_Right, sizeof(_Left)) < 0;
89  }
90  };
91 
92  template<class _Ty>
93  bool is_less_as_pod(const _Ty& _Left, const _Ty& _Right)
94  { // apply operator< to operands
95  return memcmp(&_Left, &_Right, sizeof(_Left)) < 0;
96  }
97 
98 
99  inline
100  bool sleep_no_w(long ms )
101  {
102  boost::this_thread::sleep(
103  boost::get_system_time() +
104  boost::posix_time::milliseconds( std::max<long>(ms,0) ) );
105 
106  return true;
107  }
108 
109  template<class type_vec_type>
110  type_vec_type median(std::vector<type_vec_type> &v)
111  {
112  if(v.empty())
113  return boost::value_initialized<type_vec_type>();
114  if(v.size() == 1)
115  return v[0];
116 
117  size_t n = (v.size()) / 2;
118  std::sort(v.begin(), v.end());
119  //nth_element(v.begin(), v.begin()+n-1, v.end());
120  if(v.size()%2)
121  {//1, 3, 5...
122  return v[n];
123  }else
124  {//2, 4, 6...
125  return (v[n-1] + v[n])/2;
126  }
127 
128  }
129 
130  /************************************************************************/
131  /* */
132  /************************************************************************/
133 
135  {
137  };
138 
139  typedef boost::shared_ptr<call_befor_die_base> auto_scope_leave_caller;
140 
141 
142  template<class t_scope_leave_handler>
144  {
145  t_scope_leave_handler m_func;
146  call_befor_die(t_scope_leave_handler f):m_func(f)
147  {}
149  {
150  try { m_func(); }
151  catch (...) { /* ignore */ }
152  }
153  };
154 
155  template<class t_scope_leave_handler>
157  {
159  return slc;
160  }
161 
162  template<typename T> struct struct_init: T
163  {
164  struct_init(): T{} {}
165  };
166 
167 }
168 }
t_iterator move_it_forward(t_iterator it, size_t count)
Definition: misc_language.h:65
const uint32_t T[512]
call_befor_die(t_scope_leave_handler f)
auto_scope_leave_caller create_scope_leave_handler(t_scope_leave_handler f)
bool is_less_as_pod(const _Ty &_Left, const _Ty &_Right)
Definition: misc_language.h:93
mdb_size_t count(MDB_cursor *cur)
t_type get_max_t_val(t_type t)
Definition: misc_language.h:58
boost::shared_ptr< call_befor_die_base > auto_scope_leave_caller
bool sleep_no_w(long ms)
type_vec_type median(std::vector< type_vec_type > &v)
bool operator()(const _Ty &_Left, const _Ty &_Right) const
Definition: misc_language.h:86
t_scope_leave_handler m_func
t_iterator move_it_backward(t_iterator it, size_t count)
Definition: misc_language.h:73