Search code examples
c++rubypure-virtualruby-rice

pure virtual c++ class wrapped in rice/ruby raises a TypeError ("is not a class (Module)") at runtime


I am in the process of wrapping a C++ library called essentia as a ruby extension using the rice-ruby wrapper.

In this library, there are two pure virtual classes called essentia::standard::Algorithm and essentia::streaming::Algorithm. I have followed the instructions and created the following code:

algorithm.hpp

#if !defined(_RICE_ESSENTIA_ALGORITHM_HPP_)
# define _RICE_ESSENTIA_ALGORITHM_HPP_

#include "essentia/algorithm.h"
#include "essentia/streaming/streamingalgorithm.h"

#include "rice/Director.hpp"

namespace Rice
{
  namespace Essentia
  {
    namespace Standard
    {
      class AlgorithmProxy : public essentia::standard::Algorithm, public Rice::Director
      {

        public:
        AlgorithmProxy(Rice::Object self) : Rice::Director(self) {}

        virtual void compute()
        {
          getSelf().call("compute");
        }

        void default_compute()
        {
          raisePureVirtual();
        }

        virtual void reset()
        {
          getSelf().call("reset");
        }

        void default_reset()
        {
          essentia::standard::Algorithm::reset();
        }

        virtual void declareParameters()
        {
          getSelf().call("declare_parameters");
        }

        void default_declareParameters()
        {
          raisePureVirtual();
        }
      };

      void install_algorithm();
    }

    namespace Streaming
    {
      class AlgorithmProxy : public essentia::streaming::Algorithm, public Rice::Director
      {

        public:
        AlgorithmProxy(Rice::Object self) : Rice::Director(self) {}

        virtual essentia::streaming::AlgorithmStatus process()
        {
          return from_ruby<essentia::streaming::AlgorithmStatus>(getSelf().call("process"));
        }

        essentia::streaming::AlgorithmStatus default_process()
        {
          raisePureVirtual();
          return essentia::streaming::AlgorithmStatus::FINISHED;
        }

        virtual void reset()
        {
          getSelf().call("reset");
        }

        void default_reset()
        {
          essentia::streaming::Algorithm::reset();
        }

        virtual void shouldStop(bool stop)
        {
          getSelf().call("should_stop");
        }

        void default_shouldStop(bool stop)
        {
          essentia::streaming::Algorithm::shouldStop(stop);
        }

        virtual bool shouldStop() const
        {
          return from_ruby<bool>(getSelf().call("should_stop?"));
        }

        bool default_shouldStop() const
        {
          return essentia::streaming::Algorithm::shouldStop();
        }

        virtual void declareParameters()
        {
          getSelf().call("declare_parameters");
        }

        void default_declareParameters()
        {
          raisePureVirtual();
        }
      };

      void install_algorithm();
    }
  }
}

#endif /* !defined(_RICE_ESSENTIA_ALGORITHM_HPP_) */

and

algorithm.cpp

#include "rice/Data_Type.hpp"
#include "rice/Enum.hpp"
#include "rice/Constructor.hpp"

#include "exception.hpp"
#include "modules.hpp"
#include "algorithm.hpp"

namespace Rice
{
  namespace Essentia
  {
    namespace Standard
    {

      static Rice::Data_Type<essentia::standard::Algorithm> standard_algorithm_type;

      void
      install_algorithm()
      {
         RUBY_TRY
         {
           standard_algorithm_type =
             define_class_under<essentia::standard::Algorithm>(essentia_standard_module(), "Algorithm")
             .define_director<AlgorithmProxy>()
             .define_constructor(Rice::Constructor<AlgorithmProxy, Rice::Object>())
             .add_handler<essentia::EssentiaException>(handle_essentia_exception)
             .define_method("reset", &AlgorithmProxy::default_reset)
             .define_method("compute", &AlgorithmProxy::default_compute)
             .define_method("input_names", &AlgorithmProxy::inputNames)
             .define_method("output_names", &AlgorithmProxy::outputNames)
             .define_method("input_types", &AlgorithmProxy::inputTypes)
             .define_method("output_types", &AlgorithmProxy::outputTypes)
             .define_method("declare_parameters", &AlgorithmProxy::default_declareParameters)
             ;
         }
         RUBY_CATCH
      }

    }

    namespace Streaming
    {

      static Rice::Enum<essentia::streaming::AlgorithmStatus> algorithm_status_type;

