<commit_before>module GirFFI
  # The InOutPointer class handles conversion between ruby types and
  # pointers for arguments with direction :inout and :out.
  #
  # TODO: This has now become a more general extende pointer class and should be renamed.
  class InOutPointer < FFI::Pointer
    attr_reader :value_type

    def initialize type, ptr = nil
      @value_type = type

      ptr ||= AllocationHelper.safe_malloc(value_type_size)
      super ptr
    end

    private :initialize

    # TODO: Rename to get_value
    def to_value
      case value_ffi_type
      when Class
        value_ffi_type.get_value_from_pointer(self)
      when Symbol
        self.send("get_#{value_ffi_type}", 0)
      else
        raise NotImplementedError
      end
    end

    def set_value value
      value = adjust_value_in value
      case value_ffi_type
      when Class
        value_ffi_type.copy_value_to_pointer(value, self)
      when Symbol
        self.send "put_#{value_ffi_type}", 0, value
      else
        raise NotImplementedError
      end
    end

    def value_ffi_type
      @value_ffi_type ||= TypeMap.type_specification_to_ffitype value_type
    end

    def value_type_size
      @value_type_size ||= FFI.type_size value_ffi_type
    end

    def self.for type
      self.new(type).tap {|ptr| ptr.set_value nil}
    end

    def self.from type, value
      self.new(type).tap {|ptr| ptr.set_value value}
    end

    private

    def adjust_value_in value
      case value_type
      when :gboolean
        value
      else
        value || nil_value
      end
    end

    def nil_value
      value_ffi_type == :pointer ? nil : 0
    end
  end
end<commit_msg>Simplify implementation of InOutPointer.for<commit_after>module GirFFI
  # The InOutPointer class handles conversion between ruby types and
  # pointers for arguments with direction :inout and :out.
  #
  # TODO: This has now become a more general extende pointer class and should be renamed.
  class InOutPointer < FFI::Pointer
    attr_reader :value_type

    def initialize type, ptr = nil
      @value_type = type

      ptr ||= AllocationHelper.safe_malloc(value_type_size)
      super ptr
    end

    private :initialize

    # TODO: Rename to get_value
    def to_value
      case value_ffi_type
      when Class
        value_ffi_type.get_value_from_pointer(self)
      when Symbol
        self.send("get_#{value_ffi_type}", 0)
      else
        raise NotImplementedError
      end
    end

    def set_value value
      case value_ffi_type
      when Class
        value_ffi_type.copy_value_to_pointer(value, self)
      when Symbol
        self.send "put_#{value_ffi_type}", 0, value
      else
        raise NotImplementedError
      end
    end

    def clear
      set_value nil_value
    end

    def value_ffi_type
      @value_ffi_type ||= TypeMap.type_specification_to_ffitype value_type
    end

    def value_type_size
      @value_type_size ||= FFI.type_size value_ffi_type
    end

    def self.for type
      self.new(type).tap {|ptr| ptr.clear}
    end

    def self.from type, value
      self.new(type).tap {|ptr| ptr.set_value value}
    end

    private

    def nil_value
      value_ffi_type == :pointer ? nil : 0
    end
  end
end
{'fraction_non_alphanumeric': 0.05660377358490566, 'fraction_numerical': 0.0018558614290133002, 'mean_word_length': 2.4440894568690097, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5911584', 'n_tokens_mistral': 1042, 'n_tokens_neox': 1005, 'n_words': 286}