donet_core/dclass.rs
1/*
2 This file is part of Donet.
3
4 Copyright © 2024-2025 Max Rodriguez <[email protected]>
5
6 Donet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Affero General Public License,
8 as published by the Free Software Foundation, either version 3
9 of the License, or (at your option) any later version.
10
11 Donet is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Affero General Public License for more details.
15
16 You should have received a copy of the GNU Affero General Public
17 License along with Donet. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Data model for Distributed Class definitions in the DC file.
21//! Stores DC Fields and tracks class hierarchy.
22
23use crate::dcatomic::DCAtomicField;
24use crate::dcfield::ClassField;
25use crate::globals;
26use crate::hashgen::*;
27
28/// Represents a Distributed Class defined in the DC file.
29/// Contains a map of DC Fields, as well as atomic and
30/// molecular fields that are declared within the class.
31/// Also stores other properties such as its hierarchy.
32#[derive(Debug)]
33pub struct DClass {
34 class_name: String,
35 class_id: globals::DClassId,
36 is_bogus_class: bool,
37 constructor: Option<DCAtomicField>,
38 fields: Vec<ClassField>,
39}
40
41impl std::fmt::Display for DClass {
42 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43 write!(f, "dclass ")?;
44 f.write_str(&self.get_name())?;
45
46 /*if !self.class_parents.is_empty() {
47 write!(f, " : ")?;
48
49 for (i, parent) in self.class_parents.iter().enumerate() {
50 parent.fmt(f)?;
51
52 if i != self.class_parents.len() - 1 {
53 write!(f, ", ")?;
54 }
55 }
56 }*/
57 write!(f, " {{ // index ")?;
58 self.class_id.fmt(f)?;
59 writeln!(f)?;
60
61 if let Some(constructor) = &self.constructor {
62 constructor.fmt(f)?;
63 }
64
65 for field in &self.fields {
66 match field {
67 ClassField::Atomic(cf) => cf.fmt(f)?,
68 ClassField::Field(cf) => cf.fmt(f)?,
69 ClassField::Molecular(cf) => cf.fmt(f)?,
70 }
71 }
72 writeln!(f, "}};")
73 }
74}
75
76impl LegacyDCHash for DClass {
77 fn generate_hash(&self, hashgen: &mut DCHashGenerator) {
78 hashgen.add_string(self.get_name());
79 //hashgen.add_int(self.get_num_parents().try_into().unwrap());
80
81 // Hash our inheritance tree TODO!
82 //for parent in &self.class_parents {
83 // hashgen.add_int(i32::from(parent.get_dclass_id()));
84 //}
85
86 if let Some(constructor) = &self.constructor {
87 constructor.generate_hash(hashgen);
88 }
89 hashgen.add_int(self.fields.len().try_into().unwrap());
90
91 // Hash our DC fields
92 for field in &self.fields {
93 match field {
94 ClassField::Field(field) => field.generate_hash(hashgen),
95 ClassField::Atomic(atomic) => atomic.generate_hash(hashgen),
96 ClassField::Molecular(molecular) => molecular.generate_hash(hashgen),
97 }
98 }
99 }
100}
101
102impl DClass {
103 #[inline(always)]
104 pub fn get_name(&self) -> String {
105 self.class_name.clone()
106 }
107
108 #[inline(always)]
109 pub fn get_dclass_id(&self) -> globals::DClassId {
110 self.class_id
111 }
112
113 #[inline(always)]
114 pub fn has_constructor(&self) -> bool {
115 self.constructor.is_some()
116 }
117
118 #[inline(always)]
119 pub fn get_constructor(&self) -> Option<&DCAtomicField> {
120 if let Some(atomic) = &self.constructor {
121 Some(atomic)
122 } else {
123 None
124 }
125 }
126}