1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (c) 2020, The Regents of the University of California
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 // * Redistributions of source code must retain the above copyright notice, this
11 //   list of conditions and the following disclaimer.
12 //
13 // * Redistributions in binary form must reproduce the above copyright notice,
14 //   this list of conditions and the following disclaimer in the documentation
15 //   and/or other materials provided with the distribution.
16 //
17 // * Neither the name of the copyright holder nor the names of its
18 //   contributors may be used to endorse or promote products derived from
19 //   this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
32 
33 // Generator Code Begin Cpp
34 #include "dbModule.h"
35 
36 #include "db.h"
37 #include "dbBlock.h"
38 #include "dbDatabase.h"
39 #include "dbDiff.hpp"
40 #include "dbHashTable.hpp"
41 #include "dbTable.h"
42 #include "dbTable.hpp"
43 // User Code Begin Includes
44 #include <string>
45 
46 #include "dbInst.h"
47 #include "dbModInst.h"
48 #include "dbModuleInstItr.h"
49 #include "dbModuleModInstItr.h"
50 // User Code End Includes
51 namespace odb {
52 
53 template class dbTable<_dbModule>;
54 
operator ==(const _dbModule & rhs) const55 bool _dbModule::operator==(const _dbModule& rhs) const
56 {
57   if (_name != rhs._name)
58     return false;
59 
60   if (_next_entry != rhs._next_entry)
61     return false;
62 
63   if (_insts != rhs._insts)
64     return false;
65 
66   if (_modinsts != rhs._modinsts)
67     return false;
68 
69   if (_mod_inst != rhs._mod_inst)
70     return false;
71 
72   // User Code Begin ==
73   // User Code End ==
74   return true;
75 }
operator <(const _dbModule & rhs) const76 bool _dbModule::operator<(const _dbModule& rhs) const
77 {
78   // User Code Begin <
79   if (strcmp(_name, rhs._name) >= 0)
80     return false;
81   // User Code End <
82   return true;
83 }
differences(dbDiff & diff,const char * field,const _dbModule & rhs) const84 void _dbModule::differences(dbDiff& diff,
85                             const char* field,
86                             const _dbModule& rhs) const
87 {
88   DIFF_BEGIN
89 
90   DIFF_FIELD(_name);
91   DIFF_FIELD(_next_entry);
92   DIFF_FIELD(_insts);
93   DIFF_FIELD(_modinsts);
94   DIFF_FIELD(_mod_inst);
95   // User Code Begin Differences
96   // User Code End Differences
97   DIFF_END
98 }
out(dbDiff & diff,char side,const char * field) const99 void _dbModule::out(dbDiff& diff, char side, const char* field) const
100 {
101   DIFF_OUT_BEGIN
102   DIFF_OUT_FIELD(_name);
103   DIFF_OUT_FIELD(_next_entry);
104   DIFF_OUT_FIELD(_insts);
105   DIFF_OUT_FIELD(_modinsts);
106   DIFF_OUT_FIELD(_mod_inst);
107 
108   // User Code Begin Out
109   // User Code End Out
110   DIFF_END
111 }
_dbModule(_dbDatabase * db)112 _dbModule::_dbModule(_dbDatabase* db)
113 {
114   // User Code Begin Constructor
115   _name = 0;
116   _insts = 0;
117   _modinsts = 0;
118   _mod_inst = 0;
119   // User Code End Constructor
120 }
_dbModule(_dbDatabase * db,const _dbModule & r)121 _dbModule::_dbModule(_dbDatabase* db, const _dbModule& r)
122 {
123   _name = r._name;
124   _next_entry = r._next_entry;
125   _insts = r._insts;
126   _modinsts = r._modinsts;
127   _mod_inst = r._mod_inst;
128   // User Code Begin CopyConstructor
129   // User Code End CopyConstructor
130 }
131 
operator >>(dbIStream & stream,_dbModule & obj)132 dbIStream& operator>>(dbIStream& stream, _dbModule& obj)
133 {
134   stream >> obj._name;
135   stream >> obj._next_entry;
136   stream >> obj._insts;
137   stream >> obj._modinsts;
138   stream >> obj._mod_inst;
139   // User Code Begin >>
140   // User Code End >>
141   return stream;
142 }
operator <<(dbOStream & stream,const _dbModule & obj)143 dbOStream& operator<<(dbOStream& stream, const _dbModule& obj)
144 {
145   stream << obj._name;
146   stream << obj._next_entry;
147   stream << obj._insts;
148   stream << obj._modinsts;
149   stream << obj._mod_inst;
150   // User Code Begin <<
151   // User Code End <<
152   return stream;
153 }
154 
~_dbModule()155 _dbModule::~_dbModule()
156 {
157   if (_name)
158     free((void*) _name);
159   // User Code Begin Destructor
160   // User Code End Destructor
161 }
162 
163 // User Code Begin PrivateMethods
164 // User Code End PrivateMethods
165 
166 ////////////////////////////////////////////////////////////////////
167 //
168 // dbModule - Methods
169 //
170 ////////////////////////////////////////////////////////////////////
171 
getName() const172 const char* dbModule::getName() const
173 {
174   _dbModule* obj = (_dbModule*) this;
175   return obj->_name;
176 }
177 
getModInst() const178 dbModInst* dbModule::getModInst() const
179 {
180   _dbModule* obj = (_dbModule*) this;
181   if (obj->_mod_inst == 0)
182     return NULL;
183   _dbBlock* par = (_dbBlock*) obj->getOwner();
184   return (dbModInst*) par->_modinst_tbl->getPtr(obj->_mod_inst);
185 }
186 
187 // User Code Begin dbModulePublicMethods
188 
addInst(dbInst * inst)189 void dbModule::addInst(dbInst* inst)
190 {
191   _dbModule* module = (_dbModule*) this;
192   _dbInst* _inst = (_dbInst*) inst;
193   _dbBlock* block = (_dbBlock*) module->getOwner();
194 
195   if (_inst->_module != 0) {
196     dbModule* mod = dbModule::getModule((dbBlock*) block, _inst->_module);
197     mod->removeInst(inst);
198   }
199 
200   _inst->_module = module->getOID();
201   _inst->_module_next = module->_insts;
202   module->_insts = _inst->getOID();
203 }
204 
removeInst(dbInst * inst)205 void dbModule::removeInst(dbInst* inst)
206 {
207   _dbModule* module = (_dbModule*) this;
208   _dbInst* _inst = (_dbInst*) inst;
209   if (_inst->_module != module->getOID())
210     return;
211   _dbBlock* block = (_dbBlock*) module->getOwner();
212   uint id = _inst->getOID();
213 
214   _dbInst* prev = NULL;
215   uint cur = module->_insts;
216   while (cur) {
217     _dbInst* c = block->_inst_tbl->getPtr(cur);
218     if (cur == id) {
219       if (prev == NULL)
220         module->_insts = _inst->_module_next;
221       else
222         prev->_module_next = _inst->_module_next;
223       break;
224     }
225     prev = c;
226     cur = c->_module_next;
227   }
228   _inst->_module = 0;
229   _inst->_module_next = 0;
230 }
231 
getChildren()232 dbSet<dbModInst> dbModule::getChildren()
233 {
234   _dbModule* module = (_dbModule*) this;
235   _dbBlock* block = (_dbBlock*) module->getOwner();
236   return dbSet<dbModInst>(module, block->_module_modinst_itr);
237 }
238 
getInsts()239 dbSet<dbInst> dbModule::getInsts()
240 {
241   _dbModule* module = (_dbModule*) this;
242   _dbBlock* block = (_dbBlock*) module->getOwner();
243   return dbSet<dbInst>(module, block->_module_inst_itr);
244 }
245 
create(dbBlock * block,const char * name)246 dbModule* dbModule::create(dbBlock* block, const char* name)
247 {
248   _dbBlock* _block = (_dbBlock*) block;
249   if (_block->_module_hash.hasMember(name))
250     return nullptr;
251   _dbModule* module = _block->_module_tbl->create();
252   module->_name = strdup(name);
253   ZALLOCATED(module->_name);
254   _block->_module_hash.insert(module);
255   return (dbModule*) module;
256 }
257 
destroy(dbModule * module)258 void dbModule::destroy(dbModule* module)
259 {
260   _dbModule* _module = (_dbModule*) module;
261   _dbBlock* block = (_dbBlock*) _module->getOwner();
262 
263   dbSet<dbModInst> modinsts = module->getChildren();
264   dbSet<dbModInst>::iterator itr;
265   for (itr = modinsts.begin(); itr != modinsts.end();) {
266     itr = dbModInst::destroy(itr);
267   }
268   if (_module->_mod_inst != 0)
269     dbModInst::destroy(module->getModInst());
270 
271   for (auto inst : module->getInsts()) {
272     _dbInst* _inst = (_dbInst*) inst;
273     _inst->_module = 0;
274     _inst->_module_next = 0;
275   }
276 
277   dbProperty::destroyProperties(_module);
278   block->_module_hash.remove(_module);
279   block->_module_tbl->destroy(_module);
280 }
281 
getModule(dbBlock * block_,uint dbid_)282 dbModule* dbModule::getModule(dbBlock* block_, uint dbid_)
283 {
284   _dbBlock* block = (_dbBlock*) block_;
285   return (dbModule*) block->_module_tbl->getPtr(dbid_);
286 }
287 
findModInst(const char * name)288 dbModInst* dbModule::findModInst(const char* name)
289 {
290   _dbModule* obj = (_dbModule*) this;
291   _dbBlock* par = (_dbBlock*) obj->getOwner();
292   std::string h_name = std::string(obj->_name) + "/" + std::string(name);
293   return (dbModInst*) par->_modinst_hash.find(h_name.c_str());
294 }
295 
296 // User Code End dbModulePublicMethods
297 }  // namespace odb
298    // Generator Code End Cpp
299