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