Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10371 for code/branches/core7


Ignore:
Timestamp:
Apr 18, 2015, 12:46:57 PM (10 years ago)
Author:
landauf
Message:

trying to make the initialization of parents of an identifier better understandable in code

Location:
code/branches/core7/src/libraries/core/class
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core7/src/libraries/core/class/Identifier.cc

    r10366 r10371  
    154154        }
    155155
    156         for (std::set<const Identifier*>::const_iterator it = initializationTrace.begin(); it != initializationTrace.end(); ++it)
    157             if (*it != this)
    158                 this->parents_.insert(*it);
    159     }
    160 
    161     /**
    162      * @brief Initializes the direct parents of this Identifier while creating the class hierarchy. This is only intended for abstract classes.
     156        if (this->directParents_.empty())
     157        {
     158            for (std::set<const Identifier*>::const_iterator it = initializationTrace.begin(); it != initializationTrace.end(); ++it)
     159                if (*it != this)
     160                    this->parents_.insert(*it);
     161        }
     162        else
     163            orxout(internal_error) << "Trying to add parents to " << this->getName() << " after it was already initialized with manual calls to inheritsFrom<Class>()." << endl;
     164    }
     165
     166    /**
     167     * @brief Finishes the initialization of this Identifier after creating the class hierarchy by wiring the (direct) parent/child references correctly.
    163168     */
    164     void Identifier::initializeDirectParentsOfAbstractClass()
     169    void Identifier::finishInitialization()
    165170    {
    166171        if (!IdentifierManager::getInstance().isCreatingHierarchy())
    167172        {
    168             orxout(internal_warning) << "Identifier::initializeDirectParentsOfAbstractClass() created outside of class hierarchy creation" << endl;
     173            orxout(internal_warning) << "Identifier::finishInitialization() created outside of class hierarchy creation" << endl;
    169174            return;
    170175        }
    171176
    172         // only Identifiable is allowed to have no parents (even tough it's currently not abstract)
    173         if (this->directParents_.empty() && !this->isExactlyA(Class(Identifiable)))
    174         {
     177        if (this->isInitialized())
     178            return;
     179
     180        if (!this->parents_.empty())
     181        {
     182            // parents defined -> this class was initialized by creating a sample instance and recording the trace of identifiers
     183
     184            // initialize all parents
     185            for (std::set<const Identifier*>::const_iterator it = this->parents_.begin(); it != this->parents_.end(); ++it)
     186                const_cast<Identifier*>(*it)->finishInitialization(); // initialize parent
     187
     188            // parents of parents are no direct parents of this identifier
     189            this->directParents_ = this->parents_;
     190            for (std::set<const Identifier*>::const_iterator it_parent = this->parents_.begin(); it_parent != this->parents_.end(); ++it_parent)
     191                for (std::set<const Identifier*>::const_iterator it_parent_parent = const_cast<Identifier*>(*it_parent)->parents_.begin(); it_parent_parent != const_cast<Identifier*>(*it_parent)->parents_.end(); ++it_parent_parent)
     192                    this->directParents_.erase(*it_parent_parent);
     193        }
     194        else if (!this->directParents_.empty())
     195        {
     196            // no parents defined -> this class was manually initialized by calling inheritsFrom<Class>()
     197
     198            // initialize all direct parents
     199            for (std::set<const Identifier*>::const_iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
     200                const_cast<Identifier*>(*it)->finishInitialization(); // initialize parent
     201
     202            // direct parents and their parents are also parents of this identifier (but only add them once)
     203            this->parents_ = this->directParents_;
     204            for (std::set<const Identifier*>::const_iterator it_parent = this->directParents_.begin(); it_parent != this->directParents_.end(); ++it_parent)
     205                for (std::set<const Identifier*>::const_iterator it_parent_parent = const_cast<Identifier*>(*it_parent)->parents_.begin(); it_parent_parent != const_cast<Identifier*>(*it_parent)->parents_.end(); ++it_parent_parent)
     206                    if (std::find(this->parents_.begin(), this->parents_.end(), *it_parent_parent) == this->parents_.end())
     207                        this->parents_.insert(*it_parent_parent);
     208        }
     209        else if (!this->isExactlyA(Class(Identifiable)))
     210        {
     211            // only Identifiable is allowed to have no parents (even tough it's currently not abstract)
    175212            orxout(internal_error) << "Identifier " << this->getName() << " / " << this->getTypeidName() << " is marked as abstract but has no direct parents defined" << endl;
    176213            orxout(internal_error) << "  If this class is not abstract, use RegisterClass(ThisClass);" << endl;
    177214            orxout(internal_error) << "  If this class is abstract, use RegisterAbstractClass(ThisClass).inheritsFrom(Class(BaseClass));" << endl;
    178215        }
    179     }
    180 
    181     /**
    182      * @brief Finishes the initialization of this Identifier after creating the class hierarchy by wiring the (direct) parent/child references correctly.
    183      */
    184     void Identifier::finishInitialization()
    185     {
    186         if (!IdentifierManager::getInstance().isCreatingHierarchy())
    187         {
    188             orxout(internal_warning) << "Identifier::finishInitialization() created outside of class hierarchy creation" << endl;
    189             return;
    190         }
    191 
    192         if (this->isInitialized())
    193             return;
    194 
    195         // if no direct parents were defined, initialize them with the set of all parents
    196         if (this->directParents_.empty())
    197             this->directParents_ = this->parents_;
    198 
    199         // initialize all parents before continuing to initialize this identifier
    200         for (std::set<const Identifier*>::const_iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
    201         {
    202             Identifier* directParent = const_cast<Identifier*>(*it);
    203             directParent->finishInitialization(); // initialize parent
    204             this->parents_.insert(directParent);  // direct parent is also a parent
    205             this->parents_.insert(directParent->parents_.begin(), directParent->parents_.end()); // parents of direct parent are also parents
    206         }
    207 
    208         // parents of parents are no direct parents of this identifier
    209         for (std::set<const Identifier*>::const_iterator it_parent = this->parents_.begin(); it_parent != this->parents_.end(); ++it_parent)
    210             for (std::set<const Identifier*>::const_iterator it_parent_parent = const_cast<Identifier*>(*it_parent)->parents_.begin(); it_parent_parent != const_cast<Identifier*>(*it_parent)->parents_.end(); ++it_parent_parent)
    211                 this->directParents_.erase(*it_parent_parent);
    212216
    213217        // tell all parents that this identifier is a child
     
    233237    bool Identifier::isA(const Identifier* identifier) const
    234238    {
    235         return (identifier == this || (this->parents_.find(identifier) != this->parents_.end()));
     239        return (identifier == this || (this->isChildOf(identifier)));
    236240    }
    237241
  • code/branches/core7/src/libraries/core/class/Identifier.h

    r10367 r10371  
    149149
    150150            void initializeParents(const std::set<const Identifier*>& initializationTrace);
    151             void initializeDirectParentsOfAbstractClass();
    152151            void finishInitialization();
    153152
  • code/branches/core7/src/libraries/core/class/IdentifierManager.cc

    r10370 r10371  
    133133                    delete temp;
    134134                }
    135                 else
    136                     it->second->initializeDirectParentsOfAbstractClass();
    137135
    138136                initializedIdentifiers.insert(it->second);
Note: See TracChangeset for help on using the changeset viewer.