GithubHelp home page GithubHelp logo

isabella232 / corefoundation Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chef/corefoundation

0.0 0.0 0.0 154 KB

FFI based Ruby bindings for the CoreFoundation frameworks

License: MIT License

Ruby 99.21% Shell 0.79%

corefoundation's Introduction

CoreFoundation

FFI based wrappers for a subset of core foundation: various bits of CFString, CFData, CFArray, CFDictionary are available.

Although the CF collection classes can store arbitrary pointer sized values this wrapper only supports storing CFTypes.

The CF namespace has the raw FFI generated method calls but it's usually easier to use the wrapper classes: CF::String, CF::Date, CF::Array, CF::Dictionary, CF::Boolean which try to present a rubyish view of the world (for example CF::Array implements Enumerable)

These implement methods for creating new instances from ruby objects (eg CF::String.from_string("hello world")) but you can also pass build them from an FFI::Pointer).

Setup

Add this to your Gemfile

gem 'corefoundation'

and run

bundle install

Usage

Preferences interface

  1. Add gem 'corefoundation', git: "https://github.com/chef/corefoundation.git" to your gemfile
  2. bundle install

Usage

Preferences interface

Preferences interface wraps CoreFoundation's preference utilities and provide functionality for managing preferences across domains.

domain = "NSGlobalDomain"
key = "com.apple.securitypref.logoutvalue"
value = 3150

# Getting preferences
CF::Preferences.get(key, domain)
CF::Preferences.get(key, domain, 'example_username', 'example.host')
CF::Preferences.get(key, domain, CF::Preferences::CURRENT_USER, CF::Preferences::CURRENT_HOST)

# Setting preferences
CF::Preferences.set(key, value, domain, 'example_username', 'example.host')
CF::Preferences.set(key, value, domain, CF::Preferences::ALL_USERS, CF::Preferences::ALL_HOSTS)

Converting

CF::Base objects has a to_ruby that creates a ruby object of the most approprite type (String for CF::String, Time for CF::Date, Integer or Float for CF::Number etc). The collection classes call to_ruby on their contents too.

In addition to the methods on the wrapper classes themselves, the ruby classes are extended with a to_cf method. Because CoreFoundation strings aren't arbitrary collections of bytes, String#to_cf will return a CF::Data if the string has the ASCII-8BIT encoding and a CF::String if not.

If you have an FFI::Pointer or a raw address then you can create a wrapper by passing it to new, for example CF::String.new(some_pointer). This does not check that the pointer is actually a CFString. You can use CF::Base.typecast to construct an instance of the appropriate subclass, for example CF::Base.typecast(some_pointer) would return a CF::String if some_pointer was in fact a CFStringRef.

Memory Management

The convenience methods for creating CF objects will release the cf object when they are garbage collected. Methods on the convenience classes will usually retain the result and mark it for releasing when they are garbage collected (for example CF::Dictionary#[] retains the returned value). You don't need to do any extra memory management on these.

If you pass an FFI::Pointer to new or typecast no assumptions are made for you. You should call retain to manage it manually. However, all objects will add a finalizer to the wrapper that will call CFRelease on the Core Foundation object when the wrapper is garbage collected.

If you use the raw api (eg CF.CFArrayCreate) then you're on your own.

Compatibility

Should work in MRI 2.6 and above and jruby. Not compatible with rubinius due to rubinius' ffi implementation not supporting certain features.

License

Released under the MIT license. See LICENSE

corefoundation's People

Contributors

chef-ci avatar chef-expeditor[bot] avatar dependabot[bot] avatar fcheung avatar lamont-granquist avatar logwolvy avatar rishichawda avatar tas50 avatar tmaher avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.