Remote Call Framework 3.0
Tools.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2018, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 3.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_TOOLS_HPP
20 #define INCLUDE_RCF_TOOLS_HPP
21 
22 #include <algorithm>
23 #include <functional>
24 #include <memory>
25 #include <string.h>
26 
27 #include <RCF/Config.hpp>
28 #include <RCF/Export.hpp>
29 
30 // Auto linking on VC++
31 #ifdef _MSC_VER
32 #pragma comment(lib, "ws2_32.lib")
33 #pragma comment(lib, "mswsock.lib")
34 #pragma comment(lib, "advapi32.lib")
35 #pragma comment(lib, "user32.lib")
36 #pragma comment(lib, "crypt32.lib")
37 #pragma comment(lib, "rpcrt4.lib")
38 #endif
39 
40 namespace RCF {
41 
42  // Logging
43  static const int LogNameRcf = 1;
44  static const int LogLevel_1 = 1; // Error and exceptions.
45  static const int LogLevel_2 = 2; // Larger scale setup/teardown.
46  static const int LogLevel_3 = 3; // Messages sent and received (RCF level), RCF client and session lifetime.
47  static const int LogLevel_4 = 4; // Messages sent and received (network level), network client and session lifetime.
48 
49 #define RCF_LOG_1() UTIL_LOG(RCF::LogNameRcf, RCF::LogLevel_1)
50 #define RCF_LOG_2() UTIL_LOG(RCF::LogNameRcf, RCF::LogLevel_2)
51 #define RCF_LOG_3() UTIL_LOG(RCF::LogNameRcf, RCF::LogLevel_3)
52 #define RCF_LOG_4() UTIL_LOG(RCF::LogNameRcf, RCF::LogLevel_4)
53 
54  // Asserts
55 #ifndef NDEBUG
56 
57  RCF_EXPORT void doAssert(const char * szFile, int line, const char * szFunc, const char * szAssertion);
58 
59 #define RCF_ASSERT(x) if (x) ; else RCF::doAssert(__FILE__, __LINE__, __FUNCTION__, #x);
60 #define RCF_ASSERT_ALWAYS(x) RCF::doAssert(__FILE__, __LINE__, __FUNCTION__, x);
61 
62 #else
63 
64 #define RCF_ASSERT(x)
65 #define RCF_ASSERT_ALWAYS(x)
66 
67 #endif
68 
69  // Throw mechanism
70  class Exception;
71  RCF_EXPORT void rcfThrow(const char * szFile, int line, const char * szFunc, const Exception & e);
72 
73 #define RCF_THROW(e) RCF::rcfThrow(__FILE__, __LINE__, __FUNCTION__, e)
74 #define RCF_VERIFY(cond, e) if (cond); else RCF_THROW(e)
75 
76  // Null deleter, for use with for shared_ptr
77  class NullDeleter
78  {
79  public:
80  template<typename T>
81  void operator()(T)
82  {}
83  };
84 
85  // Catch handler.
86  RCF_EXPORT void rcfDtorCatchHandler(const std::exception & e);
87 
88 // destructor try/catch blocks
89 #define RCF_DTOR_BEGIN \
90  try {
91 
92 #define RCF_DTOR_END \
93  } \
94  catch (const std::exception &e) \
95  { \
96  RCF::rcfDtorCatchHandler(e); \
97  }
98 
99  struct Void {};
100 
101  template<typename Container, typename Element>
102  void eraseRemove(Container & container, const Element & element)
103  {
104  container.erase(
105  std::remove(
106  container.begin(),
107  container.end(),
108  element),
109  container.end());
110  }
111 
112  RCF_EXPORT std::uint64_t fileSize(const std::string & path);
113 
114  // For some reason C++11 doesn't have operator< for weak_ptr.
115  template<typename T>
116  inline bool operator<(
117  const std::weak_ptr<T> & lhs,
118  const std::weak_ptr<T> & rhs)
119  {
120  std::owner_less< std::weak_ptr<T> > cmp;
121  return cmp(lhs, rhs);
122  }
123 
124  template<typename T>
125  inline bool operator==(
126  const std::weak_ptr<T> & lhs,
127  const std::weak_ptr<T> & rhs)
128  {
129  return ! (lhs < rhs) && ! (rhs < lhs);
130  }
131 
132  template<typename T>
133  inline bool operator!=(
134  const std::weak_ptr<T> & lhs,
135  const std::weak_ptr<T> & rhs)
136  {
137  return ! (lhs == rhs);
138  }
139 
140  class Noncopyable
141  {
142  protected:
143  Noncopyable() {}
144  ~Noncopyable() {}
145  Noncopyable(const Noncopyable&) = delete;
146  Noncopyable& operator=(const Noncopyable&) = delete;
147  };
148 
149  class ScopeGuard
150  {
151  public:
152  ScopeGuard(std::function<void()> func);
153  ~ScopeGuard();
154 
155  void dismiss();
156 
157  private:
158  bool m_dismissed;
159  std::function<void()> m_func;
160  };
161 
162  RCF_EXPORT void trim(std::string& s);
163  RCF_EXPORT void trimLeft(std::string& s);
164  RCF_EXPORT void trimRight(std::string& s);
165  RCF_EXPORT bool iequals(const std::string& lhs, const std::string& rhs);
166  RCF_EXPORT bool istartsWith(const std::string& s, const std::string& startsWith);
167 
168  template<typename TPtr>
169  std::string getTypeName(const TPtr & tPtr)
170  {
171  if ( tPtr )
172  {
173  auto& t = *tPtr;
174  return typeid(t).name();
175  }
176  return "";
177  }
178 
179 } // namespace RCF
180 
181 namespace SF
182 {
183  typedef RCF::Noncopyable Noncopyable;
184 }
185 
186 // Eliminate unused variable warnings, e.g. for scoped lock objects
187 #define RCF_UNUSED_VARIABLE(x) ((void) x)
188 
189 // Macros in Windows platform headers make it awkward to use std::min/std::max.
190 #define RCF_MIN (std::min)
191 #define RCF_MAX (std::max)
192 
193 //****************************************************************************
194 // Helper macros to generate serialization code for fundamental types
195 
196 #define SF_FOR_EACH_FUNDAMENTAL_TYPE_(arg) \
197  arg(char) \
198  arg(int) \
199  arg(bool) \
200  arg(float) \
201  arg(double) \
202  arg(short) \
203  arg(long) \
204  arg(unsigned short) \
205  arg(signed char) \
206  arg(unsigned char) \
207  arg(unsigned int) \
208  arg(unsigned long) \
209  arg(long double) \
210  //arg(wchar_t)
211 
212 #ifdef _MSC_VER
213 
214 #define SF_FOR_EACH_FUNDAMENTAL_TYPE(arg) \
215  SF_FOR_EACH_FUNDAMENTAL_TYPE_(arg) \
216  arg(__int64) \
217  arg(unsigned __int64)
218 
219 #else
220 
221 #define SF_FOR_EACH_FUNDAMENTAL_TYPE(arg) \
222  SF_FOR_EACH_FUNDAMENTAL_TYPE_(arg) \
223  arg(long long) \
224  arg(unsigned long long)
225 
226 #endif
227 
228 #endif // ! INCLUDE_RCF_TOOLS_HPP
Definition: ByteBuffer.hpp:189
Definition: AmiIoHandler.hpp:24