RCFProto

- Main Page
- Classes
- Files

- File List

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 <iosfwd>
26 #include <iterator>
27 #include <stdexcept>
28 #include <typeinfo>
29 #include <vector>
30
31 #include <boost/config.hpp>
32 #include <boost/shared_ptr.hpp>
33
34 #include <RCF/Export.hpp>
35 #include <RCF/util/UnusedVariable.hpp>
36
37 // Logging mechanism
38 #include <RCF/util/Log.hpp>
39
40 namespace RCF {
41 static const int LogNameRcf = 1;
42 static const int LogLevel_1 = 1; // Error and exceptions.
43 static const int LogLevel_2 = 2; // Larger scale setup/teardown.
44 static const int LogLevel_3 = 3; // Messages sent and received (RCF level), RCF client and session lifetime.
45 static const int LogLevel_4 = 4; // Messages sent and received (network level), network client and session lifetime.
46
47 } // namespace RCF
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 // Assertion mechanism
55 #include <RCF/util/Assert.hpp>
56 #define RCF_ASSERT(x) UTIL_ASSERT(x, RCF::AssertionFailureException(), RCF::LogNameRcf, RCF::LogLevel_1)
57
58 #define RCF_ASSERT_EQ(a,b) RCF_ASSERT(a == b)(a)(b)
59 #define RCF_ASSERT_NEQ(a,b) RCF_ASSERT(a != b)(a)(b)
60
61 #define RCF_ASSERT_LT(a,b) RCF_ASSERT(a < b)(a)(b)
62 #define RCF_ASSERT_LTEQ(a,b) RCF_ASSERT(a <= b)(a)(b)
63
64 #define RCF_ASSERT_GT(a,b) RCF_ASSERT(a > b)(a)(b)
65 #define RCF_ASSERT_GTEQ(a,b) RCF_ASSERT(a >= b)(a)(b)
66
67 // Throw mechanism
68 #include <RCF/util/Throw.hpp>
69 namespace RCF {
70 class Exception;
71 RCF_EXPORT ::util::DummyVariableArgMacroObject 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
75 // Verification mechanism
76 #define RCF_VERIFY(cond, e) if (cond); else RCF_THROW(e)
77
78
79 // Scope guard mechanism
80 #include <boost/multi_index/detail/scope_guard.hpp>
81
82 namespace RCF
83 {
84 class Exception;
85 }
86
87 // assorted tracing conveniences
88 namespace std {
89
90 // Trace std::vector
91 template<typename T>
92 std::ostream &operator<<(std::ostream &os, const std::vector<T> &v)
93 {
94 os << "(";
95 std::copy(v.begin(), v.end(), std::ostream_iterator<T>(os, ", "));
96 os << ")";
97 return os;
98 }
99
100 // Trace std::deque
101 template<typename T>
102 std::ostream &operator<<(std::ostream &os, const std::deque<T> &d)
103 {
104 os << "(";
105 std::copy(d.begin(), d.end(), std::ostream_iterator<T>(os, ", "));
106 os << ")";
107 return os;
108 }
109
110 // Trace type_info
111 RCF_EXPORT std::ostream &operator<<(std::ostream &os, const std::type_info &ti);
112
113 // Trace exception
114 RCF_EXPORT std::ostream &operator<<(std::ostream &os, const std::exception &e);
115
116 // Trace exception
117 RCF_EXPORT std::ostream &operator<<(std::ostream &os, const RCF::Exception &e);
118
119 } // namespace std
120
121 namespace RCF {
122
123 // null deleter, for use with for shared_ptr
124 class NullDeleter
125 {
126 public:
127 template<typename T>
128 void operator()(T)
129 {}
130 };
131
132 class SharedPtrIsNull
133 {
134 public:
135 template<typename T>
136 bool operator()(boost::shared_ptr<T> spt) const
137 {
138 return spt.get() == NULL;
139 }
140 };
141
142 } // namespace RCF
143
144 namespace RCF {
145
146
147
148 } // namespace RCF
149
150 // destructor try/catch blocks
151 #define RCF_DTOR_BEGIN \
152 try {
153
154 #define RCF_DTOR_END \
155 } \
156 catch (const std::exception &e) \
157 { \
158 if (!util::detail::uncaught_exception()) \
159 { \
160 throw; \
161 } \
162 else \
163 { \
164 RCF_LOG_1()(e); \
165 } \
166 }
167
168 //#if defined(_MSC_VER) && _MSC_VER < 1310
169 //#define RCF_PFTO_HACK long
170 //#else
171 //#define RCF_PFTO_HACK
172 //#endif
173 #define RCF_PFTO_HACK
174
175 // Auto linking on VC++
176 #ifdef _MSC_VER
177 #pragma comment(lib, "ws2_32.lib")
178 #pragma comment(lib, "mswsock.lib")
179 #pragma comment(lib, "advapi32.lib")
180 #pragma comment(lib, "user32.lib")
181 #pragma comment(lib, "crypt32.lib")
182 #endif
183
184 namespace RCF {
185
186 struct Void {};
187
188 template<typename Container, typename Element>
189 void eraseRemove(Container & container, const Element & element)
190 {
191 container.erase(
192 std::remove(
193 container.begin(),
194 container.end(),
195 element),
196 container.end());
197 }
198
199 RCF_EXPORT boost::uint64_t fileSize(const std::string & path);
200
201 } // namespace RCF
202
203 namespace boost {
204
205 template<typename T>
206 inline bool operator==(
207 const boost::weak_ptr<T> & lhs,
208 const boost::weak_ptr<T> & rhs)
209 {
210 return ! (lhs < rhs) && ! (rhs < lhs);
211 }
212
213 template<typename T>
214 inline bool operator!=(
215 const boost::weak_ptr<T> & lhs,
216 const boost::weak_ptr<T> & rhs)
217 {
218 return ! (lhs == rhs);
219 }
220
221 } // namespace boost
222
223 #endif // ! INCLUDE_RCF_TOOLS_HPP