Update of /cvsroot/cpptool/rfta/include/rfta/refactoring In directory sc8-pr-cvs1:/tmp/cvs-serv25617/include/rfta/refactoring Modified Files: CodeModel.h CodeModelElement.h CodeModelExpressions.h CodeModelStatements.h CodeModelVisitor.h Added Files: ChangeTrackers.h Log Message: * added support for appending a variable declaration in a declaration expression. --- NEW FILE: ChangeTrackers.h --- #ifndef RFTA_CHANGETRACKERS_H_INCLUDED #define RFTA_CHANGETRACKERS_H_INCLUDED #include <rfta/refactoring/CodeModelForward.h> #include <rfta/parser/SourceRange.h> #include <stdexcept> #include <vector> namespace Refactoring { namespace CodeModel { class RFTA_API Change { public: enum ChangeType { added = 0, replaced, removed, unmodified }; Change() : type_( unmodified ) { } Change( ChangeType type, const SourceRange &oldRange = SourceRange() ) : type_( type ) , oldRange_( oldRange ) { } bool wasReplaced() const { return type_ == replaced; } bool wasRemoved() const { return type_ == removed; } bool wasAdded() const { return type_ == added; } bool isUnmodified() const { return type_ == unmodified; } bool hasSourceRange() const { return !oldRange_.isEmpty(); } ChangeType type_; SourceRange oldRange_; }; struct NoConstraintPolicy { template<typename Type> static void checkConstraint( const Type & ) { } }; struct NotNullConstraintPolicy { template<typename Type> static void checkConstraint( const Type &pointer ) { if ( !pointer ) throw std::invalid_argument( "ChangeTracker: parameter can not be null." ); } }; // The tracked element always exist, so change may only be unmodified or replaced. template<typename TrackedType ,typename ConstraintPolicy = NotNullConstraintPolicy> class ChangeTracker { public: ChangeTracker( const TrackedType &element ) : element_( element ) , change_( Change::added ) { ConstraintPolicy::checkConstraint( element ); } void setElementIsFromSource() { change_.type_ = Change::unmodified; change_.oldRange_ = hasElement() ? element_->getSourceRange() : SourceRange(); } void setElement( const TrackedType &newElement ) { ConstraintPolicy::checkConstraint( newElement ); change_.type_ = Change::replaced; element_ = newElement; } bool hasElement() const { return element_; } TrackedType getElement() const { return element_; } Change getChange() const { return change_; } private: Change change_; TrackedType element_; }; typedef ChangeTracker<StatementPtr> StatementChangeTracker; typedef ChangeTracker<ExpressionPtr> ExpressionChangeTracker; // Tracked element may not exist, so change may be of any types. template<typename TrackedType> class OptionalChangeTracker { public: OptionalChangeTracker() : change_( Change::added ) { } OptionalChangeTracker( const TrackedType &element ) : element_( element ) , change_( Change::added ) { } void setElementIsFromSource() { change_.type_ = Change::unmodified; change_.oldRange_ = hasElement() ? element_->getSourceRange() : SourceRange(); } void setElement( const TrackedType &newElement ) { if ( newElement ) { change_.type_ = hadElementInSource() ? Change::replaced : Change::added; } else { change_.type_ = hadElementInSource() ? Change::removed : Change::unmodified; } element_ = newElement; } bool hasElement() const { return element_; } TrackedType getElement() const { return element_; } Change getChange() const { return change_; } private: bool hadElementInSource() const { return !change_.oldRange_.isEmpty(); } Change change_; TrackedType element_; }; template<typename TrackedType> class CompositeChangeTracker { public: void setElementIsFromSource() { for ( Changes::iterator it = changes_.begin(); it != changes_.end(); ++it ) it->setElementIsFromSource(); } void removeAt( int index ) { changes_.at( getActualIndex(index) ).setElement( TrackedType() ); } void insertAt( int index, const TrackedType &element ) { changes_.insert( changes_.begin() + getActualIndex( index ), Tracker(element) ); } void append( const TrackedType &element ) { changes_.push_back( Tracker(element) ); } void setAt( int index, const TrackedType &element ) { changes_.at( getActualIndex(index) ).setElement( element ); } int getElementCount() const { int count = 0; for ( Changes::const_iterator it = changes_.begin(); it != changes_.end(); ++it ) { if ( it->hasElement() ) ++count; } return count; } TrackedType getAt( int index ) const { return changes_.at( getActualIndex(index) ).getElement(); } int getChangeCount() const { return changes_.size(); } Change getChangeAt( int index ) const { return changes_.at( index ).getChange(); } TrackedType getChangeElementAt( int index ) const { return changes_.at( index ).getElement(); } private: int getActualIndex( int index ) const { int count = 0; for ( Changes::const_iterator it = changes_.begin(); it != changes_.end(); ++it ) { if ( it->hasElement() && count++ == index ) return it - changes_.begin(); } throw std::out_of_range( "index out of range for CompositeChangeTracker::getActualIndex()" ); } private: typedef OptionalChangeTracker<TrackedType> Tracker; typedef std::vector<Tracker> Changes; Changes changes_; }; typedef OptionalChangeTracker<StatementPtr> OptionalStatementChangeTracker; typedef OptionalChangeTracker<ExpressionPtr> OptionalExpressionChangeTracker; } // namespace CodeModel } // namespace Refactoring #endif // RFTA_CHANGETRACKERS_H_INCLUDED Index: CodeModel.h =================================================================== RCS file: /cvsroot/cpptool/rfta/include/rfta/refactoring/CodeModel.h,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** CodeModel.h 6 Apr 2003 07:42:16 -0000 1.4 --- CodeModel.h 10 Apr 2003 08:37:46 -0000 1.5 *************** *** 56,59 **** --- 56,61 ---- TypePartPtr getDeclaratorPrimaryType( const ASTNodePtr &declaratorNode ); + TypePartPtr makeOptionalType( const ASTNodePtr &typeNode ); + IdentifierPtr makeIdentifier( const ASTNodePtr &identifierNode ); DeclaratorPtr makeVariableDeclarator( const ASTNodePtr &variableDeclaratorNode ); ExpressionPtr makeVariableInitializerExpression( const ASTNodePtr &initializerNode ); Index: CodeModelElement.h =================================================================== RCS file: /cvsroot/cpptool/rfta/include/rfta/refactoring/CodeModelElement.h,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** CodeModelElement.h 5 Apr 2003 18:28:57 -0000 1.13 --- CodeModelElement.h 10 Apr 2003 08:37:46 -0000 1.14 *************** *** 7,15 **** #include <rfta/refactoring/CodeModelForward.h> - #include <rfta/parser/SourceRange.h> #include <rfta/parser/ASTNodeForward.h> #include <boost/enable_shared_from_this.hpp> #include <boost/utility.hpp> - #include <stdexcept> namespace Refactoring { namespace CodeModel --- 7,13 ---- *************** *** 59,253 **** ASTNodePtr node_; }; - - - class RFTA_API Change - { - public: - enum ChangeType - { - added = 0, - replaced, - removed, - unmodified - }; - - Change() - : type_( unmodified ) - { - } - - Change( ChangeType type, - const SourceRange &oldRange = SourceRange() ) - : type_( type ) - , oldRange_( oldRange ) - { - } - - bool wasReplaced() const - { - return type_ == replaced; - } - - bool wasRemoved() const - { - return type_ == removed; - } - - bool wasAdded() const - { - return type_ == added; - } - - bool isUnmodified() const - { - return type_ == unmodified; - } - - ChangeType type_; - SourceRange oldRange_; - }; - - - struct NoConstraintPolicy - { - template<typename Type> - static void checkConstraint( const Type & ) - { - } - }; - - - struct NotNullConstraintPolicy - { - template<typename Type> - static void checkConstraint( const Type &pointer ) - { - if ( !pointer ) - throw std::invalid_argument( "ChangeTracker: parameter can not be null." ); - } - }; - - - // The tracked element always exist, so change may only be unmodified or replaced. - template<typename TrackedType - ,typename ConstraintPolicy = NotNullConstraintPolicy> - class ChangeTracker - { - public: - ChangeTracker( const TrackedType &element ) - : element_( element ) - , change_( Change::added ) - { - ConstraintPolicy::checkConstraint( element ); - } - - void setElementIsFromSource() - { - change_.type_ = Change::unmodified; - change_.oldRange_ = hasElement() ? element_->getSourceRange() - : SourceRange(); - } - - void setElement( const TrackedType &newElement ) - { - ConstraintPolicy::checkConstraint( newElement ); - - change_.type_ = Change::replaced; - element_ = newElement; - } - - bool hasElement() const - { - return element_; - } - - TrackedType getElement() const - { - return element_; - } - - Change getChange() const - { - return change_; - } - - private: - Change change_; - TrackedType element_; - }; - - - typedef ChangeTracker<StatementPtr> StatementChangeTracker; - typedef ChangeTracker<ExpressionPtr> ExpressionChangeTracker; - - - - // Tracked element may not exist, so change may be of any types. - template<typename TrackedType> - class OptionalChangeTracker - { - public: - OptionalChangeTracker() - : change_( Change::added ) - { - } - - OptionalChangeTracker( const TrackedType &element ) - : element_( element ) - , change_( Change::added ) - { - } - - void setElementIsFromSource() - { - change_.type_ = Change::unmodified; - change_.oldRange_ = hasElement() ? element_->getSourceRange() - : SourceRange(); - } - - void setElement( const TrackedType &newElement ) - { - if ( newElement ) - { - change_.type_ = hadElementInSource() ? Change::replaced - : Change::added; - } - else - { - change_.type_ = hadElementInSource() ? Change::removed - : Change::unmodified; - } - - element_ = newElement; - } - - bool hasElement() const - { - return element_; - } - - TrackedType getElement() const - { - return element_; - } - - Change getChange() const - { - return change_; - } - - private: - bool hadElementInSource() const - { - return !change_.oldRange_.isEmpty(); - } - - Change change_; - TrackedType element_; - }; - - - typedef OptionalChangeTracker<StatementPtr> OptionalStatementChangeTracker; - typedef OptionalChangeTracker<ExpressionPtr> OptionalExpressionChangeTracker; --- 57,60 ---- Index: CodeModelExpressions.h =================================================================== RCS file: /cvsroot/cpptool/rfta/include/rfta/refactoring/CodeModelExpressions.h,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** CodeModelExpressions.h 6 Apr 2003 18:06:59 -0000 1.15 --- CodeModelExpressions.h 10 Apr 2003 08:37:47 -0000 1.16 *************** *** 6,10 **** #define RFTA_CODEMODELEXPRESSIONS_H ! #include <rfta/refactoring/Config.h> #include <rfta/refactoring/CodeModelForward.h> #include <rfta/refactoring/CodeModelElement.h> --- 6,10 ---- #define RFTA_CODEMODELEXPRESSIONS_H ! #include <rfta/refactoring/ChangeTrackers.h> #include <rfta/refactoring/CodeModelForward.h> #include <rfta/refactoring/CodeModelElement.h> *************** *** 42,46 **** void accept( ExpressionVisitor &visitor ); ! private: // overridden from CodeElement void setElementIsFromSource(); --- 42,46 ---- void accept( ExpressionVisitor &visitor ); ! private: // overridden from Element void setElementIsFromSource(); *************** *** 58,61 **** --- 58,64 ---- std::string getTypeText() const; + // overridden from Element + void accept( ElementVisitor &visitor ); + private: std::string typePart_; *************** *** 74,91 **** Change getPrimaryTypeChange() const; - int getDeclaratorCount() const; DeclaratorPtr getDeclaratorAt( int index ) const; void insertDeclaratorAt( int index, const DeclaratorPtr &declarator ); void appendDeclarator( const DeclaratorPtr &declarator ); void removeDeclaratorAt( int index ); // overridden from Expression void accept( ExpressionVisitor &visitor ); private: ! typedef std::vector<DeclaratorPtr> Declarators; ! Declarators declarators_; ChangeTracker<TypePartPtr> primaryTypeTracker_; }; --- 77,107 ---- Change getPrimaryTypeChange() const; int getDeclaratorCount() const; + DeclaratorPtr getDeclaratorAt( int index ) const; + void insertDeclaratorAt( int index, const DeclaratorPtr &declarator ); + void appendDeclarator( const DeclaratorPtr &declarator ); + void removeDeclaratorAt( int index ); + Change getChangeAt( int changeIndex ) const; + + DeclaratorPtr getChangeDeclaratorAt( int changeIndex ) const; + + int getChangeCount() const; + // overridden from Expression void accept( ExpressionVisitor &visitor ); + private: // overridden from Element + void setElementIsFromSource(); + private: ! // typedef std::vector<DeclaratorPtr> Declarators; ! // Declarators declarators_; ! CompositeChangeTracker<DeclaratorPtr> declaratorsTracker_; ChangeTracker<TypePartPtr> primaryTypeTracker_; }; *************** *** 99,102 **** --- 115,121 ---- std::string getIdentifier() const; + // overridden from Element + void accept( ElementVisitor &visitor ); + private: std::string identifier_; *************** *** 119,129 **** --- 138,159 ---- TypePartPtr getType() const; + void setType( const TypePartPtr &type ); + bool hasType() const; + + Change getTypeChange() const; + TypePartPtr getTypeSuffix() const; + void setTypeSuffix( const TypePartPtr &typeSuffix ); + bool hasTypeSuffix() const; + + Change getTypeSuffixChange() const; + IdentifierPtr getName() const; void setName( const IdentifierPtr &name ); + Change getNameChange() const; bool hasInitializer() const; *************** *** 134,141 **** void accept( ElementVisitor &visitor ); private: ! TypePartPtr type_; ! IdentifierPtr name_; ! TypePartPtr typeSuffix_; ExpressionPtr initializer_; }; --- 164,174 ---- void accept( ElementVisitor &visitor ); + private: // overridden from Element + void setElementIsFromSource(); + private: ! OptionalChangeTracker<TypePartPtr> typeTracker_; ! ChangeTracker<IdentifierPtr> nameTracker_; ! OptionalChangeTracker<TypePartPtr> typeSuffixTracker_; ExpressionPtr initializer_; }; Index: CodeModelStatements.h =================================================================== RCS file: /cvsroot/cpptool/rfta/include/rfta/refactoring/CodeModelStatements.h,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** CodeModelStatements.h 5 Apr 2003 18:28:57 -0000 1.19 --- CodeModelStatements.h 10 Apr 2003 08:37:47 -0000 1.20 *************** *** 6,13 **** #define RFTA_CODEMODELSTATEMENTS_H ! #include <rfta/refactoring/Config.h> #include <rfta/refactoring/CodeModelForward.h> #include <rfta/refactoring/CodeModelElement.h> - //#include <rfta/parser/SourceRange.h> #include <string> #include <vector> --- 6,12 ---- #define RFTA_CODEMODELSTATEMENTS_H ! #include <rfta/refactoring/ChangeTrackers.h> #include <rfta/refactoring/CodeModelForward.h> #include <rfta/refactoring/CodeModelElement.h> #include <string> #include <vector> *************** *** 31,46 **** { public: - struct StatementChange : public Change - { - StatementChange( ChangeType type, - const StatementPtr &statement ) - : Change( type ) - , statement_( statement ) - { - } - - StatementPtr statement_; - }; - void removeStatementAt( int index ); --- 30,33 ---- *************** *** 59,63 **** int getChangeCount() const; ! StatementChange getChangeAt( int changeIndex ) const; // overriden from Statement --- 46,52 ---- int getChangeCount() const; ! Change getChangeAt( int changeIndex ) const; ! ! StatementPtr getChangeStatementAt( int changeIndex ) const; // overriden from Statement *************** *** 68,76 **** private: ! int getActualIndex( int index ) const; ! ! private: ! typedef std::vector<StatementChange> Statements; ! Statements statements_; }; --- 57,61 ---- private: ! CompositeChangeTracker<StatementPtr> statementsTracker_; }; Index: CodeModelVisitor.h =================================================================== RCS file: /cvsroot/cpptool/rfta/include/rfta/refactoring/CodeModelVisitor.h,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** CodeModelVisitor.h 8 Apr 2003 08:06:36 -0000 1.6 --- CodeModelVisitor.h 10 Apr 2003 08:37:49 -0000 1.7 *************** *** 58,61 **** --- 58,67 ---- { public: + using StatementVisitor::visit; + using ExpressionVisitor::visit; + + virtual void visit( const TypePartPtr &element ) =0; + virtual void visit( const IdentifierPtr &element ) =0; + // should add Label }; |