aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2024-01-19 09:52:01 +0100
committerThomas White <taw@physics.org>2024-02-06 16:59:35 +0100
commit9a907d58a931472eb1178c42bfa1e7629e30ab00 (patch)
tree5d7e5e31e72960b82dfe321c4b081dbd19cd1bea
parent1b4fdfa5cc1ab4fe9a1cdcbb0bf1602834eaec13 (diff)
Ownership for Crystal.Cell
-rw-r--r--julia/CrystFEL/src/crystal.jl35
-rw-r--r--julia/CrystFEL/src/image.jl2
-rw-r--r--libcrystfel/src/crystal.c11
-rw-r--r--libcrystfel/src/crystal.h1
4 files changed, 47 insertions, 2 deletions
diff --git a/julia/CrystFEL/src/crystal.jl b/julia/CrystFEL/src/crystal.jl
index eba7a615..38204a62 100644
--- a/julia/CrystFEL/src/crystal.jl
+++ b/julia/CrystFEL/src/crystal.jl
@@ -10,6 +10,7 @@ mutable struct InternalCrystal end
mutable struct Crystal
internalptr::Ptr{InternalCrystal}
+ cell
end
@@ -39,7 +40,7 @@ function Crystal(cell::UnitCell; profileradius=2e6, mosaicity=0)
Cvoid, (Ptr{InternalCrystal},Cdouble),
out, mosaicity)
- cr = Crystal(out)
+ cr = Crystal(out, nothing)
finalizer(cr) do x
ccall((:crystal_free, libcrystfel), Cvoid, (Ptr{InternalCrystal},),
@@ -67,4 +68,36 @@ function Base.setproperty!(cr::Crystal, name::Symbol, val)
end
+function getcell(cr)
+ out = @ccall libcrystfel.crystal_relinquish_cell(cr.internalptr::Ptr{InternalCrystal})::Ptr{InternalUnitCell}
+ if getfield(cr, :cell) === nothing || getfield(cr, :cell).internalptr != out
+ if out != C_NULL
+ setfield!(cr, :cell, UnitCell(out))
+ else
+ setfield!(cr, :cell, nothing)
+ end
+ end
+ return getfield(cr, :cell)
+end
+
+
+function Base.getproperty(cr::Crystal, name::Symbol)
+ if name === :internalptr
+ getfield(cr, :internalptr)
+ elseif name === :cell
+ return getcell(cr)
+ end
+end
+
+
+function Base.show(io::IO, cr::Crystal)
+ write(io, "Crystal(")
+ if cr.cell !== nothing
+ show(io, cr.cell)
+ else
+ write(io, "no cell")
+ end
+ write(io, ")")
+end
+
end # of module
diff --git a/julia/CrystFEL/src/image.jl b/julia/CrystFEL/src/image.jl
index 3384f0c9..5740dfaf 100644
--- a/julia/CrystFEL/src/image.jl
+++ b/julia/CrystFEL/src/image.jl
@@ -76,7 +76,7 @@ function makecrystallist(image, listptr, n)
if n !== nothing
cr = getfield(image, :crystals)[n]
else
- cr = Crystal(pairptr.crystal)
+ cr = Crystal(pairptr.crystal, nothing)
end
if pairptr.reflist == C_NULL
diff --git a/libcrystfel/src/crystal.c b/libcrystfel/src/crystal.c
index e6ae4b09..0cf246ae 100644
--- a/libcrystfel/src/crystal.c
+++ b/libcrystfel/src/crystal.c
@@ -41,6 +41,7 @@
struct _crystal
{
UnitCell *cell;
+ int owns_cell;
double m; /* Mosaicity in radians */
double osf;
double Bfac;
@@ -72,6 +73,7 @@ Crystal *crystal_new()
if ( cryst == NULL ) return NULL;
cryst->cell = NULL;
+ cryst->owns_cell = 1;
cryst->m = 0.0;
cryst->osf = 1.0;
cryst->Bfac = 0.0;
@@ -128,6 +130,7 @@ Crystal *crystal_copy(const Crystal *cryst)
void crystal_free(Crystal *cryst)
{
if ( cryst == NULL ) return;
+ if ( cryst->owns_cell ) cell_free(cryst->cell);
cffree(cryst->notes);
cffree(cryst);
}
@@ -142,6 +145,13 @@ UnitCell *crystal_get_cell(Crystal *cryst)
}
+UnitCell *crystal_relinquish_cell(Crystal *cryst)
+{
+ cryst->owns_cell = 0;
+ return cryst->cell;
+}
+
+
const UnitCell *crystal_get_cell_const(const Crystal *cryst)
{
return cryst->cell;
@@ -217,6 +227,7 @@ void crystal_set_cell(Crystal *cryst, UnitCell *cell)
{
if ( cryst->owns_cell ) cell_free(cryst->cell);
cryst->cell = cell;
+ cryst->owns_cell = 1;
}
diff --git a/libcrystfel/src/crystal.h b/libcrystfel/src/crystal.h
index 7147111f..1804fa35 100644
--- a/libcrystfel/src/crystal.h
+++ b/libcrystfel/src/crystal.h
@@ -52,6 +52,7 @@ extern Crystal *crystal_copy(const Crystal *cryst);
extern void crystal_free(Crystal *cryst);
extern UnitCell *crystal_get_cell(Crystal *cryst);
+extern UnitCell *crystal_relinquish_cell(Crystal *cryst);
extern double crystal_get_profile_radius(const Crystal *cryst);
extern double crystal_get_resolution_limit(Crystal *cryst);
extern int crystal_get_user_flag(Crystal *cryst);