Overview Tutorial API reference Examples Build Download ZmqMessage 0.1 - 21 Oct 2011

include/ZmqTools.hpp

Go to the documentation of this file.
00001 
00008 #ifndef ZMQMESSAGE_ZMQ_TOOLS_INCLUDED_
00009 #define ZMQMESSAGE_ZMQ_TOOLS_INCLUDED_
00010 
00011 #include <cstdlib>
00012 #include <ctime>
00013 
00014 #include <string>
00015 #include <sstream>
00016 
00017 #include "zmqmessage/Config.hpp"
00018 #include "zmqmessage/MetaTypes.hpp"
00019 #include "zmqmessage/RawMessage.hpp"
00020 
00021 namespace ZmqMessage
00022 {
00029   template <typename T>
00030   inline
00031   void
00032   get_bin(zmq::message_t& message, T& t)
00033   {
00034     t = *(reinterpret_cast<T*>(message.data()));
00035   }
00036 
00042   template <typename T>
00043   inline T
00044   get_bin(zmq::message_t& message)
00045   {
00046     return *(reinterpret_cast<T*>(message.data()));
00047   }
00048 
00056   template <typename T>
00057   inline
00058   T
00059   get(zmq::message_t& message,
00060     typename Private::EnableIf<Private::IsStr<T>::value>::type* = 0)
00061   {
00062     return T(static_cast<char*>(message.data()), message.size());
00063   }
00064 
00073   template <typename T>
00074   inline
00075   T
00076   get(zmq::message_t& message,
00077     typename Private::DisableIf<Private::IsStr<T>::value>::type* = 0,
00078     typename Private::DisableIf<Private::IsRaw<T>::value>::type* = 0)
00079   {
00080     std::stringstream ss;
00081     ss.write(static_cast<char*>(message.data()), message.size());
00082     T t;
00083     ss >> t;
00084     return t;
00085   }
00086 
00093   template <typename T>
00094   inline
00095   T
00096   get(zmq::message_t& message,
00097     typename Private::DisableIf<Private::IsStr<T>::value>::type* = 0,
00098     typename Private::EnableIf<Private::IsRaw<T>::value>::type* = 0)
00099   {
00100     return get_bin<T>(message);
00101   }
00102 
00110   template <typename T>
00111   inline
00112   void
00113   get(zmq::message_t& message, T& t,
00114     typename Private::EnableIf<Private::IsStr<T>::value>::type* = 0)
00115   {
00116     t = T(static_cast<char*>(message.data()), message.size());
00117   }
00118 
00127   template <typename T>
00128   inline
00129   void
00130   get(zmq::message_t& message, T& t,
00131     typename Private::DisableIf<Private::IsStr<T>::value>::type* = 0,
00132     typename Private::DisableIf<Private::IsRaw<T>::value>::type* = 0)
00133   {
00134     std::stringstream ss;
00135     ss.write(static_cast<char*>(message.data()), message.size());
00136     ss >> t;
00137   }
00138 
00145   template <typename T>
00146   inline
00147   void
00148   get(zmq::message_t& message, T& t,
00149     typename Private::DisableIf<Private::IsStr<T>::value>::type* = 0,
00150     typename Private::EnableIf<Private::IsRaw<T>::value>::type* = 0)
00151   {
00152     get_bin(message, t);
00153   }
00154 
00159   template <typename T>
00160   inline
00161   T
00162   get(zmq::message_t& message, bool binary_mode)
00163   {
00164     return binary_mode ? get_bin<T>(message) : get<T>(message);
00165   }
00166 
00172   template <typename T>
00173   inline
00174   void
00175   get(zmq::message_t& message, T& t,  bool binary_mode)
00176   {
00177     binary_mode ? get_bin(message, t) : get(message, t);
00178   }
00179 
00183   inline RawMessage
00184   get_raw(zmq::message_t& message)
00185   {
00186     return RawMessage(message.data(), message.size());
00187   }
00188 
00192   template <typename T>
00193   inline T
00194   get_string(zmq::message_t& message)
00195   {
00196     return T(static_cast<char*>(message.data()), message.size());
00197   }
00198 
00202   inline ZMQMESSAGE_STRING_CLASS
00203   get_string(zmq::message_t& message)
00204   {
00205     return get_string<ZMQMESSAGE_STRING_CLASS>(message);
00206   }
00207 
00213   template <typename T>
00214   inline T
00215   get_string(zmq::message_t& message, size_t limit)
00216   {
00217     return T(
00218       static_cast<char*>(message.data()), std::min(message.size(), limit)
00219     );
00220   }
00221 
00225   inline ZMQMESSAGE_STRING_CLASS
00226   get_string(zmq::message_t& message, size_t limit)
00227   {
00228     return get_string<ZMQMESSAGE_STRING_CLASS>(message, limit);
00229   }
00230 
00234   time_t
00235   get_time(zmq::message_t& message);
00236 
00241   inline int
00242   msgcmp(zmq::message_t& message, const char* str, size_t len)
00243   {
00244     int ret = memcmp(message.data(), str, std::min(message.size(), len));
00245     return ret ? ret : message.size() - len;
00246   }
00247 
00252   inline int
00253   msgcmp(zmq::message_t& message, const char* str)
00254   {
00255     return msgcmp(message, str, strlen(str));
00256   }
00257 
00262   template <typename T>
00263   inline int
00264   msgcmp(zmq::message_t& message, const T& str,
00265     typename Private::EnableIf<Private::IsStr<T>::value>::type* = 0)
00266   {
00267     return msgcmp(message, str.data(), str.length());
00268   }
00269 
00275   void
00276   init_msg(const void* t, size_t sz, zmq::message_t& msg);
00277 
00282   template <class T>
00283   inline void
00284   init_msg(const T& t, zmq::message_t& msg,
00285     typename Private::EnableIf<Private::IsStr<T>::value>::type* = 0)
00286   {
00287     init_msg(t.data(), t.length(), msg);
00288   }
00289 
00290   /*
00291    * Initialize zmq message by arbitrary (non-string) type that can be
00292    * written to output stream.
00293    */
00294   template <class T>
00295   inline void
00296   init_msg(const T& t, zmq::message_t& msg,
00297            typename Private::DisableIf<Private::IsStr<T>::value>::type* = 0,
00298            typename Private::DisableIf<Private::IsRaw<T>::value>::type* = 0)
00299   {
00300     std::ostringstream os;
00301     os << t;
00302     const std::string& s = os.str();
00303     init_msg(s, msg);
00304   }
00305 
00306   /*
00307    * Initialize zmq message by arbitrary (non-string) type to send
00308    * in binary form.
00309    * For types explicitly marked as raw (containing raw_mark typedef)
00310    */
00311   template <class T>
00312   inline void
00313   init_msg(const T& t, zmq::message_t& msg,
00314            typename Private::DisableIf<Private::IsStr<T>::value>::type* = 0,
00315            typename Private::EnableIf<Private::IsRaw<T>::value>::type* = 0)
00316   {
00317     init_msg(&t, sizeof(T), msg);
00318   }
00319 
00323   inline void
00324   init_msg(const char* t, zmq::message_t& msg)
00325   {
00326     init_msg(t, strlen(t), msg);
00327   }
00328 
00333   template <class T>
00334   inline void
00335   init_msg_bin(const T& t, zmq::message_t& msg)
00336   {
00337     init_msg(&t, sizeof(T), msg);
00338   }
00339 
00345   template <class T>
00346   inline void
00347   init_msg(const T& t, zmq::message_t& msg, bool binary_mode)
00348   {
00349     binary_mode ? init_msg_bin(t, msg) : init_msg(t, msg);
00350   }
00351 
00352 
00357   inline void
00358   throw_zmq_exception(const zmq::error_t& e)
00359     throw(ZmqErrorType)
00360   {
00361 #ifdef ZMQMESSAGE_WRAP_ZMQ_ERROR
00362     throw ZmqErrorType(e.what());
00363 #else
00364     throw e;
00365 #endif
00366   }
00367 
00371   inline void
00372   recv_msg(zmq::socket_t& sock, zmq::message_t& msg,
00373     int flags = 0) throw(ZmqErrorType)
00374   {
00375     try
00376     {
00377       if (!sock.recv(&msg, flags))
00378       {
00379         throw zmq::error_t();
00380       }
00381     }
00382     catch (const zmq::error_t& e)
00383     {
00384       throw_zmq_exception(e);
00385     }
00386   }
00387 
00392   inline bool
00393   try_recv_msg(zmq::socket_t& sock, zmq::message_t& msg,
00394     int flags = ZMQ_NOBLOCK) throw(ZmqErrorType)
00395   {
00396     bool res = false;
00397     try
00398     {
00399       res = sock.recv(&msg, flags);
00400     }
00401     catch (const zmq::error_t& e)
00402     {
00403       throw_zmq_exception(e);
00404     }
00405     return res;
00406   }
00407 
00411   inline void
00412   send_msg(zmq::socket_t& sock, zmq::message_t& msg, int flags)
00413     throw(ZmqErrorType)
00414   {
00415     try
00416     {
00417       if (!sock.send(msg, flags))
00418       {
00419         throw zmq::error_t();
00420       }
00421     }
00422     catch (const zmq::error_t& e)
00423     {
00424       throw_zmq_exception(e);
00425     }
00426   }
00427 
00431   inline void
00432   copy_msg(zmq::message_t& target, zmq::message_t& source)
00433     throw(ZmqErrorType)
00434   {
00435     try
00436     {
00437       target.copy(&source);
00438     }
00439     catch (const zmq::error_t& e)
00440     {
00441       throw_zmq_exception(e);
00442     }
00443   }
00444 
00448   bool
00449   has_more(zmq::socket_t& sock);
00450 
00460   int
00461   relay_raw(zmq::socket_t& src, zmq::socket_t& dst,
00462     bool check_first_part = true);
00463 }
00464 
00465 
00466 #endif //ZMQMESSAGE_ZMQ_TOOLS_INCLUDED_
00467 

ZmqMessage 0.1 — open source software, support@zmqmessage.org