K
K
KarlKremen2017-07-03 15:59:47
ruby
KarlKremen, 2017-07-03 15:59:47

How to get from Rice::Object Rice::Struct::Instance?

There is this code:

Lots of code
#include "library.hpp"
#include <rice/Module.hpp>
#include <rice/Struct.hpp>
#include <rice/Data_Type.hpp>
#include <rice/Constructor.hpp>
#include "CIEDE2K.hpp"
#include "ImagePNG.hpp"

using namespace std;
using namespace png;
using namespace Rice;
using namespace CIEDE2K;

#define I76R ImagePNG < CIE76RGB::Comparator >
#define IDE2K ImagePNG < CIEDE2K::Comparator >

Module rb_mImage;
Struct rb_sPixel;
Data_Type < I76R > rb_cImagePNG76R;
Data_Type < IDE2K > rb_cImagePNGDE2K;

template < >
Object to_ruby < px >(const px &_px)
{
  Array args;
  args.push((uint) _px.red);
  args.push((uint) _px.green);
  args.push((uint) _px.blue);
  args.push((uint) _px.alpha);

  return rb_sPixel.new_instance(args);
}


template < >
px from_ruby < px >(Struct::Instance s_px)
{
  ubyte rgba[4] = {from_ruby < ubyte >(s_px["r"]), from_ruby < ubyte >(s_px["g"]), from_ruby < ubyte >(s_px["b"]),
                   from_ruby < ubyte >(s_px["a"])};
  return px(rgba[0], rgba[1], rgba[2], rgba[3]);
}

typedef void (I76R::*save_s76R)(std::string);

typedef void (I76R::*save_u76R)( );

typedef double (I76R::*compare_dif76R)(I76R);

typedef bool (I76R::*compare_eql76R)(I76R, double);

typedef double (I76R::*_find76R)(I76R);


typedef void (IDE2K::*save_sDE2K)(std::string);


typedef void (IDE2K::*save_uDE2K)( );

typedef double (IDE2K::*compare_difDE2K)(IDE2K);

typedef bool (IDE2K::*compare_eqlDE2K)(IDE2K, double);

typedef double (IDE2K::*_findDE2K)(IDE2K);


extern "C"
void Init_imglib( )
{
  rb_mImage = define_module("Module");

  rb_sPixel
      .initialize(rb_mImage, "Pixel")
      .define_member("r")
      .define_member("g")
      .define_member("b")
      .define_member("a");

  rb_cImagePNG76R
      = rb_mImage.define_class < I76R >("CIE76RGB");

  rb_cImagePNG76R
      .define_constructor(Constructor < I76R, string >())
      .define_constructor(Constructor < I76R, uint, uint >())

      .define_method("save", (&I76R::saveAs))
      .define_method("save!", (&I76R::save))

      .define_method("[]", &I76R::getPixel)
      .define_method("get_pixel", &I76R::getPixel)

      .define_method("[]=", &I76R::setPixel)
      .define_method("set_pixel", &I76R::setPixel)

      .define_method("diff", (&I76R::compare))
      .define_method("equal?", (&I76R::compare))

      .define_method("find", _find76R(&I76R::find))

      .define_method("crop", &I76R::crop)

      .define_method("width", &I76R::width)
      .define_method("height", &I76R::height);

  rb_cImagePNGDE2K
      = rb_mImage.define_class < IDE2K >("CIEDE2K");

  rb_cImagePNGDE2K
      .define_constructor(Constructor < IDE2K, string >())
      .define_constructor(Constructor < IDE2K, uint, uint >())

      .define_method("save", (&IDE2K::saveAs))
      .define_method("save!", (&IDE2K::save))

      .define_method("[]", &IDE2K::getPixel)
      .define_method("get_pixel", &IDE2K::getPixel)

      .define_method("[]=", &IDE2K::setPixel)
      .define_method("set_pixel", &IDE2K::setPixel)

      .define_method("diff", (&IDE2K::compare))
      .define_method("equal?", (&IDE2K::compare))

      .define_method("find", _findDE2K(&IDE2K::find))

      .define_method("crop", &IDE2K::crop)

      .define_method("width", &IDE2K::width)
      .define_method("height", &IDE2K::height);


}

#include "ImagePNG.cpp"

The from_ruby <px> function takes a Rice::Object which is an ancestor of Rice::Struct::Instance. If I specify the Rice::Struct::Instance parameter type, it writes:
library.cpp:44:4: error: template-id 'from_ruby<px>' for 'px from_ruby(Rice::Struct::Instance)' does not match any template declaration

If I specify the Rice::Object type, it gives me something really wild:
library.cpp: In function 'typename Rice::detail::from_ruby_<T>::Retval_T from_ruby(Rice::Object) [with T = png::basic_rgba_pixel<unsigned char>; typename Rice::detail::from_ruby_<T>::Retval_T = png::basic_rgba_pixel<unsigned char>]':
library.cpp:46:47: error: conversion from 'Rice::Object' to 'long long int' is ambiguous
library.cpp:44:4: note: candidates are:
In file included from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rice-2.1.1/ruby/lib/include/rice/Module_defn.hpp:4:0,
                 from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rice-2.1.1/ruby/lib/include/rice/Module.hpp:4,
                 from library.cpp:10:
C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rice-2.1.1/ruby/lib/include/rice/Object_defn.hpp:50:3: note: Rice::Object::operator VALUE() const
C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rice-2.1.1/ruby/lib/include/rice/Object_defn.hpp:44:3: note: Rice::Object::operator bool() const

And yes, it doesn't cast Object to Struct::Instance

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question