Author |
Topic  |
|
20thCenturyBoy
Junior Member
 
Australia
11 Posts |
Posted - Oct 30 2004 : 04:31:06 AM
|
What is the simplest way to automatically add an include guard around selected text in a header file? Eg
#ifndef GUARD_HEADER_H // include guard
#define GUARD_HEADER_H // include guard
#include <vector>
etc...
#endif // include guard Ideally I would like the word HEADER to be automatically changed to the name of the file minus the .h.
Does anyone else do this using VAX?
Martin
|
|
WannabeeDeveloper
Tomato Guru
    
Germany
775 Posts |
Posted - Oct 30 2004 : 04:58:35 AM
|
No, I do not use that, but I like the idea... Let's see if we can get that one together...
Instructions for the Autotext/Code Template feature.
According to the Instructions, we can use Environment Variables or VAX-defined Variables, where %FILE_BASE% is "name of the File without Path and Extension".
So we can add this to the CPP.TPL:
Insert Include Guard:
#ifndef %FILE_BASE%_H // include guard
#define %FILE_BASE%_H // include guard
-?
#endif // include guard
(That square tile under #endif is the "FF" char within the VAX-Template-Editor) Have a look:

Now all you need to do is invoke the enhanced rightclick-menu (SHIFT + rightclick into source) and choose "Insert code template -> Insert Include Guard".

So, let's test it in a real example: Imagine you have a file called InitInstances.h. It is IMPORTANT that you save the header-file under its name prior to adding the include-guard. Using the rightclick-menu-method we would end up with this code:
#ifndef InitInstances_H // include guard
#define InitInstances_H // include guard
|
#endif // include guard
Note that '|' is where the caret appears (we told our Autotext-Template to do so by putting a -? at the place we want the caret at after inserting the Autotext).
...and you're done! |
 |
Edited by - WannabeeDeveloper on Oct 30 2004 05:04:59 AM |
 |
|
20thCenturyBoy
Junior Member
 
Australia
11 Posts |
Posted - Oct 30 2004 : 06:08:59 AM
|
Hey that works perfectly! Thanks for the quick response.
I also added the %0 option, so the full template is:
Insert Include Guard:
#ifndef %FILE_BASE%_H // include guard
#define %FILE_BASE%_H // include guard
%0-?
#endif // include guard
which allows you to put the include guard around selected text.
This really is a flexible tool, I'm going to investigate some more!
Martin |
 |
|
jpizzi
Tomato Guru
    
USA
642 Posts |
Posted - Oct 31 2004 : 5:56:39 PM
|
Hmmm. Been using that one for a couple of years (with VA). I thought it was a natural to be added (although I never suggested it be added to the example autotext files that come with VA).
An additional enhancement that I would like (I think I have read others asking for it, too) is the ability to have the autotext forced to upper-case, so that in the above example, the resulting ifdef line became: #ifdef INITINSTANCES_H Another thing I do, since I never have to type the symbol, is I name mine, %FILE_BASE%_HEADER_INCLUDED The extra verbose symbol helps me to ensure that I don't accidently re-use the ifdef (perhaps when debugging)...
|
Joe Pizzi |
 |
|
WannabeeDeveloper
Tomato Guru
    
Germany
775 Posts |
Posted - Nov 01 2004 : 02:20:06 AM
|
I'm sure if we open up a single thread (maybe in the lounge) with a "Suggestions for Autotext-Templates delivered with VAX" subject, we can convince the devs to include them in the VAX-Setup...
At least we'd have a base for starters, they could put those template-suggestions into their template-files (*.tpl)? |
 |
 |
|
Stephen
Tomato Guru
    
United Kingdom
781 Posts |
Posted - Nov 01 2004 : 04:29:14 AM
|
Everyone knows about "#pragma once", right? Of course, it's less portable than the "#ifdef _FILE_H" trick. |
Stephen Turner ClickTracks http://www.clicktracks.com/ Winner: ClickZ's Best Web Analytics Tool 2003 & 2004
|
 |
|
feline
Whole Tomato Software
    
United Kingdom
19177 Posts |
Posted - Nov 01 2004 : 6:13:27 PM
|
nope, i only discovered #pragma once a couple of weeks ago, when following up a thread from this forum. all of the windows header files i have seen at work use the #ifdef method, so i suspect #pragma once isn't that widely known. |
zen is the art of being at one with the two'ness |
 |
|
ivan
Ketchup Master
   
Russia
75 Posts |
Posted - Nov 01 2004 : 7:31:00 PM
|
#pragma once is MS-specific (I think Intel C++ Compiler supports it, too) but it has been around since VS6. Why it isn't widely known puzzles me - most programs created in VS are only compiled in VS... |
 |
|
|
Topic  |
|