Search code examples
c++thrift

Apache thrift and cpp code generation


I am following the tutorial for apache thrift:

https://svn.apache.org/repos/asf/thrift/attic/branches/0.9.x/tutorial/tutorial.thrift

It uses the following shared.thrift:

https://svn.apache.org/repos/asf/thrift/attic/branches/0.9.x/tutorial/shared.thrift

I generate the required cpp source files via:

thrift --gen cpp shared.thrift
thrift --gen cpp tutorial.thrift

it gives me a list of cpp files, and in one of them I see the following:

class CalculatorHandler : virtual public CalculatorIf {
  ... }

where

class CalculatorIf : virtual public  ::shared::SharedServiceIf {
... }

and

 class SharedServiceIf {
 public:
  virtual ~SharedServiceIf() {}
  virtual void getStruct(SharedStruct& _return, const int32_t key) = 0;
};

This is not compiling due to the fact that virtual void getStruct is a pure virtual in the class, but it is defined in:

class SharedServiceHandler : virtual public SharedServiceIf {
  void getStruct(SharedStruct& _return, const int32_t key) {
   // Your implementation goes here
   printf("getStruct\n");
  }
}

This is the compilation error:

Calculator_server.skeleton.cpp:49:63: error: cannot allocate an object of abstract type 'CalculatorHandler'
   shared_ptr<CalculatorHandler> handler(new CalculatorHandler());
                                                               ^
Calculator_server.skeleton.cpp:19:7: note:   because the following virtual functions are pure within 'CalculatorHandler':
 class CalculatorHandler : virtual public CalculatorIf {
       ^
In file included from Calculator.h:12:0,
                 from Calculator_server.skeleton.cpp:4:
SharedService.h:18:16: note:    virtual void shared::SharedServiceIf::getStruct(shared::SharedStruct&, int32_t)
   virtual void getStruct(SharedStruct& _return, const int32_t key) = 0;

So here comes the question: is this a bug in thrift's CPP code generator, that it cannot correctly identify the required base class or I am doing something wrongly?

(This question is NOT about fixing the C++ compilation errors since this is ALL generated code by thrift. This question is about thrift)


Solution

  • Yes, the skeletons have a problem with service inheritance. If you compare that code against the code in the /tutorial/cpp folder of the source tree you will see some notable differences.

    I hesitate a bit to advise against using the skeleton code at all, but that's probably what most people really do. They use the source tree tutorial and the Test Suite code as a reference. In fact, C++ is the only target language where skeletons are generated at all. I think that fact alone tells a lot.