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 
61 #else
62 
63 #define RCF_ASSERT(x)
64 
65 #endif
66 
67  // Throw mechanism
68  class Exception;
69  RCF_EXPORT void rcfThrow(const char * szFile, int line, const char * szFunc, const Exception & e);
70 
71 #define RCF_THROW(e) RCF::rcfThrow(__FILE__, __LINE__, __FUNCTION__, e)
72 #define RCF_VERIFY(cond, e) if (cond); else RCF_THROW(e)
73 
74  // Null deleter, for use with for shared_ptr
75  class NullDeleter
76  {
77  public:
78  template<typename T>
79  void operator()(T)
80  {}
81  };
82 
83  // Catch handler.
84  RCF_EXPORT void rcfDtorCatchHandler(const std::exception & e);
85 
86 // destructor try/catch blocks
87 #define RCF_DTOR_BEGIN \
88  try {
89 
90 #define RCF_DTOR_END \
91  } \
92  catch (const std::exception &e) \
93  { \
94  RCF::rcfDtorCatchHandler(e); \
95  }
96 
97  struct Void {};
98 
99  template<typename Container, typename Element>
100  void eraseRemove(Container & container, const Element & element)
101  {
102  container.erase(
103  std::remove(
104  container.begin(),
105  container.end(),
106  element),
107  container.end());
108  }
109 
110  RCF_EXPORT std::uint64_t fileSize(const std::string & path);
111 
112  // For some reason C++11 doesn't have operator< for weak_ptr.
113  template<typename T>
114  inline bool operator<(
115  const std::weak_ptr<T> & lhs,
116  const std::weak_ptr<T> & rhs)
117  {
118  std::owner_less< std::weak_ptr<T> > cmp;
119  return cmp(lhs, rhs);
120  }
121 
122  template<typename T>
123  inline bool operator==(
124  const std::weak_ptr<T> & lhs,
125  const std::weak_ptr<T> & rhs)
126  {
127  return ! (lhs < rhs) && ! (rhs < lhs);
128  }
129 
130  template<typename T>
131  inline bool operator!=(
132  const std::weak_ptr<T> & lhs,
133  const std::weak_ptr<T> & rhs)
134  {
135  return ! (lhs == rhs);
136  }
137 
138  class Noncopyable
139  {
140  protected:
141  Noncopyable() {}
142  ~Noncopyable() {}
143  Noncopyable(const Noncopyable&) = delete;
144  Noncopyable& operator=(const Noncopyable&) = delete;
145  };
146 
147  class ScopeGuard
148  {
149  public:
150  ScopeGuard(std::function<void()> func);
151  ~ScopeGuard();
152 
153  void dismiss();
154 
155  private:
156  bool m_dismissed;
157  std::function<void()> m_func;
158  };
159 
160  RCF_EXPORT void trim(std::string& s);
161  RCF_EXPORT void trimLeft(std::string& s);
162  RCF_EXPORT void trimRight(std::string& s);
163  RCF_EXPORT bool iequals(const std::string& lhs, const std::string& rhs);
164  RCF_EXPORT bool istartsWith(const std::string& s, const std::string& startsWith);
165 
166  template<typename TPtr>
167  std::string getTypeName(const TPtr & tPtr)
168  {
169  if ( tPtr )
170  {
171  auto& t = *tPtr;
172  return typeid(t).name();
173  }
174  return "";
175  }
176 
177 } // namespace RCF
178 
179 namespace SF
180 {
181  typedef RCF::Noncopyable Noncopyable;
182 }
183 
184 // Eliminate unused variable warnings, e.g. for scoped lock objects
185 #define RCF_UNUSED_VARIABLE(x) ((void) x)
186 
187 // Macros in Windows platform headers make it awkward to use std::min/std::max.
188 #define RCF_MIN (std::min)
189 #define RCF_MAX (std::max)
190 
191 //****************************************************************************
192 // Helper macros to generate serialization code for fundamental types
193 
194 #define SF_FOR_EACH_FUNDAMENTAL_TYPE_(arg) \
195  arg(char) \
196  arg(int) \
197  arg(bool) \
198  arg(float) \
199  arg(double) \
200  arg(short) \
201  arg(long) \
202  arg(unsigned short) \
203  arg(signed char) \
204  arg(unsigned char) \
205  arg(unsigned int) \
206  arg(unsigned long) \
207  arg(long double) \
208  //arg(wchar_t)
209 
210 #ifdef _MSC_VER
211 
212 #define SF_FOR_EACH_FUNDAMENTAL_TYPE(arg) \
213  SF_FOR_EACH_FUNDAMENTAL_TYPE_(arg) \
214  arg(__int64) \
215  arg(unsigned __int64)
216 
217 #else
218 
219 #define SF_FOR_EACH_FUNDAMENTAL_TYPE(arg) \
220  SF_FOR_EACH_FUNDAMENTAL_TYPE_(arg) \
221  arg(long long) \
222  arg(unsigned long long)
223 
224 #endif
225 
226 #endif // ! INCLUDE_RCF_TOOLS_HPP
Definition: ByteBuffer.hpp:189
Definition: AmiIoHandler.hpp:24