spb/forge Public MIT
Forge — LLM training from scratch in pure C++20 + Metal on Apple Silicon.
C++ 61.2%
C 23%
Python 7.6%
TeX 7.2%
CMake 1.1%
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Foundation/NSNotification.hpp4//5// Copyright 2020-2024 Apple Inc.6//7// Licensed under the Apache License, Version 2.0 (the "License");8// you may not use this file except in compliance with the License.9// You may obtain a copy of the License at10//11// http://www.apache.org/licenses/LICENSE-2.012//13// Unless required by applicable law or agreed to in writing, software14// distributed under the License is distributed on an "AS IS" BASIS,15// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.16// See the License for the specific language governing permissions and17// limitations under the License.18//19//-------------------------------------------------------------------------------------------------------------------------------------------------------------2021#pragma once2223//-------------------------------------------------------------------------------------------------------------------------------------------------------------2425#include "NSDefines.hpp"26#include "NSDictionary.hpp"27#include "NSObject.hpp"28#include "NSString.hpp"29#include "NSTypes.hpp"30#include <functional>3132//-------------------------------------------------------------------------------------------------------------------------------------------------------------3334namespace NS35{36using NotificationName = class String*;3738class Notification : public NS::Referencing<Notification>39{40public:41 NS::String* name() const;42 NS::Object* object() const;43 NS::Dictionary* userInfo() const;44};4546using ObserverBlock = void(^)(Notification*);47using ObserverFunction = std::function<void(Notification*)>;4849class NotificationCenter : public NS::Referencing<NotificationCenter>50{51 public:52 static class NotificationCenter* defaultCenter();53 Object* addObserver(NotificationName name, Object* pObj, void* pQueue, ObserverBlock block);54 Object* addObserver(NotificationName name, Object* pObj, void* pQueue, ObserverFunction &handler);55 void removeObserver(Object* pObserver);5657};58}5960//-------------------------------------------------------------------------------------------------------------------------------------------------------------6162_NS_INLINE NS::String* NS::Notification::name() const63{64 return Object::sendMessage<NS::String*>(this, _NS_PRIVATE_SEL(name));65}6667//-------------------------------------------------------------------------------------------------------------------------------------------------------------6869_NS_INLINE NS::Object* NS::Notification::object() const70{71 return Object::sendMessage<NS::Object*>(this, _NS_PRIVATE_SEL(object));72}7374//-------------------------------------------------------------------------------------------------------------------------------------------------------------7576_NS_INLINE NS::Dictionary* NS::Notification::userInfo() const77{78 return Object::sendMessage<NS::Dictionary*>(this, _NS_PRIVATE_SEL(userInfo));79}8081//-------------------------------------------------------------------------------------------------------------------------------------------------------------8283_NS_INLINE NS::NotificationCenter* NS::NotificationCenter::defaultCenter()84{85 return NS::Object::sendMessage<NS::NotificationCenter*>(_NS_PRIVATE_CLS(NSNotificationCenter), _NS_PRIVATE_SEL(defaultCenter));86}8788//-------------------------------------------------------------------------------------------------------------------------------------------------------------8990_NS_INLINE NS::Object* NS::NotificationCenter::addObserver(NS::NotificationName name, Object* pObj, void* pQueue, NS::ObserverBlock block)91{92 return NS::Object::sendMessage<Object*>(this, _NS_PRIVATE_SEL(addObserverName_object_queue_block_), name, pObj, pQueue, block);93}9495//-------------------------------------------------------------------------------------------------------------------------------------------------------------9697_NS_INLINE NS::Object* NS::NotificationCenter::addObserver(NS::NotificationName name, Object* pObj, void* pQueue, NS::ObserverFunction &handler)98{99 __block ObserverFunction blockFunction = handler;100101 return addObserver(name, pObj, pQueue, ^(NS::Notification* pNotif) {blockFunction(pNotif);});102}103104//-------------------------------------------------------------------------------------------------------------------------------------------------------------105106_NS_INLINE void NS::NotificationCenter::removeObserver(Object* pObserver)107{108 return NS::Object::sendMessage<void>(this, _NS_PRIVATE_SEL(removeObserver_), pObserver);109}110111