Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

customization - Customizing include-guard for Eclipse CDT

I want an automatically generated include-guard by creating a new C++-class with Eclipse/CDT, but I don't find any way to change the ${include_guard_symbol} attribute.

My wish is an include-guard with a namespace prefix like following:

#ifndef NAMSPACE1_NAMESPACE2_HEADER_HPP

But if I use #ifndef ${namespace_name}_${include_guard_symbol} for this, it will produce:

namepace1::namespace2::_HEADER_HPP

How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had a dig around in the source for CDT, and found an undocumented preference setting you can use to change what is generated by ${include_guard_symbol}. There's no GUI for it either, but if you add the codetemplates.includeGuardGenerationScheme setting to <projectpath>/.settings/org.eclipse.cdt.ui.prefs, you can choose between file name (the default), file path or UUID.

Given the file <projectpath>/src/include/Class.h, the following values give these results:

  • 0 gives an upper-case filename, i.e. CLASS_H_
  • 1 gives a UUID, for example. HC9ABE718_D04E_411C_B5A2_F9FE1D9F9409
  • 2 gives an upper-case file path, that is, SRC_INCLUDE_CLASS_H_

To avoid any doubt, here's the contents of our .settings/org.eclipse.cdt.ui.prefs:

codetemplates.includeGuardGenerationScheme=2
eclipse.preferences.version=1
formatter_settings_version=1

It's obviously not exactly what you're after, but we use 2 to give us an approximation of our namespaces since, generally speaking, our namespaces follow our folder structure.

The relevant code is in these files in the CDT source:

  • core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/PreferenceConstants.java for the constants for each option
  • core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/StubUtility.java for the generateIncludeGuardSymbol() method that does the work.

It would be really nice to see an extra option added for using the namespace, and a GUI too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...