Skip to content

Indentation consistency #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ it is possible to access these directly using `@objc`, ObjectiveC.jl provides a
automatically generate the appropriate `getproperty`, `setproperty!` and `propertynames`
definitions:

```julia
```julia-repl
julia> @objcproperties NSValue begin
@autoproperty pointerValue::Ptr{Cvoid}
@autoproperty pointerValue::Ptr{Cvoid}
end

julia> obj.pointerValue
Expand All @@ -82,30 +82,30 @@ property macros:

```julia
@objcproperties SomeObject begin
# simplest definition: just generate a getter,
# and convert the property value to `DstTyp`
@autoproperty someProperty::DstTyp

# also generate a setter
@autoproperty someProperty::DstTyp setter=setSomeProperty

# if the property is an ObjC object, use an object pointer type.
# this will make sure to do a nil check and return nothing,
# or convert the pointer to an instance of the specified type
@autoproperty someProperty::id{DstTyp}

# sometimes you may want to convert to a different type
@autoproperty someStringProperty::id{NSString} type=String

# and finally, if more control is needed, just do it yourselv:
@getproperty someComplexProperty function(obj)
# do something with obj
# return a value
end
@setproperty! someComplexProperty function(obj, val)
# do something with obj and val
# return nothing
end
# simplest definition: just generate a getter,
# and convert the property value to `DstTyp`
@autoproperty someProperty::DstTyp

# also generate a setter
@autoproperty someProperty::DstTyp setter=setSomeProperty

# if the property is an ObjC object, use an object pointer type.
# this will make sure to do a nil check and return nothing,
# or convert the pointer to an instance of the specified type
@autoproperty someProperty::id{DstTyp}

# sometimes you may want to convert to a different type
@autoproperty someStringProperty::id{NSString} type=String

# and finally, if more control is needed, just do it yourself:
@getproperty someComplexProperty function(obj)
# do something with obj
# return a value
end
@setproperty! someComplexProperty function(obj, val)
# do something with obj and val
# return nothing
end
end
```

Expand All @@ -114,10 +114,10 @@ end

Julia callables can be converted to Objective-C blocks using the `@objcblock` macro:

```julia
```julia-repl
julia> function hello(x)
println("Hello, $x!")
x+1
println("Hello, $x!")
x+1
end
julia> block = @objcblock(hello, Cint, (Cint,))
```
Expand All @@ -134,11 +134,11 @@ may decide to coalesce multiple conditions into a single execution, so it is pre
use `@objcblock` whenever possible. It is also not possible to pass any arguments to the
condition, but you can use a closure to capture any state you need:

```julia
```julia-repl
julia> counter = 0
julia> cond = Base.AsyncCondition() do async_cond
counter += 1
end
counter += 1
end
julia> block = @objcasyncblock(cond)
```

Expand All @@ -147,7 +147,7 @@ julia> block = @objcasyncblock(cond)

ObjectiveC.jl also provides ready-made wrappers for essential frameworks like Foundation:

```julia
```julia-repl
julia> using .Foundation


Expand All @@ -156,9 +156,9 @@ NSString("test")