      void
      install_algorithm_status()
      {
        algorithm_status_type =
          define_enum<essentia::streaming::AlgorithmStatus>("AlgorithmStatus", essentia_streaming_module())
          .define_value("OK", essentia::streaming::AlgorithmStatus::OK)
          .define_value("CONTINUE", essentia::streaming::AlgorithmStatus::CONTINUE)
          .define_value("PASS", essentia::streaming::AlgorithmStatus::PASS)
          .define_value("FINISHED", essentia::streaming::AlgorithmStatus::FINISHED)
          .define_value("NO_INPUT", essentia::streaming::AlgorithmStatus::NO_INPUT)
          .define_value("NO_OUTPUT", essentia::streaming::AlgorithmStatus::NO_OUTPUT)
          ;
      }

      static Rice::Data_Type<essentia::streaming::Algorithm> streaming_algorithm_type;
      typedef void (AlgorithmProxy::*set_should_stop)(bool);
      typedef bool (AlgorithmProxy::*get_should_stop)(void) const;

      void
      install_algorithm()
      {
         RUBY_TRY
         {
           streaming_algorithm_type =
             define_class_under<essentia::streaming::Algorithm>(essentia_streaming_module(), "Algorithm")
             .define_director<AlgorithmProxy>()
             .define_constructor(Rice::Constructor<AlgorithmProxy, Rice::Object>())
             .add_handler<essentia::EssentiaException>(handle_essentia_exception)
             .define_method("reset", &AlgorithmProxy::default_reset)
             .define_method("input_names", &AlgorithmProxy::inputNames)
             .define_method("output_names", &AlgorithmProxy::outputNames)
             .define_method("should_stop", set_should_stop(&AlgorithmProxy::default_shouldStop))
             .define_method("should_stop?", get_should_stop(&AlgorithmProxy::default_shouldStop))
             .define_method("disconnect_all", &AlgorithmProxy::disconnectAll)
             .define_method("process", &AlgorithmProxy::default_process)
             .define_method("declare_parameters", &AlgorithmProxy::default_declareParameters)
             ;
           install_algorithm_status();
         }
         RUBY_CATCH
      }

    }
  }
}

while my Init_ code is as follows:

init.cpp

#include "modules.hpp"
#include "setup.hpp"
#include "types.hpp"
#include "exception.hpp"
#include "algorithm.hpp"
#include "io.hpp"

extern "C" {

  void Init_essentia_ruby_wrap()
  {
    Rice::Essentia::create_essentia_modules();
    Rice::Essentia::install_essentia_types();
    Rice::Essentia::setup_essentia();
    Rice::Essentia::Standard::install_io();
    Rice::Essentia::Standard::install_algorithm();
    Rice::Essentia::Streaming::install_algorithm();
  }

}

Everything compiles fine (using clang++ -std=c++1y) but then, when I try to run the produced code I get:

eeepc-1215B:.../essentia-ruby$ ruby -I./lib/essentia -e "require 'essentia_ruby_wrap'"
/home/nicb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': Essentia::Streaming::Algorithm is not a class (Module) (TypeError)
    from /home/nicb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

I can't get what's wrong in the code, and I've looked everywhere for an answer to no avail.

P.S. my full code is here


Solution

  • SOLVED

    As I thought, it was a really stupid thing (of me).

    I stumbled upon the solution by looking at the ruby code that produced the error:

    class.c (lines 641-660)

    642 VALUE
    643 rb_define_class(const char *name, VALUE super)
    644 {
    645     VALUE klass;
    646     ID id;
    647 
    648     id = rb_intern(name);
    649     if (rb_const_defined(rb_cObject, id)) {
    650     klass = rb_const_get(rb_cObject, id);
    651     if (!RB_TYPE_P(klass, T_CLASS)) {
    652         rb_raise(rb_eTypeError, "%s is not a class (%"PRIsVALUE")",
    653              name, rb_obj_class(klass));
    654     }
    655     if (rb_class_real(RCLASS_SUPER(klass)) != super) {
    656         rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
    657     }
    658     return klass;
    659     }
    

    and the documentation written immediately above that says:

    throws a TypeError if the constant name is already taken but
    the constant is not a Class.
    

    and it came to my mind that, in trying to mimick with ruby conventions the directory tree of essentia (thus building a module for each directory) I had already created a module called Essentia::Streaming::Algorithm and thus there could have been no class with the same name.

    I could say that I was mislead by a number of different issues that conjured up but I will avoid that: it was just very stupid of me to forget that I had coded that module some days ago - no excuses. The lesson learned is that ruby will not allow a module and a class with the same name, and will just pick up whoever comes first. That's completely logical and that's it.