[Gptfdisk-general] [PATCH 12/15] Explicitly namespace-qualify all std names in header files
Brought to you by:
srs5694
From: Bin M. <bme...@gm...> - 2022-04-02 07:01:03
|
The windows executables fail to compile with gcc 11 mingw cross-compile toolchain which defaults to '-std=c++17', due to conflicting definitions. In file included from /usr/x86_64-w64-mingw32/sys-root/mingw/include/wtypes.h:8, from /usr/x86_64-w64-mingw32/sys-root/mingw/include/winscard.h:10, from /usr/x86_64-w64-mingw32/sys-root/mingw/include/windows.h:97, from /usr/x86_64-w64-mingw32/sys-root/mingw/include/rpc.h:16, from guid.h:23, from parttypes.h:16, from gptpart.h:22, from gptpart.cc:27: /usr/x86_64-w64-mingw32/sys-root/mingw/include/rpcndr.h:64:11: error:reference to 'byte' is ambiguous 64 | typedef byte cs_byte; | ^~~~ In file included from /usr/x86_64-w64-mingw32/sys-root/mingw/include/c++/bits/stl_algobase.h:61, from /usr/x86_64-w64-mingw32/sys-root/mingw/include/c++/bits/char_traits.h:39, from /usr/x86_64-w64-mingw32/sys-root/mingw/include/c++/ios:40, from /usr/x86_64-w64-mingw32/sys-root/mingw/include/c++/ostream:38, from /usr/x86_64-w64-mingw32/sys-root/mingw/include/c++/iostream:39, from gptpart.cc:26: /usr/x86_64-w64-mingw32/sys-root/mingw/include/c++/bits/cpp_type_traits.h:404:30: note: candidates are: 'enum class std::byte' 404 | enum class byte : unsigned char; | ^~~~ The issue has been unnoticed so far all the way up to gcc 10, which defaulted to '-std=c++14'. But it can still be reproduced with gcc 10 with an explicit '-std=c++17' flag. "using namespace std" in header files is considered as a bad practice, often known as global namespace pollution. Problems may occur when more than one namespace has the same symbol name with signature, then it will be ambiguous for the compiler to decide which one to use. Let's explicitly namespace-qualify all std names in header files, and move "using namespace std" to *.cc files. Signed-off-by: Bin Meng <bme...@gm...> --- attributes.h | 10 ++++------ basicmbr.h | 12 +++++------- bsd.h | 5 +---- diskio.h | 16 +++++++--------- gdisk.cc | 2 ++ gpt.h | 20 +++++++++----------- gptcl.cc | 2 ++ gptcl.h | 10 ++++------ gptcurses.h | 10 ++++------ gptpart.cc | 11 +++++------ gptpart.h | 6 ++---- gpttext.h | 12 +++++------- guid.h | 12 +++++------- mbr.h | 4 +--- mbrpart.h | 2 -- parttypes.h | 16 +++++++--------- support.h | 16 +++++++--------- 17 files changed, 70 insertions(+), 96 deletions(-) diff --git a/attributes.h b/attributes.h index 6a61b8c..5eb57e3 100644 --- a/attributes.h +++ b/attributes.h @@ -10,11 +10,9 @@ #define NUM_ATR 64 /* # of attributes -- 64, since it's a 64-bit field */ #define ATR_NAME_SIZE 25 /* maximum size of attribute names */ -using namespace std; - class Attributes { protected: - static string atNames[NUM_ATR]; + static std::string atNames[NUM_ATR]; static int numAttrs; void Setup(void); uint64_t attributes; @@ -30,12 +28,12 @@ public: void ShowAttributes(const uint32_t partNum); void ChangeAttributes(void); - bool OperateOnAttributes(const uint32_t partNum, const string& attributeOperator, const string& attributeBits); + bool OperateOnAttributes(const uint32_t partNum, const std::string& attributeOperator, const std::string& attributeBits); - static const string& GetAttributeName(const uint32_t bitNum) {return atNames [bitNum];} + static const std::string& GetAttributeName(const uint32_t bitNum) {return atNames [bitNum];} static void ListAttributes(void); }; // class Attributes -ostream & operator<<(ostream & os, const Attributes & data); +std::ostream & operator<<(std::ostream & os, const Attributes & data); #endif diff --git a/basicmbr.h b/basicmbr.h index f4b0e45..696f826 100644 --- a/basicmbr.h +++ b/basicmbr.h @@ -16,8 +16,6 @@ // Maximum number of MBR partitions #define MAX_MBR_PARTS 128 -using namespace std; - /**************************************** * * * MBRData class and related structures * @@ -57,23 +55,23 @@ protected: uint32_t numSecspTrack; // number of sectors per track, in CHS scheme DiskIO* myDisk; int canDeleteMyDisk; - string device; + std::string device; MBRValidity state; MBRPart* GetPartition(int i); // Return primary or logical partition public: BasicMBRData(void); - BasicMBRData(string deviceFilename); + BasicMBRData(std::string deviceFilename); BasicMBRData(const BasicMBRData &); ~BasicMBRData(void); BasicMBRData & operator=(const BasicMBRData & orig); // File I/O functions... - int ReadMBRData(const string & deviceFilename); + int ReadMBRData(const std::string & deviceFilename); int ReadMBRData(DiskIO * theDisk, int checkBlockSize = 1); int ReadLogicalParts(uint64_t extendedStart, int partNum); int WriteMBRData(void); int WriteMBRData(DiskIO *theDisk); - int WriteMBRData(const string & deviceFilename); + int WriteMBRData(const std::string & deviceFilename); int WriteMBRData(struct TempMBR & mbr, DiskIO *theDisk, uint64_t sector); void DiskSync(void) {myDisk->DiskSync();} void SetDisk(DiskIO *theDisk); @@ -147,7 +145,7 @@ public: uint64_t GetLength(int i); // User interaction functions.... - int DoMenu(const string& prompt = "\nMBR command (? for help): "); + int DoMenu(const std::string& prompt = "\nMBR command (? for help): "); void ShowCommands(void); }; // class BasicMBRData diff --git a/bsd.h b/bsd.h index c4b74a4..e042b7b 100644 --- a/bsd.h +++ b/bsd.h @@ -30,9 +30,6 @@ // memory errors will occur. #define MAX_BSD_PARTS 64 - -using namespace std; - /**************************************** * * * BSDData class and related structures * @@ -75,7 +72,7 @@ class BSDData { public: BSDData(void); ~BSDData(void); - int ReadBSDData(const string & deviceFilename, uint64_t startSector, uint64_t endSector); + int ReadBSDData(const std::string & deviceFilename, uint64_t startSector, uint64_t endSector); int ReadBSDData(DiskIO *myDisk, uint64_t startSector, uint64_t endSector); void ReverseMetaBytes(void); void DisplayBSDData(void); diff --git a/diskio.h b/diskio.h index 0bdaba4..b5c2ecc 100644 --- a/diskio.h +++ b/diskio.h @@ -37,8 +37,6 @@ #include "support.h" //#include "parttypes.h" -using namespace std; - /*************************************** * * * DiskIO class and related structures * @@ -47,9 +45,9 @@ using namespace std; class DiskIO { protected: - string userFilename; - string realFilename; - string modelName; + std::string userFilename; + std::string realFilename; + std::string modelName; int isOpen; int openForWrite; #ifdef _WIN32 @@ -62,9 +60,9 @@ class DiskIO { ~DiskIO(void); void MakeRealName(void); - int OpenForRead(const string & filename); + int OpenForRead(const std::string & filename); int OpenForRead(void); - int OpenForWrite(const string & filename); + int OpenForWrite(const std::string & filename); int OpenForWrite(void); void Close(); int Seek(uint64_t sector); @@ -73,12 +71,12 @@ class DiskIO { int DiskSync(void); // resync disk caches to use new partitions int GetBlockSize(void); int GetPhysBlockSize(void); - string GetModel(void) {return modelName;} + std::string GetModel(void) {return modelName;} uint32_t GetNumHeads(void); uint32_t GetNumSecsPerTrack(void); int IsOpen(void) {return isOpen;} int IsOpenForWrite(void) {return openForWrite;} - string GetName(void) const {return realFilename;} + std::string GetName(void) const {return realFilename;} uint64_t DiskSize(int* err); }; // class DiskIO diff --git a/gdisk.cc b/gdisk.cc index 5f85498..c9443bd 100644 --- a/gdisk.cc +++ b/gdisk.cc @@ -11,6 +11,8 @@ #include <iostream> #include "gpttext.h" +using namespace std; + int main(int argc, char* argv[]) { GPTDataTextUI theGPT; string device = ""; diff --git a/gpt.h b/gpt.h index cf05f59..5d19372 100644 --- a/gpt.h +++ b/gpt.h @@ -24,8 +24,6 @@ // smallest Advanced Format drive I know of is 320GB in size #define SMALLEST_ADVANCED_FORMAT UINT64_C(585937500) -using namespace std; - /**************************************** * * * GPTData class and related structures * @@ -67,7 +65,7 @@ protected: uint32_t numParts; // # of partitions the table can hold struct GPTHeader secondHeader; MBRData protectiveMBR; - string device; // device filename + std::string device; // device filename DiskIO myDisk; uint32_t blockSize; // device logical block size uint32_t physBlockSize; // device physical block size (or 0 if it can't be determined) @@ -93,7 +91,7 @@ public: // Basic necessary functions.... GPTData(void); GPTData(const GPTData &); - GPTData(string deviceFilename); + GPTData(std::string deviceFilename); virtual ~GPTData(void); GPTData & operator=(const GPTData & orig); @@ -111,18 +109,18 @@ public: int FindInsanePartitions(void); // Load or save data from/to disk - int SetDisk(const string & deviceFilename); + int SetDisk(const std::string & deviceFilename); DiskIO* GetDisk(void) {return &myDisk;} - int LoadMBR(const string & f) {return protectiveMBR.ReadMBRData(f);} + int LoadMBR(const std::string & f) {return protectiveMBR.ReadMBRData(f);} int WriteProtectiveMBR(void) {return protectiveMBR.WriteMBRData(&myDisk);} void PartitionScan(void); - int LoadPartitions(const string & deviceFilename); + int LoadPartitions(const std::string & deviceFilename); int ForceLoadGPTData(void); int LoadMainTable(void); int LoadSecondTableAsMain(void); int SaveGPTData(int quiet = 0); - int SaveGPTBackup(const string & filename); - int LoadGPTBackup(const string & filename); + int SaveGPTBackup(const std::string & filename); + int LoadGPTBackup(const std::string & filename); int SaveMBR(void); int DestroyGPT(void); int DestroyMBR(void); @@ -204,9 +202,9 @@ public: void ReversePartitionBytes(); // for endianness // Attributes functions - int ManageAttributes(int partNum, const string & command, const string & bits); + int ManageAttributes(int partNum, const std::string & command, const std::string & bits); void ShowAttributes(const uint32_t partNum); - void GetAttribute(const uint32_t partNum, const string& attributeBits); + void GetAttribute(const uint32_t partNum, const std::string& attributeBits); }; // class GPTData diff --git a/gptcl.cc b/gptcl.cc index 0141994..34c9421 100644 --- a/gptcl.cc +++ b/gptcl.cc @@ -26,6 +26,8 @@ #include <popt.h> #include "gptcl.h" +using namespace std; + GPTDataCL::GPTDataCL(void) { attributeOperation = backupFile = partName = hybrids = newPartInfo = NULL; mbrParts = twoParts = outDevice = typeCode = partGUID = diskGUID = NULL; diff --git a/gptcl.h b/gptcl.h index 183b846..57d3f5d 100644 --- a/gptcl.h +++ b/gptcl.h @@ -25,8 +25,6 @@ #include "gpt.h" #include <popt.h> -using namespace std; - class GPTDataCL : public GPTData { protected: // Following are variables associated with popt parameters.... @@ -41,14 +39,14 @@ class GPTDataCL : public GPTData { int BuildMBR(char* argument, int isHybrid); public: GPTDataCL(void); - GPTDataCL(string filename); + GPTDataCL(std::string filename); ~GPTDataCL(void); - void LoadBackupFile(string backupFile, int &saveData, int &neverSaveData); + void LoadBackupFile(std::string backupFile, int &saveData, int &neverSaveData); int DoOptions(int argc, char* argv[]); }; // class GPTDataCL int CountColons(char* argument); -uint64_t GetInt(const string & argument, int itemNum); -string GetString(string argument, int itemNum); +uint64_t GetInt(const std::string & argument, int itemNum); +std::string GetString(std::string argument, int itemNum); #endif diff --git a/gptcurses.h b/gptcurses.h index a080987..8d2ecaf 100644 --- a/gptcurses.h +++ b/gptcurses.h @@ -27,12 +27,10 @@ #include "gptpart.h" #include "gpt.h" -using namespace std; - struct MenuItem { int key; // Keyboard shortcut - string name; // Item name; 8 characters - string desc; // Description + std::string name; // Item name; 8 characters + std::string desc; // Description }; static struct MenuItem menuMain[] = { @@ -81,7 +79,7 @@ protected: Space *lastSpace; Space *currentSpace; int currentSpaceNum; - string whichOptions; + std::string whichOptions; char currentKey; int numSpaces; int displayType; @@ -130,7 +128,7 @@ public: void ClearLine(int lineNum); void ClearBottom(void); void PromptToContinue(void); -void Report(string theText); +void Report(std::string theText); void ShowTypes(void); #endif diff --git a/gptpart.cc b/gptpart.cc index b83254d..5fe7b5a 100644 --- a/gptpart.cc +++ b/gptpart.cc @@ -15,17 +15,16 @@ #define __STDC_LIMIT_MACROS #define __STDC_CONSTANT_MACROS -#ifdef USE_UTF16 -#include <unicode/ustdio.h> -#else -#define UnicodeString string -#endif - #include <string.h> #include <stdio.h> #include <iostream> #include "gptpart.h" #include "attributes.h" +#ifdef USE_UTF16 +#include <unicode/ustdio.h> +#else +#define UnicodeString std::string +#endif using namespace std; diff --git a/gptpart.h b/gptpart.h index 51bfb38..ede8139 100644 --- a/gptpart.h +++ b/gptpart.h @@ -23,8 +23,6 @@ #include "guid.h" #include "attributes.h" -using namespace std; - // Values returned by GPTPart::IsSizedForMBR() #define MBR_SIZED_GOOD 0 /* Whole partition under 2^32 sectors */ #define MBR_SIZED_IFFY 1 /* Partition starts under 2^32 & is less than 2^32, but ends over 2^32 */ @@ -59,7 +57,7 @@ class GPTPart { // Simple data retrieval: PartType & GetType(void) {return partitionType;} uint16_t GetHexType(void) const; - string GetTypeName(void); + std::string GetTypeName(void); UnicodeString GetUTypeName(void); const GUIDData GetUniqueGUID(void) const {return uniqueGUID;} uint64_t GetFirstLBA(void) const {return firstLBA;} @@ -80,7 +78,7 @@ class GPTPart { void SetLastLBA(uint64_t l) {lastLBA = l;} void SetAttributes(uint64_t a) {attributes = a;} void SetAttributes(void) {attributes.ChangeAttributes();} - void SetName(const string & theName); + void SetName(const std::string & theName); #ifdef USE_UTF16 void SetName(const UnicodeString & theName); #endif diff --git a/gpttext.h b/gpttext.h index 36a17f9..32e2f88 100644 --- a/gpttext.h +++ b/gpttext.h @@ -23,13 +23,11 @@ #include "gpt.h" -using namespace std; - class GPTDataTextUI : public GPTData { protected: public: GPTDataTextUI(void); - GPTDataTextUI(string filename); + GPTDataTextUI(std::string filename); ~GPTDataTextUI(void); // This one needs to be explicitly defined, even though it does nothing new.... @@ -55,17 +53,17 @@ class GPTDataTextUI : public GPTData { void ShowDetails(void); void MakeHybrid(void); int XFormToMBR(void); // convert GPT to MBR, wiping GPT afterwards. Returns 1 if successful - uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, const string & prompt); + uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, const std::string & prompt); // An informational function.... void WarnAboutIffyMBRPart(int partNum); // Main menu functions - void MainMenu(string filename); + void MainMenu(std::string filename); void ShowCommands(void); - void ExpertsMenu(string filename); + void ExpertsMenu(std::string filename); void ShowExpertCommands(void); - void RecoveryMenu(string filename); + void RecoveryMenu(std::string filename); void ShowRecoveryCommands(void); }; // class GPTDataTextUI diff --git a/guid.h b/guid.h index 229d5bd..515a75d 100644 --- a/guid.h +++ b/guid.h @@ -30,8 +30,6 @@ typedef unsigned char my_uuid_t[16]; typedef uuid_t my_uuid_t; #endif -using namespace std; - // Note: This class's data size is critical. If data elements must be added, // it will be necessary to modify various GPT classes to compensate. class GUIDData { @@ -39,17 +37,17 @@ class GUIDData { static bool firstInstance; protected: my_uuid_t uuidData; - string DeleteSpaces(string s); + std::string DeleteSpaces(std::string s); public: GUIDData(void); GUIDData(const GUIDData & orig); - GUIDData(const string & orig); + GUIDData(const std::string & orig); GUIDData(const char * orig); ~GUIDData(void); // Data assignment operators.... GUIDData & operator=(const GUIDData & orig); - GUIDData & operator=(const string & orig); + GUIDData & operator=(const std::string & orig); GUIDData & operator=(const char * orig); void Zero(void); void Randomize(void); @@ -59,9 +57,9 @@ class GUIDData { int operator!=(const GUIDData & orig) const; // Data retrieval.... - string AsString(void) const; + std::string AsString(void) const; }; // class GUIDData -ostream & operator<<(ostream & os, const GUIDData & data); +std::ostream & operator<<(std::ostream & os, const GUIDData & data); #endif diff --git a/mbr.h b/mbr.h index 21c1d7b..3eea775 100644 --- a/mbr.h +++ b/mbr.h @@ -13,8 +13,6 @@ #include "diskio.h" #include "basicmbr.h" -using namespace std; - /**************************************** * * * MBRData class and related structures * @@ -26,7 +24,7 @@ class MBRData : public BasicMBRData { protected: public: MBRData(void) {} - MBRData(string deviceFilename) : BasicMBRData(deviceFilename) {} + MBRData(std::string deviceFilename) : BasicMBRData(deviceFilename) {} MBRData & operator=(const BasicMBRData & orig); ~MBRData(void); diff --git a/mbrpart.h b/mbrpart.h index 0de365f..6c7b21a 100644 --- a/mbrpart.h +++ b/mbrpart.h @@ -32,8 +32,6 @@ #define EBR 4 /* sector is used as an EBR or MBR */ #define INVALID 8 /* sector number is too large for disk */ -using namespace std; - // Data for a single MBR partition record // Note that firstSector and lastSector are in CHS addressing, which // splits the bits up in a weird way. diff --git a/parttypes.h b/parttypes.h index 92f3d64..4e1d968 100644 --- a/parttypes.h +++ b/parttypes.h @@ -6,16 +6,14 @@ #include <stdint.h> #include <stdlib.h> +#include <string> +#include "support.h" +#include "guid.h" #ifdef USE_UTF16 #include <unicode/ustream.h> #else -#define UnicodeString string +#define UnicodeString std::string #endif -#include <string> -#include "support.h" -#include "guid.h" - -using namespace std; // A partition type struct AType { @@ -24,7 +22,7 @@ struct AType { // codes required by GPT uint16_t MBRType; GUIDData GUIDType; - string name; + std::string name; int display; // 1 to show to users as available type, 0 not to AType* next; }; // struct AType @@ -48,7 +46,7 @@ public: int AddType(uint16_t mbrType, const char * guidData, const char * name, int toDisplay = 1); // New assignment operators.... - PartType & operator=(const string & orig); + PartType & operator=(const std::string & orig); PartType & operator=(const char * orig); // Assignment operators based on base class.... @@ -58,7 +56,7 @@ public: PartType & operator=(uint16_t ID); // Use MBR type code times 0x0100 to assign GUID // Retrieve transformed GUID data based on type code matches - string TypeName(void) const; + std::string TypeName(void) const; UnicodeString UTypeName(void) const; uint16_t GetHexType() const; diff --git a/support.h b/support.h index a61ddf4..e79b62e 100644 --- a/support.h +++ b/support.h @@ -69,18 +69,16 @@ #define GPT_RESERVED 420 #define NAME_SIZE 36 // GPT allows 36 UTF-16LE code units for a name in a 128 byte partition entry -using namespace std; - -string ReadString(void); -uint64_t GetNumber(uint64_t low, uint64_t high, uint64_t def, const string & prompt); +std::string ReadString(void); +uint64_t GetNumber(uint64_t low, uint64_t high, uint64_t def, const std::string & prompt); char GetYN(void); -uint64_t IeeeToInt(string IeeeValue, uint64_t sSize, uint64_t low, uint64_t high, uint32_t sectorAlignment, uint64_t def = 0); -string BytesToIeee(uint64_t size, uint32_t sectorSize); -unsigned char StrToHex(const string & input, unsigned int position); -int IsHex(string input); // Returns 1 if input can be hexadecimal number.... +uint64_t IeeeToInt(std::string IeeeValue, uint64_t sSize, uint64_t low, uint64_t high, uint32_t sectorAlignment, uint64_t def = 0); +std::string BytesToIeee(uint64_t size, uint32_t sectorSize); +unsigned char StrToHex(const std::string & input, unsigned int position); +int IsHex(std::string input); // Returns 1 if input can be hexadecimal number.... int IsLittleEndian(void); // Returns 1 if CPU is little-endian, 0 if it's big-endian void ReverseBytes(void* theValue, int numBytes); // Reverses byte-order of theValue void WinWarning(void); -string ToLower(const string& input); +std::string ToLower(const std::string& input); #endif -- 2.25.1 |