Skip to content

Commit 98283dc

Browse files
committed
more work on inverse attrs, code generation
1 parent 65c703b commit 98283dc

25 files changed

+713
-740
lines changed

src/cllazyfile/lazyRefs.h

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,29 @@ class lazyRefs {
7575
//3c - for each item in both _refMap and edL, add it to _referentInstances
7676
potentialReferentInsts( edL );
7777
//3d - load each inst
78-
invAttrListNode * invNode = invAttr( _inst, ia /*, iaList*/ );
78+
/*invAttrListNode * invNode*/
79+
iAstruct * ias = invAttr( _inst, ia /*, iaList*/ );
7980
referentInstances_t::iterator insts = _referentInstances.begin();
8081
for( ; insts != _referentInstances.end(); ++insts ) {
81-
loadInstIFFreferent( *insts, invNode );
82+
loadInstIFFreferent( *insts, ias, ia );
8283
}
8384
//3f - cache edL - TODO
8485
}
8586

86-
void loadInstIFFreferent( instanceID inst, invAttrListNode * invNode ) {
87+
void loadInstIFFreferent( instanceID inst, iAstruct * ias, const Inverse_attribute * ia ) {
8788
bool prevLoaded = _lim->isLoaded( inst );
8889
SDAI_Application_instance * rinst = _lim->loadInstance( inst );
89-
bool ref = refersToCurrentInst( invNode->inverseADesc(), rinst );
90+
bool ref = refersToCurrentInst( ia, rinst );
9091
if( ref ) {
91-
if( invNode->isAggregate() ) {
92-
EntityAggregate * ea = ( ( invAttrListNodeA * )invNode )->getter()( _inst );
92+
if( ia->inverted_attr_()->IsAggrType() ) {
93+
EntityAggregate * ea = ias->a;
94+
assert( ea && "is it possible for this to be null here? if so, must create & assign");
9395
//TODO check if duplicate
9496
ea->AddNode( new EntityNode( rinst ) );
9597
} else {
96-
SDAI_Application_instance * ai = ( ( invAttrListNodeI * )invNode )->getter()( _inst );
98+
SDAI_Application_instance * ai = ias->i;
9799
if( !ai ) {
98-
( ( invAttrListNodeI * )invNode )->setter()( _inst, rinst );
100+
ias->i = rinst;
99101
} else if( ai->GetFileId() != inst ) {
100102
std::cerr << "ERROR: two instances (" << rinst << " and " << ai->GetFileId() << ") refer to inst ";
101103
std::cerr << _inst->GetFileId() << ", but its inverse attribute is not an aggregation type!" << std::endl;
@@ -110,7 +112,7 @@ class lazyRefs {
110112
}
111113

112114
///3e - check if actually inverse ref
113-
bool refersToCurrentInst( Inverse_attribute * ia, SDAI_Application_instance * referrer ) {
115+
bool refersToCurrentInst( const Inverse_attribute * ia, SDAI_Application_instance * referrer ) {
114116
//find the attr
115117
int rindex = attrIndex( referrer, ia->_inverted_attr_id, ia->_inverted_entity_id );
116118
STEPattribute sa = referrer->attributes[ rindex ];
@@ -155,7 +157,7 @@ class lazyRefs {
155157
return -1;
156158
}
157159

158-
invAttrListNode * invAttr( SDAI_Application_instance * inst, const Inverse_attribute * ia /*, iaList_t & iaList */ ) {
160+
iAstruct * invAttr( SDAI_Application_instance * inst, const Inverse_attribute * ia /*, iaList_t & iaList */ ) {
159161
/* looks for iAttrs in schemas/sdai_ap214e3/entity/SdaiGeometric_representation_context.cc, but the ctors don't populate it
160162
* shouldn't the parent class ctor populate it?
161163
ENTITY representation_context;
@@ -171,16 +173,22 @@ SUBTYPE OF (representation_context);
171173
coordinate_space_dimension : dimension_count;
172174
END_ENTITY; -- 10303-42: geometry_schema
173175
*/
174-
invAttrListNode * n = ( invAttrListNode * ) inst->iAttrs.GetHead();
175-
while( n ) {
176-
if( n->inverseADesc() == ia ) {
177-
return n;
176+
SDAI_Application_instance::iAMap_t map = inst->getInvAttrs();
177+
SDAI_Application_instance::iAMap_t::iterator iai = map.begin();
178+
while( iai != map.end() ) {
179+
if( iai->first == ia ) {
180+
return &( iai->second );
178181
}
179-
n = ( invAttrListNode * ) n->NextNode();
182+
++iai;
180183
}
181-
std::cerr << "Error! inverse attr " << ia->Name() << " (" << ia << ") not found in iAttrs (";
182-
std::cerr << ( void * )( & ( inst->iAttrs ) ) << ") - entity " << inst->eDesc->Name() << "." << std::endl;
183-
return 0;
184+
iai = map.begin();
185+
//FIXME treat as unrecoverable?
186+
std::cerr << "Error! inverse attr " << ia->Name() << " (" << ia << ") not found in iAMap for entity " << inst->getEDesc()->Name() << ". Map contents:" << std::endl;
187+
for( ; iai != map.end(); ++iai ) {
188+
std::cerr << iai->first->Name() << ": " << (void*)(iai->second.a) << ", ";
189+
}
190+
std::cerr << std::endl;
191+
return NULL;
184192
}
185193

186194
/** 3c. compare the type of each item in R with types in A
@@ -208,13 +216,13 @@ END_ENTITY; -- 10303-42: geometry_schema
208216
const Inverse_attribute * iAttr;
209217
for( ; !supersIter.empty(); ++supersIter ) {
210218
//look at attrs of *si
211-
InverseAItr iai( ( *supersIter )->InverseAttr() );
219+
InverseAItr iai( &( ( *supersIter )->InverseAttr() ) );
212220
while( 0 != ( iAttr = iai.NextInverse_attribute() ) ) {
213221
iaList.insert( iAttr );
214222
}
215223
}
216224
// look at our own attrs
217-
InverseAItr invAttrIter( ed->InverseAttr() );
225+
InverseAItr invAttrIter( &( ed->InverseAttr() ) );
218226
while( 0 != ( iAttr = invAttrIter.NextInverse_attribute() ) ) {
219227
iaList.insert( iAttr );
220228
}
@@ -272,7 +280,7 @@ END_ENTITY; -- 10303-42: geometry_schema
272280

273281

274282
// 1. find inverse attrs with recursion
275-
getInverseAttrs( ai->eDesc, _iaList );
283+
getInverseAttrs( ai->getEDesc(), _iaList );
276284

277285
//2. find reverse refs, map id to type (stop if there are no inverse attrs or no refs)
278286
if( _iaList.size() == 0 || !mapRefsToTypes() ) {

src/cllazyfile/sectionReader.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ SDAI_Application_instance * sectionReader::getRealInstance( const Registry * reg
308308
if( !comment.empty() ) {
309309
inst->AddP21Comment( comment );
310310
}
311-
assert( inst->eDesc );
311+
assert( inst->getEDesc() );
312312
_file.seekg( begin );
313313
findNormalString( "(" );
314314
_file.unget();

src/clstepcore/ExpDict.cc

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,9 @@ const AttrDescriptor * AttrDescItr::NextAttrDesc() {
554554
return 0;
555555
}
556556

557-
const Inverse_attribute * InverseAItr::NextInverse_attribute() {
557+
Inverse_attribute * InverseAItr::NextInverse_attribute() {
558558
if( cur ) {
559-
const Inverse_attribute * ia = cur->Inverse_attr();
559+
Inverse_attribute * ia = cur->Inverse_attr();
560560
cur = ( Inverse_attributeLinkNode * )( cur->NextNode() );
561561
return ia;
562562
}
@@ -811,6 +811,31 @@ EntityDescriptor::~EntityDescriptor() {
811811
delete _uniqueness_rules;
812812
}
813813

814+
/** initialize inverse attrs
815+
* call once per eDesc (once per EXPRESS entity type)
816+
* must be called _after_ init_Sdai* functions for any ia->inverted_entity_id_'s
817+
*
818+
*/
819+
void EntityDescriptor::InitIAttrs( entFinderFn entFinder ) {
820+
InverseAItr iai( &( InverseAttr() ) );
821+
Inverse_attribute * ia;
822+
while( 0 != ( ia = iai.NextInverse_attribute() ) ) {
823+
const AttrDescriptor * ad;
824+
const char * aid = ia->inverted_attr_id_();
825+
const char * eid = ia->inverted_entity_id_();
826+
const EntityDescriptor * e = entFinder( eid );
827+
AttrDescItr adl( e->ExplicitAttr() ); //TODO does this include inherited attrs? redefined? etc...
828+
while( ( 0 != ( ad = adl.NextAttrDesc() ) ) && !strcmp( aid, ad->Name() ) ) {
829+
// loop condition side effects do everything
830+
}
831+
if( !ad ) {
832+
std::cerr << "Inverse attr " << ia->Name() << " for " << Name() << ": cannot find AttrDescriptor " << aid << " for entity " << eid << "." << std::endl;
833+
//FIXME should we abort? or is there a sensible recovery path?
834+
}
835+
ia->inverted_attr_( ad );
836+
}
837+
}
838+
814839
const char * EntityDescriptor::GenerateExpress( std::string & buf ) const {
815840
std::string sstr;
816841
int count;
@@ -878,7 +903,7 @@ const char * EntityDescriptor::GenerateExpress( std::string & buf ) const {
878903
}
879904
/////////
880905

881-
InverseAItr iai( _inverseAttr );
906+
InverseAItr iai( &_inverseAttr );
882907

883908
iai.ResetItr();
884909
const Inverse_attribute * ia = iai.NextInverse_attribute();

src/clstepcore/ExpDict.h

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ class SC_CORE_EXPORT Schema : public Dictionary_instance {
664664
protected:
665665
const char * _name;
666666
EntityDescriptorList _entList; // list of entities in the schema
667+
EntityDescriptorList _entsWithInverseAttrs;
667668
TypeDescriptorList _typeList; // list of types in the schema
668669
TypeDescriptorList _unnamed_typeList; // list of unnamed types in the schema (for cleanup)
669670
Interface_spec _interface; // list of USE and REF interfaces (SDAI)
@@ -729,15 +730,31 @@ class SC_CORE_EXPORT Schema : public Dictionary_instance {
729730
EntityDescLinkNode * AddEntity( EntityDescriptor * ed ) {
730731
return _entList.AddNode( ed );
731732
}
733+
/// must be called in addition to AddEntity()
734+
EntityDescLinkNode * AddEntityWInverse( EntityDescriptor * ed ) {
735+
return _entsWithInverseAttrs.AddNode( ed );
736+
}
732737

733738
TypeDescLinkNode * AddType( TypeDescriptor * td ) {
734739
return _typeList.AddNode( td );
735740
}
736-
737741
TypeDescLinkNode * AddUnnamedType( TypeDescriptor * td ) {
738742
return _unnamed_typeList.AddNode( td );
739743
}
740744

745+
const EntityDescriptorList * Entities() const {
746+
return & _entList;
747+
}
748+
const EntityDescriptorList * EntsWInverse() const {
749+
return & _entsWithInverseAttrs;
750+
}
751+
const TypeDescriptorList * Types() const {
752+
return & _typeList;
753+
}
754+
const TypeDescriptorList * UnnamedTypes() const {
755+
return & _unnamed_typeList;
756+
}
757+
741758
// the whole schema
742759
void GenerateExpress( ostream & out ) const;
743760

@@ -811,7 +828,7 @@ class SC_CORE_EXPORT Inverse_attributeLinkNode : public SingleLinkNode {
811828
Inverse_attributeLinkNode();
812829
virtual ~Inverse_attributeLinkNode();
813830

814-
const Inverse_attribute * Inverse_attr() const {
831+
Inverse_attribute * Inverse_attr() const {
815832
return _invAttr;
816833
}
817834
void Inverse_attr( Inverse_attribute * ia ) {
@@ -833,18 +850,21 @@ class SC_CORE_EXPORT Inverse_attributeList : public SingleLinkList {
833850

834851
class SC_CORE_EXPORT InverseAItr {
835852
protected:
836-
const Inverse_attributeList & ial;
853+
const Inverse_attributeList * ial;
837854
const Inverse_attributeLinkNode * cur;
838855

839856
public:
840-
InverseAItr( const Inverse_attributeList & iaList );
857+
InverseAItr( const Inverse_attributeList * iaList );
841858
virtual ~InverseAItr();
842859

843-
void ResetItr() {
844-
cur = ( Inverse_attributeLinkNode * )( ial.GetHead() );
860+
void ResetItr( const Inverse_attributeList * iaList = 0 ) {
861+
if( iaList ) {
862+
ial = iaList;
863+
}
864+
cur = ( Inverse_attributeLinkNode * )( ial->GetHead() );
845865
}
846866

847-
const Inverse_attribute * NextInverse_attribute();
867+
Inverse_attribute * NextInverse_attribute();
848868
};
849869

850870
/**
@@ -1072,7 +1092,7 @@ class SC_CORE_EXPORT Inverse_attribute : public AttrDescriptor {
10721092
const char * _inverted_attr_id;
10731093
const char * _inverted_entity_id;
10741094
protected:
1075-
AttrDescriptor * _inverted_attr; // not implemented (?!)
1095+
const AttrDescriptor * _inverted_attr; // not implemented (?!) (perhaps this means "not used"?)
10761096
public:
10771097

10781098
Inverse_attribute(
@@ -1107,22 +1127,23 @@ class SC_CORE_EXPORT Inverse_attribute : public AttrDescriptor {
11071127
_inverted_entity_id = iei;
11081128
}
11091129

1110-
/// FIXME not implemented (?!)
1111-
class AttrDescriptor * inverted_attr_() {
1130+
/// FIXME not implemented (?!) (perhaps this means "not set"?)
1131+
//set _inverted_attr in an extra init step in generated code? any other way to ensure pointers are valid?
1132+
const class AttrDescriptor * inverted_attr_() const {
11121133
return _inverted_attr;
11131134
}
11141135

1115-
void inverted_attr_( AttrDescriptor * ia ) {
1136+
void inverted_attr_( const AttrDescriptor * ia ) {
11161137
_inverted_attr = ia;
11171138
}
11181139

11191140
// below are obsolete (and not implemented anyway)
1120-
class AttrDescriptor * InverseAttribute() {
1121-
return _inverted_attr;
1122-
}
1123-
void InverseOf( AttrDescriptor * invAttr ) {
1124-
_inverted_attr = invAttr;
1125-
}
1141+
// class AttrDescriptor * InverseAttribute() {
1142+
// return _inverted_attr;
1143+
// }
1144+
// void InverseOf( AttrDescriptor * invAttr ) {
1145+
// _inverted_attr = invAttr;
1146+
// }
11261147
};
11271148

11281149
/** \class SchRename
@@ -1473,9 +1494,14 @@ class SC_CORE_EXPORT EnumTypeDescriptor : public TypeDescriptor {
14731494
* will be building the same thing but using the new schema info.
14741495
* nodes (i.e. EntityDesc nodes) for each entity.
14751496
*/
1497+
1498+
class Registry;
1499+
14761500
class SC_CORE_EXPORT EntityDescriptor : public TypeDescriptor {
14771501

14781502
protected:
1503+
//used in InitIAttrs so we don't have to #include registry.h
1504+
14791505
SDAI_LOGICAL _abstractEntity;
14801506
SDAI_LOGICAL _extMapping;
14811507
// does external mapping have to be used to create an instance of
@@ -1487,6 +1513,7 @@ class SC_CORE_EXPORT EntityDescriptor : public TypeDescriptor {
14871513
Inverse_attributeList _inverseAttr; // OPTIONAL
14881514
std::string _supertype_stmt;
14891515
public:
1516+
typedef const EntityDescriptor * (*entFinderFn)(const char *);
14901517
Uniqueness_rule__set_var _uniqueness_rules; // initially a null pointer
14911518

14921519
// pointer to a function that will create a new instance of a SDAI_Application_instance
@@ -1502,6 +1529,8 @@ class SC_CORE_EXPORT EntityDescriptor : public TypeDescriptor {
15021529

15031530
virtual ~EntityDescriptor();
15041531

1532+
void InitIAttrs( entFinderFn entFinder );
1533+
15051534
const char * GenerateExpress( std::string & buf ) const;
15061535

15071536
const char * QualifiedName( std::string & s ) const;

src/clstepcore/ExpDict.inline.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ Inverse_attributeLinkNode * Inverse_attributeList::AddNode( Inverse_attribute *
162162
return node;
163163
}
164164

165-
InverseAItr::InverseAItr( const Inverse_attributeList & iaList )
165+
InverseAItr::InverseAItr( const Inverse_attributeList * iaList )
166166
: ial( iaList ) {
167-
cur = ( Inverse_attributeLinkNode * )( ial.GetHead() );
167+
cur = ( Inverse_attributeLinkNode * )( ial->GetHead() );
168168
}
169169

170170
InverseAItr::~InverseAItr() {

src/clstepcore/Registry.inline.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ SDAI_Application_instance * Registry::ObjCreate( const char * nm, const char * s
324324
se->Error().severity( SEVERITY_WARNING );
325325
se->Error().UserMsg( "ENTITY requires external mapping" );
326326
}
327-
se->eDesc = entd;
327+
se->setEDesc( entd );
328328
return se;
329329
} else {
330330
return ENTITY_NULL;

0 commit comments

Comments
 (0)