Sha256: 808e8d4377780072e566cf5a9e812de7debd95cbf83c223c6851cc92df5ecd54

Contents?: true

Size: 1.84 KB

Versions: 29

Compression:

Stored size: 1.84 KB

Contents

#ifndef Rice__detail__Caster__hpp_
#define Rice__detail__Caster__hpp_

#include "../Module.hpp"
#include <stdexcept>

namespace Rice
{

namespace detail
{

class Abstract_Caster
{
public:
  virtual void * cast_to_base(void * derived, Module type) const = 0;
  virtual ~Abstract_Caster() { }
};

template<typename Derived_T, typename Base_T>
class Caster
  : public Abstract_Caster
{
public:
  Caster(Abstract_Caster * base_caster, Module type)
    : base_caster_(base_caster)
    , type_(type)
  {
  }

protected:
  virtual void * cast_to_base(void * derived, Module type) const
  {
    if(type.value() == type_.value())
    {
      Derived_T * d(static_cast<Derived_T *>(derived));
      return static_cast<Base_T *>(d);
    }
    else
    {
      if(base_caster_)
      {
        return base_caster_->cast_to_base(derived, type);
      }
      else
      {
        std::string s = "bad cast. No caster found for ";
        s += type_.name().str();
        throw std::runtime_error(s);
      }
    }
  }

private:
  Abstract_Caster * base_caster_;
  Module type_;
};

template<typename To_T, typename From_T>
class Implicit_Caster
  : public Abstract_Caster
{
public:
  Implicit_Caster(Abstract_Caster * base_caster, Module type)
    : base_caster_(base_caster)
    , type_(type)
  {
  }

protected:
  virtual void * cast_to_base(void * derived, Module type) const
  {
    if(type.value() == type_.value())
    {
      return new To_T( *static_cast<From_T*>(derived) );
    }
    else
    {
      if(base_caster_)
      {
        return base_caster_->cast_to_base(derived, type);
      }
      else
      {
        std::string s = "bad cast. No implicit caster found for ";
        s += type_.name().str();
        throw std::runtime_error(s);
      }
    }
  }

private:
  Abstract_Caster * base_caster_;
  Module type_;
};



} // detail

} // Rice

#endif //Rice__detail__Caster__hpp_

Version data entries

29 entries across 29 versions & 5 rubygems

Version Path
rice-3.0.0 rice/detail/Caster.hpp
rice2-2.2.1 rice/detail/Caster.hpp
rice2-2.2.0 rice/detail/Caster.hpp
rice-2.2.0 rice/detail/Caster.hpp
rice-2.1.3 rice/detail/Caster.hpp
rice-2.1.2 rice/detail/Caster.hpp
rice-2.1.1 rice/detail/Caster.hpp
rice-2.1.0 rice/detail/Caster.hpp
rice-2.0.0 rice/detail/Caster.hpp
rice-1.7.0 rice/detail/Caster.hpp
rice-1.6.3 rice/detail/Caster.hpp
rice-1.6.2 rice/detail/Caster.hpp
rice-1.6.1 rice/detail/Caster.hpp
rice-1.6.0 rice/detail/Caster.hpp
rice-1.6.0.pre rice/detail/Caster.hpp
rice-1.5.3 rice/detail/Caster.hpp
rice-1.5.2 rice/detail/Caster.hpp
keyme-rice-1.5.1.keyme1 rice/detail/Caster.hpp
keyme-rice-1.5.1.keyme rice/detail/Caster.hpp
rice-1.5.1 rice/detail/Caster.hpp