julia> NSArray([str, str])
(
test,
test
<__NSArrayI 0x12e69b9b0>(
test,
test
)


Expand All @@ -181,7 +181,7 @@ Dict{NSString, NSString} with 1 entry:
To see what ObjectiveC.jl is doing under the hood, you can toggle the `tracing` preference,
which will make the package print out the Objective-C calls it makes:

```julia
```julia-repl
julia> using ObjectiveC
julia> ObjectiveC.enable_tracing(true)
[ Info: ObjectiveC.jl tracing setting changed; restart your Julia session for this change to take effect!
Expand Down
40 changes: 20 additions & 20 deletions src/blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ end
# NSBlocks are untracked by the Julia GC, so we need to manually root the Julia objects
const julia_block_roots = Dict{NSBlock,JuliaBlock}()
function julia_block_copy(_dst, _src)
dst_nsblock = NSBlock(reinterpret(id{NSBlock}, _dst))
src_nsblock = NSBlock(reinterpret(id{NSBlock}, _src))
dst_nsblock = NSBlock(reinterpret(id{NSBlock}, _dst))
src_nsblock = NSBlock(reinterpret(id{NSBlock}, _src))

@assert haskey(julia_block_roots, src_nsblock)
julia_block_roots[dst_nsblock] = julia_block_roots[src_nsblock]
@assert haskey(julia_block_roots, src_nsblock)
julia_block_roots[dst_nsblock] = julia_block_roots[src_nsblock]

return
return
end
function julia_block_dispose(_block)
block = unsafe_load(_block)
nsblock = NSBlock(reinterpret(id{NSBlock}, _block))
block = unsafe_load(_block)
nsblock = NSBlock(reinterpret(id{NSBlock}, _block))

@assert haskey(julia_block_roots, nsblock)
delete!(julia_block_roots, nsblock)
@assert haskey(julia_block_roots, nsblock)
delete!(julia_block_roots, nsblock)

return
return
end

# JuliaBlock is the concrete version of NSBlock, so make it possible to derive a regular
Expand Down Expand Up @@ -78,13 +78,13 @@ const julia_block_descriptor_initialized = Ref{Bool}(false)
function JuliaBlock(trampoline, callable)
# lazily create a descriptor (these sometimes don't precompile properly)
if !julia_block_descriptor_initialized[]
# simple cfunctions, so don't need to be rooted
copy_cb = @cfunction(julia_block_copy, Nothing, (Ptr{JuliaBlock}, Ptr{JuliaBlock}))
dispose_cb = @cfunction(julia_block_dispose, Nothing, (Ptr{JuliaBlock},))
# simple cfunctions, so don't need to be rooted
copy_cb = @cfunction(julia_block_copy, Nothing, (Ptr{JuliaBlock}, Ptr{JuliaBlock}))
dispose_cb = @cfunction(julia_block_dispose, Nothing, (Ptr{JuliaBlock},))

julia_block_descriptor[] = JuliaBlockDescriptor(0, sizeof(JuliaBlock),
copy_cb, dispose_cb)
julia_block_descriptor_initialized[] = true
julia_block_descriptor[] = JuliaBlockDescriptor(0, sizeof(JuliaBlock),
copy_cb, dispose_cb)
julia_block_descriptor_initialized[] = true
end

# set-up the block data structures
Expand Down Expand Up @@ -183,10 +183,10 @@ const julia_async_block_descriptor_initialized = Ref{Bool}(false)
function JuliaAsyncBlock(cond)
# lazily create a descriptor (these sometimes don't precompile properly)
if !julia_async_block_descriptor_initialized[]
# simple cfunctions, so don't need to be rooted
julia_async_block_descriptor[] = JuliaAsyncBlockDescriptor(0, sizeof(JuliaAsyncBlock),
C_NULL, C_NULL)
julia_async_block_descriptor_initialized[] = true
# simple cfunctions, so don't need to be rooted
julia_async_block_descriptor[] = JuliaAsyncBlockDescriptor(0, sizeof(JuliaAsyncBlock),
C_NULL, C_NULL)
julia_async_block_descriptor_initialized[] = true
end

# create a trampoline to wake libuv with the user-provided condition
Expand Down
34 changes: 17 additions & 17 deletions src/classes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@
# resurrecting it, please look at the repository at commit 22118319da.

function allocclass(name, super)
ptr = ccall(:objc_allocateClassPair, Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cchar}, Csize_t),
super, name, 0)
ptr == C_NULL && error("Couldn't allocate class $name")
return Class(ptr)
ptr = ccall(:objc_allocateClassPair, Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cchar}, Csize_t),
super, name, 0)
ptr == C_NULL && error("Couldn't allocate class $name")
return Class(ptr)
end

function register(class::Class)
ccall(:objc_registerClassPair, Cvoid, (Ptr{Cvoid},),
class)
return class
ccall(:objc_registerClassPair, Cvoid, (Ptr{Cvoid},),
class)
return class
end

createclass(name, super) = allocclass(name, super) |> register

getmethod(class::Class, sel::Selector) =
ccall(:class_getInstanceMethod, Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}),
class, sel)
ccall(:class_getInstanceMethod, Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}),
class, sel)

methodtypeenc(method::Ptr) =
ccall(:method_getTypeEncoding, Ptr{Cchar}, (Ptr{Cvoid},),
method) |> unsafe_string
ccall(:method_getTypeEncoding, Ptr{Cchar}, (Ptr{Cvoid},),
method) |> unsafe_string

methodtypeenc(class::Class, sel::Selector) = methodtypeenc(getmethod(class, sel))

methodtype(args...) = methodtypeenc(args...) |> parseencoding

replacemethod(class::Class, sel::Selector, imp::Ptr{Cvoid}, types::String) =
ccall(:class_replaceMethod, Bool, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cchar}),
class, sel, imp, types)
ccall(:class_replaceMethod, Bool, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cchar}),
class, sel, imp, types)

function setmethod(class::Class, sel::Selector, imp::Ptr{Cvoid}, types::String)
meth = getmethod(class, sel)
meth ≠ C_NULL && methodtype(meth) != parseencoding(types) &&
error("New method $(name(sel)) of $class must match $(methodtype(meth))")
replacemethod(class, sel, imp, types)
meth = getmethod(class, sel)
meth ≠ C_NULL && methodtype(meth) != parseencoding(types) &&
error("New method $(name(sel)) of $class must match $(methodtype(meth))")
replacemethod(class, sel, imp, types)
end
Loading
Loading