Line data Source code
1 : use lief_ffi as ffi;
2 :
3 : use std::marker::PhantomData;
4 : use crate::common::{FromFFI, into_optional};
5 : use crate::declare_fwd_iterator;
6 : use super::{Method, Protocol, IVar, Property, DeclOpt};
7 :
8 : /// This class represents an Objective-C class (`@interface`)
9 : pub struct Class<'a> {
10 : ptr: cxx::UniquePtr<ffi::ObjC_Class>,
11 : _owner: PhantomData<&'a ()>,
12 : }
13 :
14 : impl FromFFI<ffi::ObjC_Class> for Class<'_> {
15 0 : fn from_ffi(info: cxx::UniquePtr<ffi::ObjC_Class>) -> Self {
16 0 : Self {
17 0 : ptr: info,
18 0 : _owner: PhantomData
19 0 : }
20 0 : }
21 : }
22 :
23 : impl Class<'_> {
24 : /// Name of the class
25 0 : pub fn name(&self) -> String {
26 0 : self.ptr.name().to_string()
27 0 : }
28 :
29 : /// Demangled name of the class
30 0 : pub fn demangled_name(&self) -> String {
31 0 : self.ptr.demangled_name().to_string()
32 0 : }
33 :
34 : /// Parent class in case of inheritance
35 0 : pub fn super_class(&self) -> Option<Class> {
36 0 : into_optional(self.ptr.super_class())
37 0 : }
38 :
39 0 : pub fn is_meta(&self) -> bool {
40 0 : self.ptr.is_meta()
41 0 : }
42 :
43 : /// Iterator over the different [`Method`] defined by this class
44 0 : pub fn methods(&self) -> Methods {
45 0 : Methods::new(self.ptr.methods())
46 0 : }
47 :
48 : /// Iterator over the different [`Protocol`] implemented by this class
49 0 : pub fn protocols(&self) -> Protocols {
50 0 : Protocols::new(self.ptr.protocols())
51 0 : }
52 :
53 : /// Iterator over the [`Property`] of this class
54 0 : pub fn properties(&self) -> Properties {
55 0 : Properties::new(self.ptr.properties())
56 0 : }
57 :
58 : /// Iterator over the different instance variables ([`IVar`]) defined in this class
59 0 : pub fn ivars(&self) -> IVars {
60 0 : IVars::new(self.ptr.ivars())
61 0 : }
62 :
63 : /// Generate a header-like string for this specific class
64 0 : pub fn to_decl(&self) -> String {
65 0 : self.ptr.to_decl().to_string()
66 0 : }
67 :
68 : /// Same behavior as [`Class::to_decl`] but with an additional
69 : /// [`DeclOpt`] parameter to customize the output
70 0 : pub fn to_decl_with_opt(&self, opt: &DeclOpt) -> String {
71 0 : self.ptr.to_decl_with_opt(opt.to_ffi()).to_string()
72 0 : }
73 : }
74 :
75 0 : declare_fwd_iterator!(
76 0 : Methods,
77 0 : Method<'a>,
78 0 : ffi::ObjC_Method,
79 0 : ffi::ObjC_Class,
80 0 : ffi::ObjC_Class_it_methods
81 0 : );
82 :
83 0 : declare_fwd_iterator!(
84 0 : Protocols,
85 0 : Protocol<'a>,
86 0 : ffi::ObjC_Protocol,
87 0 : ffi::ObjC_Class,
88 0 : ffi::ObjC_Class_it_protocols
89 0 : );
90 :
91 0 : declare_fwd_iterator!(
92 0 : Properties,
93 0 : Property<'a>,
94 0 : ffi::ObjC_Property,
95 0 : ffi::ObjC_Class,
96 0 : ffi::ObjC_Class_it_properties
97 0 : );
98 :
99 0 : declare_fwd_iterator!(
100 0 : IVars,
101 0 : IVar<'a>,
102 0 : ffi::ObjC_IVar,
103 0 : ffi::ObjC_Class,
104 0 : ffi::ObjC_Class_it_ivars
105 0 : );
|