Make GeglOperation object-safe by implementing Default in a roundabout way.

This commit is contained in:
Bauke 2024-01-25 19:57:44 +01:00
parent 6b403f6943
commit 849588a96b
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 17 additions and 2 deletions

View File

@ -15,16 +15,27 @@ pub type GeglData = indexmap::IndexMap<&'static str, String>;
/// The [`GeglOperation`] trait defines a set of common functions for the /// The [`GeglOperation`] trait defines a set of common functions for the
/// individual operations to implement so they can be used with the GEGL CLI. /// individual operations to implement so they can be used with the GEGL CLI.
pub trait GeglOperation: Default + std::fmt::Debug { pub trait GeglOperation: std::fmt::Debug {
/// Some GEGL operations will run infinitely unless you limit the buffer in /// Some GEGL operations will run infinitely unless you limit the buffer in
/// some way, so all operations must indicate whether or not they should be /// some way, so all operations must indicate whether or not they should be
/// followed by a crop operation. /// followed by a crop operation.
fn append_crop_operation(&self) -> bool; fn append_crop_operation(&self) -> bool;
/// Return the default values for the operation as a [`GeglData`].
///
/// The reason this can't use [`Default`] is because we want the trait to be
/// object-safe so we can use [`Box<dyn GeglOperation>`]. Implementing
/// [`Default`] makes the trait [`Sized`] and no longer object-safe.
///
/// The [`gegl_operation`] macro still implements and calls [`Default`] anyway
/// so it's easy to instantiate operations but we have to go in a roundabout
/// way to actually get the values from it for this function.
fn default_values(&self) -> GeglData;
/// Creates the parameters for the graph to be used with the GEGL CLI. /// Creates the parameters for the graph to be used with the GEGL CLI.
fn graph(&self, include_default_values: bool) -> Vec<String> { fn graph(&self, include_default_values: bool) -> Vec<String> {
let mut graph = vec![self.name().to_string()]; let mut graph = vec![self.name().to_string()];
let defaults = Self::default().values(); let defaults = self.default_values();
for (key, value) in self.values() { for (key, value) in self.values() {
if !include_default_values && defaults.get(key) == Some(&value) { if !include_default_values && defaults.get(key) == Some(&value) {

View File

@ -31,6 +31,10 @@ macro_rules! gegl_operation {
$append_crop $append_crop
} }
fn default_values(&self) -> $crate::GeglData {
Self::default().values()
}
fn name(&self) -> &'static str { fn name(&self) -> &'static str {
concat!("gegl:", $gegl_name) concat!("gegl:", $gegl_name)
} }