<commit_before>//
//  KeyValueObserving.swift
//  SwiftRebound
//
//  Created by <NAME> on 15/07/2016.
//
//

import Foundation

///
/// Collects all the bindings attached to a particular object
///
private class ObserverBindings : NSObject {
    /// The active bindings on this observer
    private var _attachedBindings = [String: KvoBound]();
    
    /// The target object, or nil if there are no attached bindings
    ///
    /// Cocoa throws an exception if there are any observers attached to an object when it is deinitialised.
    ////
    /// Often, it's unstable in terms of deallocation order of things, so the observers may not get deallocated
    /// first, so this exception is random,
    ///
    /// Therefore, we have to keep the object alive for as long as anything is attached to it.
    /// (Ideally, we'd just consider it as a weak reference as a dead object can't update itself, but there's no
    /// way to control object deallocation order sufficiently. This is annoying as for 'natural' bindings the
    /// behaviour is that if a binding goes away it stops updating)
    private var _target: NSObject? = 0;
    
    /// How many active attached bindings there are
    private var _attachCount: Int = 0;
    
    ///
    /// Attaches a binding and ensures that the target object is kept in memory
    ///
    func attachBinding(target: NSObject) {
        _attachCount    += 1;
        _target         = target;
    }
    
    ///
    /// Detaches a binding, allowing the target object to be released if the reference count goes low enough
    ///
    func detachBinding() {
        _attachCount -= 1;
        if _attachCount == 0 {
            _target = nil;
        }
    }

    /// Callback when an observed binding changes
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        // Keypath must exist
        if let keyPath = keyPath {<commit_msg>Use atomic operations for incrementing/decrementing the attach count (though the binding dictionary means we don't really get thread safety yet)<commit_after>//
//  KeyValueObserving.swift
//  SwiftRebound
//
//  Created by <NAME> on 15/07/2016.
//
//

import Foundation

///
/// Collects all the bindings attached to a particular object
///
private class ObserverBindings : NSObject {
    /// The active bindings on this observer
    private var _attachedBindings = [String: KvoBound]();
    
    /// The target object, or nil if there are no attached bindings
    ///
    /// Cocoa throws an exception if there are any observers attached to an object when it is deinitialised.
    ////
    /// Often, it's unstable in terms of deallocation order of things, so the observers may not get deallocated
    /// first, so this exception is random,
    ///
    /// Therefore, we have to keep the object alive for as long as anything is attached to it.
    /// (Ideally, we'd just consider it as a weak reference as a dead object can't update itself, but there's no
    /// way to control object deallocation order sufficiently. This is annoying as for 'natural' bindings the
    /// behaviour is that if a binding goes away it stops updating)
    private var _target: NSObject? = 0;
    
    /// How many active attached bindings there are
    private var _attachCount: Int32 = 0;
    
    ///
    /// Attaches a binding and ensures that the target object is kept in memory
    ///
    func attachBinding(target: NSObject) {
        OSAtomicIncrement32(&_attachCount);
        _target = target;
    }
    
    ///
    /// Detaches a binding, allowing the target object to be released if the reference count goes low enough
    ///
    func detachBinding() {
        let newAttachCount = OSAtomicDecrement32(&_attachCount);
        if newAttachCount == 0 {
            _target = nil;
        }
    }

    /// Callback when an observed binding changes
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        // Keypath must exist
        if let keyPath = keyPath {
{'fraction_non_alphanumeric': 0.08543922984356198, 'fraction_numerical': 0.007220216606498195, 'mean_word_length': 2.4691151919866443, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '362947', 'n_tokens_mistral': 1131, 'n_tokens_neox': 1063, 'n_words': 489}