RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
common/messages.h
00001 /* messages.h                                                      -*- C++ -*-
00002    Jeremy Barnes, 31 May 2012
00003    Copyright (c) 2012 Datacratic.  All rights reserved.
00004 
00005    Functions to aid in messaging.
00006 */
00007 
00008 #ifndef __router__messages_h__
00009 #define __router__messages_h__
00010 
00011 #include "soa/service/zmq.hpp"
00012 
00013 #include "rtbkit/common/json_holder.h"
00014 
00015 namespace Datacratic {
00016 
00017 inline zmq::message_t encodeMessage(const Id & id)
00018 {
00019     return id.toString();
00020 }
00021 
00022 } // namespace Datacratic
00023 
00024 namespace RTBKIT {
00025 
00026 inline zmq::message_t encodeMessage(const JsonHolder & j)
00027 {
00028     return j.toString();
00029 }
00030 
00031 inline int toInt(const std::string & str)
00032 {
00033     char * end;
00034     const char * start = str.c_str();
00035     int result = strtol(start, &end, 10);
00036     if (end != start + str.length())
00037         throw ML::Exception("couldn't parse int");
00038     return result;
00039 }
00040 
00041 inline long toLong(const std::string & str)
00042 {
00043     char * end;
00044     const char * start = str.c_str();
00045     long result = strtol(start, &end, 10);
00046     if (end != start + str.length())
00047         throw ML::Exception("couldn't parse int");
00048     return result;
00049 }
00050 
00051 inline double toDouble(const std::string & str)
00052 {
00053     char * end;
00054     const char * start = str.c_str();
00055     double result = strtod(start, &end);
00056     if (end != start + str.length())
00057         throw ML::Exception("couldn't parse double");
00058     return result;
00059 }
00060 
00061 inline std::string toString(double d)
00062 {
00063     return ML::format("%f", d);
00064 }
00065 
00066 inline std::string toString(int i)
00067 {
00068     return ML::format("%i", i);
00069 }
00070 
00071 inline std::string toString(signed long l)
00072 {
00073     return ML::format("%li", l);
00074 }
00075 
00076 inline std::string toString(unsigned long l)
00077 {
00078     return ML::format("%ld", l);
00079 }
00080 
00081 } // namespace RTBKIT
00082 
00083 #endif /* __router__messages_h__ */
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator