Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-patch] New Class Wizard 'include guard' option

> 
> Hello,
> 
> The New Class Wizard is just great.  I tried to add an option to create a
> classic include guard around the header file.
> e.g for class Foo:
> 
> #ifndef FOO_H
> #define FOO_H
> 
> ...
> 
> #endif // FOO_H
> 

Personnaly, I would advocate that it is good practice to put a #ifdef guard
when doing a header for protection against multiple inclusions. Meaning
removing the "include" button are always have it "enable" by default.

> Maybe Hoda already thought about this, in this case sorry for the noise.
> 

Note,  it would make life easier, if you add a ChangeLog entry for the patch.
Patch apply with some modifications, to go around a bug in the C/C++ parse(see below).
======================================================================================
Index: ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/ChangeLog,v
retrieving revision 1.100
diff -u -r1.100 ChangeLog
--- ChangeLog	4 Jun 2003 19:34:54 -0000	1.100
+++ ChangeLog	6 Jun 2003 01:07:24 -0000
@@ -1,3 +1,16 @@
+2003-06-05 Alain Magloire
+
+	Patch from Christophe Juniet, this patch adds #ifdef guards
+	when generating a header for a class.
+
+	Note the field NewClassWizardPage.createClass change to ICElement
+	since the C/C++ consider:
+	class foo { };
+	like a variable instead IVariable instead of a IVariableDeclaration. 
+
+	* src/org/eclipse/cdt/ui/wizards/NewClassWizardPage.java
+	* src/org/eclipse/cdt/internal/ui/wizards/NewWizardMessages.properties.
+
 2003-05-23 Alain Magloire
 
 	Patch from Victor Mozgin to deal with PR 38405
Index: src/org/eclipse/cdt/internal/ui/wizards/NewWizardMessages.properties
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/NewWizardMessages.properties,v
retrieving revision 1.2
diff -u -r1.2 NewWizardMessages.properties
--- src/org/eclipse/cdt/internal/ui/wizards/NewWizardMessages.properties	30 May 2003 15:26:37 -0000	1.2
+++ src/org/eclipse/cdt/internal/ui/wizards/NewWizardMessages.properties	6 Jun 2003 01:07:25 -0000
@@ -146,6 +146,7 @@
 
 NewClassWizardPage.constdest.virtualdestructor=&Virtual Destructor
 NewClassWizardPage.constdest.inline=&Inline
+NewClassWizardPage.constdest.includeguard=Include &Guard
 
 NewClassWizardPage.files.header=Header File:
 NewClassWizardPage.files.body=Body File:
Index: src/org/eclipse/cdt/ui/wizards/NewClassWizardPage.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/wizards/NewClassWizardPage.java,v
retrieving revision 1.3
diff -u -r1.3 NewClassWizardPage.java
--- src/org/eclipse/cdt/ui/wizards/NewClassWizardPage.java	30 May 2003 15:26:37 -0000	1.3
+++ src/org/eclipse/cdt/ui/wizards/NewClassWizardPage.java	6 Jun 2003 01:07:25 -0000
@@ -101,7 +101,7 @@
 	private ITranslationUnit parentHeaderTU = null;
 	private ITranslationUnit parentBodyTU = null;
 	// the created class element
-	private IStructure createdClass = null;
+	private /*IStructure*/ ICElement createdClass = null;
 	
 	private ArrayList elementsOfTypeClassInProject = null;
 	
@@ -149,9 +149,10 @@
 		String[] buttonNames2= new String[] {
 			/* 0 == INLINE_INDEX */ NewWizardMessages.getString("NewClassWizardPage.constdest.inline"),
 			/* 1 == VIRTUAL_DEST_INDEX */ NewWizardMessages.getString("NewClassWizardPage.constdest.virtualdestructor"),
+			/* 2 == INCLUDE_GUARD_INDEX */ NewWizardMessages.getString("NewClassWizardPage.constdest.includeguard"),
 		};				
 		
-		fConstDestButtons= new SelectionButtonDialogFieldGroup(SWT.CHECK, buttonNames2, 4);
+		fConstDestButtons= new SelectionButtonDialogFieldGroup(SWT.CHECK, buttonNames2, 3);
 		fConstDestButtons.setDialogFieldListener(adapter);
 				
 		linkedResourceGroupForHeader = new LinkToFileGroup(adapter, this);
@@ -456,6 +457,10 @@
 		return fBaseClassDialogField.getText();
 	}
 	
+	public boolean isIncludeGuard(){
+		return fConstDestButtons.isSelected(2);	
+	}
+	
 	public boolean isVirtualDestructor(){
 		return fConstDestButtons.isSelected(1);
 	}
@@ -482,7 +487,7 @@
 		return parentBodyTU;
 	}
 
-	public IStructure getCreatedClassElement(){
+	public /*IStructure*/ ICElement getCreatedClassElement(){
 		return createdClass;
 	}
 	
@@ -514,7 +519,8 @@
 					headerWC.reconcile();	
 					headerWC.commit(true, monitor);
 				}
-				createdClass= (IStructure)headerWC.getElement(getNewClassName());				
+				//createdClass= (IStructure)headerWC.getElement(getNewClassName());				
+				createdClass= headerWC.getElement(getNewClassName());				
 			}
 			if(parentBodyTU != null){
 				String body = constructBodyFileContent(lineDelimiter);
@@ -742,6 +748,18 @@
 			}
 		}
 		
+		if(isIncludeGuard()){
+			text.append("#ifndef ");
+			text.append(getNewClassName().toUpperCase());
+			text.append("_H");
+			text.append(lineDelimiter);
+			text.append("#define ");
+			text.append(getNewClassName().toUpperCase());
+			text.append("_H");
+			text.append(lineDelimiter);
+			text.append(lineDelimiter);
+		}
+		
 		if(extendingBase){
 			text.append("#include \"");
 			text.append(baseClassFileName);
@@ -793,7 +811,15 @@
 		}
 		text.append("};");
 		text.append(lineDelimiter);		
-				
+		
+		if(isIncludeGuard()){
+			text.append(lineDelimiter);
+			text.append("#endif // ");
+			text.append(getNewClassName().toUpperCase());
+			text.append("_H");
+			text.append(lineDelimiter);		
+		}
+					
 		return text.toString();	 		
 	}
 		



Back to the top