Skip to content

Commit 954f59d

Browse files
committed
Bridge: Fixed File "duplicate definitions" when using SD and Bridge libraries together
The two File classes have been enclosed into different namespaces. To guarantee compatibility with old sketches that uses only one of the two libraries an additional line: using namespace xxxxx; has been added so the users can still use "File" where there is no ambiguity. BridgeLib::File and SDLib::File classes have been also aliased to BridgeFile and SDFile respectively, users are encouraged to use that instead of File.
1 parent 36c2216 commit 954f59d

File tree

7 files changed

+43
-2
lines changed

7 files changed

+43
-2
lines changed

libraries/Bridge/keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ connected KEYWORD2
4242

4343
# FileIO Class
4444
File KEYWORD2
45+
BridgeFile KEYWORD2
4546
seek KEYWORD2
4647
position KEYWORD2
4748
size KEYWORD2

libraries/Bridge/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Bridge
2-
version=1.0.7
2+
version=1.1.0
33
author=Arduino
44
maintainer=Arduino <[email protected]>
55
sentence=Enables the communication between the Linux processor and the AVR. For Arduino Yún and TRE only.

libraries/Bridge/src/FileIO.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#include <FileIO.h>
2020

21-
21+
namespace BridgeLib {
2222

2323
File::File(BridgeClass &b) : mode(255), bridge(b) {
2424
// Empty
@@ -279,3 +279,5 @@ boolean FileSystemClass::rmdir(const char *filepath) {
279279
}
280280

281281
FileSystemClass FileSystem;
282+
283+
}

libraries/Bridge/src/FileIO.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#define FILE_WRITE 1
2626
#define FILE_APPEND 2
2727

28+
namespace BridgeLib {
29+
2830
class File : public Stream {
2931

3032
public:
@@ -100,4 +102,19 @@ class FileSystemClass {
100102

101103
extern FileSystemClass FileSystem;
102104

105+
};
106+
107+
// We enclose File and FileSystem classes in namespace BridgeLib to avoid
108+
// conflicts with legacy SD library.
109+
110+
// This ensure compatibility with older sketches that uses only Bridge lib
111+
// (the user can still use File instead of BridgeFile)
112+
using namespace BridgeLib;
113+
114+
// This allows sketches to use BridgeLib::File together with SD library
115+
// (you must use BridgeFile instead of File when needed to disambiguate)
116+
typedef BridgeLib::File BridgeFile;
117+
typedef BridgeLib::FileSystemClass BridgeFileSystemClass;
118+
#define BridgeFileSystem BridgeLib::FileSystem
119+
103120
#endif

libraries/SD/keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
SD KEYWORD1 SD
1010
File KEYWORD1 SD
11+
SDFile KEYWORD1 SD
1112

1213
#######################################
1314
# Methods and Functions (KEYWORD2)

libraries/SD/src/SD.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252

5353
#include "SD.h"
5454

55+
namespace SDLib {
56+
5557
// Used by `getNextPathComponent`
5658
#define MAX_COMPONENT_LEN 12 // What is max length?
5759
#define PATH_COMPONENT_BUFFER_LEN MAX_COMPONENT_LEN+1
@@ -614,3 +616,5 @@ void File::rewindDirectory(void) {
614616
}
615617

616618
SDClass SD;
619+
620+
};

libraries/SD/src/SD.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#define FILE_READ O_READ
2424
#define FILE_WRITE (O_READ | O_WRITE | O_CREAT)
2525

26+
namespace SDLib {
27+
2628
class File : public Stream {
2729
private:
2830
char _name[13]; // our name
@@ -104,4 +106,18 @@ class SDClass {
104106

105107
extern SDClass SD;
106108

109+
};
110+
111+
// We enclose File and SD classes in namespace SDLib to avoid conflicts
112+
// with others legacy libraries that redefines File class.
113+
114+
// This ensure compatibility with sketches that uses only SD library
115+
using namespace SDLib;
116+
117+
// This allows sketches to use SDLib::File with other libraries (in the
118+
// sketch you must use SDFile instead of File to disambiguate)
119+
typedef SDLib::File SDFile;
120+
typedef SDLib::SDClass SDFileSystemClass;
121+
#define SDFileSystem SDLib::SD
122+
107123
#endif

0 commit comments

Comments
 (0)