libtasks Documentation  1.6
thrift_server_writer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 ADTECH GmbH
3  * Licensed under MIT (https://github.com/adtechlabs/libtasks/blob/master/COPYING)
4  *
5  * Author: Andreas Pohl
6  */
7 
8 #ifndef _THRIFT_SERVER_WRITER_H_
9 #define _THRIFT_SERVER_WRITER_H_
10 
11 #include <string>
12 #include <boost/shared_ptr.hpp>
13 #include <thrift/protocol/TProtocol.h>
14 
15 namespace tasks {
16 namespace tools {
17 
18 template <class data_type, class transport_type, class protocol_type>
20  public:
21  thrift_server_writer(std::string name, boost::shared_ptr<transport_type> t, boost::shared_ptr<protocol_type> p)
22  : m_name(name), m_transport(t), m_protocol(p) {}
23 
24  inline void write(data_type& d) {
25  using namespace apache::thrift::protocol;
26  m_protocol->writeMessageBegin(m_name, T_REPLY, 0);
27  m_protocol->writeStructBegin("result");
28  m_protocol->writeFieldBegin("success", T_STRUCT, 0);
29  d.write(m_protocol.get());
30  m_protocol->writeFieldEnd();
31  m_protocol->writeFieldStop();
32  m_protocol->writeStructEnd();
33  m_protocol->writeMessageEnd();
34  m_transport->writeEnd();
35  m_transport->flush();
36  }
37 
38  private:
39  std::string m_name;
40  boost::shared_ptr<transport_type> m_transport;
41  boost::shared_ptr<protocol_type> m_protocol;
42 };
43 
44 } // tools
45 } // tasks
46 
47 #endif // _THRIFT_SERVER_WRITER_H_
boost::shared_ptr< protocol_type > m_protocol
boost::shared_ptr< transport_type > m_transport
thrift_server_writer(std::string name, boost::shared_ptr< transport_type > t, boost::shared_ptr< protocol_type > p)