RCFProto
 All Classes Functions Typedefs
Tools.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, 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: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_TOOLS_HPP
20 #define INCLUDE_RCF_TOOLS_HPP
21 
22 // Various utilities
23 
24 #include <deque>
25 #include <stdexcept>
26 #include <typeinfo>
27 #include <vector>
28 
29 #include <boost/config.hpp>
30 #include <boost/shared_ptr.hpp>
31 
32 #include <RCF/Export.hpp>
33 #include <RCF/util/UnusedVariable.hpp>
34 #include <RCF/util/VariableArgMacro.hpp>
35 
36 // Logging mechanism
37 #include <RCF/util/Log.hpp>
38 
39 namespace RCF {
40  static const int LogNameRcf = 1;
41  static const int LogLevel_1 = 1; // Error and exceptions.
42  static const int LogLevel_2 = 2; // Larger scale setup/teardown.
43  static const int LogLevel_3 = 3; // Messages sent and received (RCF level), RCF client and session lifetime.
44  static const int LogLevel_4 = 4; // Messages sent and received (network level), network client and session lifetime.
45 
46 } // namespace RCF
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 // Assertion mechanism
54 #ifndef NDEBUG
55 
56 // Debug build asserts.
57 #include <RCF/util/Assert.hpp>
58 #define RCF_ASSERT(x) UTIL_ASSERT(x, RCF::AssertionFailureException(), RCF::LogNameRcf, RCF::LogLevel_1)
59 
60 #define RCF_ASSERT_EQ(a,b) RCF_ASSERT(a == b)(a)(b)
61 #define RCF_ASSERT_NEQ(a,b) RCF_ASSERT(a != b)(a)(b)
62 
63 #define RCF_ASSERT_LT(a,b) RCF_ASSERT(a < b)(a)(b)
64 #define RCF_ASSERT_LTEQ(a,b) RCF_ASSERT(a <= b)(a)(b)
65 
66 #define RCF_ASSERT_GT(a,b) RCF_ASSERT(a > b)(a)(b)
67 #define RCF_ASSERT_GTEQ(a,b) RCF_ASSERT(a >= b)(a)(b)
68 
69 #else
70 
71 // Release build - strip out asserts.
72 #define RCF_ASSERT(x) DUMMY_VARIABLE_ARG_MACRO()
73 
74 #define RCF_ASSERT_EQ(a,b) DUMMY_VARIABLE_ARG_MACRO()
75 #define RCF_ASSERT_NEQ(a,b) DUMMY_VARIABLE_ARG_MACRO()
76 
77 #define RCF_ASSERT_LT(a,b) DUMMY_VARIABLE_ARG_MACRO()
78 #define RCF_ASSERT_LTEQ(a,b) DUMMY_VARIABLE_ARG_MACRO()
79 
80 #define RCF_ASSERT_GT(a,b) DUMMY_VARIABLE_ARG_MACRO()
81 #define RCF_ASSERT_GTEQ(a,b) DUMMY_VARIABLE_ARG_MACRO()
82 
83 #endif
84 
85 // Throw mechanism
86 
87 namespace RCF {
88  class Exception;
89  RCF_EXPORT DummyVariableArgMacroObject rcfThrow(const char * szFile, int line, const char * szFunc, const Exception & e);
90 }
91 
92 #ifndef NDEBUG
93 
94 // Debug build throw - embed file and line info
95 #define RCF_THROW(e) RCF::rcfThrow(__FILE__, __LINE__, __FUNCTION__, e)
96 
97 #else
98 
99 // Release build throw.
100 #define RCF_THROW(e) RCF::rcfThrow(__FILE__, __LINE__, __FUNCTION__, e)
101 //#define RCF_THROW(e) RCF::rcfThrow(__FILE__, __LINE__, "", e)
102 //#define RCF_THROW(e) RCF::rcfThrow("", 0, "", e)
103 //#define RCF_THROW(e) throw e;
104 
105 #endif
106 
107 // Verification mechanism
108 #define RCF_VERIFY(cond, e) if (cond); else RCF_THROW(e)
109 
110 // Scope guard mechanism
111 #include <boost/multi_index/detail/scope_guard.hpp>
112 
113 namespace RCF {
114 
115  // null deleter, for use with for shared_ptr
116  class NullDeleter
117  {
118  public:
119  template<typename T>
120  void operator()(T)
121  {}
122  };
123 
124  class SharedPtrIsNull
125  {
126  public:
127  template<typename T>
128  bool operator()(boost::shared_ptr<T> spt) const
129  {
130  return spt.get() == NULL;
131  }
132  };
133 
134 } // namespace RCF
135 
136 namespace RCF {
137 
138  RCF_EXPORT void rcfDtorCatchHandler(const std::exception & e);
139 
140 } // namespace RCF
141 
142 // destructor try/catch blocks
143 #define RCF_DTOR_BEGIN \
144  try {
145 
146 #define RCF_DTOR_END \
147  } \
148  catch (const std::exception &e) \
149  { \
150  RCF::rcfDtorCatchHandler(e); \
151  }
152 
153 //#if defined(_MSC_VER) && _MSC_VER < 1310
154 //#define RCF_PFTO_HACK long
155 //#else
156 //#define RCF_PFTO_HACK
157 //#endif
158 #define RCF_PFTO_HACK
159 
160 // Auto linking on VC++
161 #ifdef _MSC_VER
162 #pragma comment(lib, "ws2_32.lib")
163 #pragma comment(lib, "mswsock.lib")
164 #pragma comment(lib, "advapi32.lib")
165 #pragma comment(lib, "user32.lib")
166 #pragma comment(lib, "crypt32.lib")
167 #endif
168 
169 namespace RCF {
170 
171  struct Void {};
172 
173  template<typename Container, typename Element>
174  void eraseRemove(Container & container, const Element & element)
175  {
176  container.erase(
177  std::remove(
178  container.begin(),
179  container.end(),
180  element),
181  container.end());
182  }
183 
184  RCF_EXPORT boost::uint64_t fileSize(const std::string & path);
185 
186 } // namespace RCF
187 
188 namespace boost {
189 
190  template<typename T>
191  inline bool operator==(
192  const boost::weak_ptr<T> & lhs,
193  const boost::weak_ptr<T> & rhs)
194  {
195  return ! (lhs < rhs) && ! (rhs < lhs);
196  }
197 
198  template<typename T>
199  inline bool operator!=(
200  const boost::weak_ptr<T> & lhs,
201  const boost::weak_ptr<T> & rhs)
202  {
203  return ! (lhs == rhs);
204  }
205 
206 } // namespace boost
207 
208 #endif // ! INCLUDE_RCF_TOOLS_HPP