From 53b1e351cfe0cde6421be29bb9b94795a5d6f06b Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 23 May 2012 21:25:28 +0100 Subject: [PATCH] Changed the return values of some virtual methods in FileBasedDocument - Strings and File objects are now non-const, and the load/save functions return a Result object rather than a string. --- .../Source/Application/jucer_CommandLine.cpp | 9 +++++---- .../Project Saving/jucer_ProjectSaver.h | 20 +++++++++++++------ .../Source/Project/jucer_Project.cpp | 18 ++++++++--------- .../Introjucer/Source/Project/jucer_Project.h | 12 +++++------ .../audio plugin host/Source/FilterGraph.cpp | 16 +++++++-------- extras/audio plugin host/Source/FilterGraph.h | 8 ++++---- .../src/model/jucer_JucerDocument.cpp | 20 +++++++++---------- .../the jucer/src/model/jucer_JucerDocument.h | 8 ++++---- .../documents/juce_FileBasedDocument.cpp | 18 +++++++---------- .../documents/juce_FileBasedDocument.h | 16 ++++++--------- 10 files changed, 73 insertions(+), 72 deletions(-) diff --git a/extras/Introjucer/Source/Application/jucer_CommandLine.cpp b/extras/Introjucer/Source/Application/jucer_CommandLine.cpp index 813f5298da..6e7d1d1081 100644 --- a/extras/Introjucer/Source/Application/jucer_CommandLine.cpp +++ b/extras/Introjucer/Source/Application/jucer_CommandLine.cpp @@ -82,12 +82,12 @@ namespace : "The Introjucer - Re-saving file: ") << projectFile.getFullPathName() << std::endl; - String error (justSaveResources ? proj.saveResourcesOnly (projectFile) + Result error (justSaveResources ? proj.saveResourcesOnly (projectFile) : proj.saveProject (projectFile, true)); - if (error.isNotEmpty()) + if (error.failed()) { - std::cout << "Error when saving: " << error << std::endl; + std::cout << "Error when saving: " << error.getErrorMessage() << std::endl; return 1; } @@ -222,8 +222,9 @@ namespace const File projectFile (getFile (args[1])); Project proj (projectFile); + const Result result (proj.loadDocument (projectFile)); - if (proj.loadDocument (projectFile).isNotEmpty()) + if (result.failed()) { std::cout << "Failed to load project: " << projectFile.getFullPathName() << std::endl; return 1; diff --git a/extras/Introjucer/Source/Project Saving/jucer_ProjectSaver.h b/extras/Introjucer/Source/Project Saving/jucer_ProjectSaver.h index e543063558..6a5afa73a2 100644 --- a/extras/Introjucer/Source/Project Saving/jucer_ProjectSaver.h +++ b/extras/Introjucer/Source/Project Saving/jucer_ProjectSaver.h @@ -50,7 +50,8 @@ public: { public: SaveThread (ProjectSaver& saver_) - : ThreadWithProgressWindow ("Saving...", true, false), saver (saver_) + : ThreadWithProgressWindow ("Saving...", true, false), + saver (saver_), result (Result::ok()) {} void run() @@ -60,12 +61,12 @@ public: } ProjectSaver& saver; - String result; + Result result; JUCE_DECLARE_NON_COPYABLE (SaveThread); }; - String save (bool showProgressBox) + Result save (bool showProgressBox) { if (showProgressBox) { @@ -111,15 +112,22 @@ public: writeReadmeFile(); if (errors.size() > 0) + { project.setFile (oldFile); + return Result::fail (errors[0]); + } - return errors[0]; + return Result::ok(); } - String saveResourcesOnly() + Result saveResourcesOnly() { writeBinaryDataFiles(); - return errors[0]; + + if (errors.size() > 0) + return Result::fail (errors[0]); + + return Result::ok(); } Project::Item saveGeneratedFile (const String& filePath, const MemoryOutputStream& newData) diff --git a/extras/Introjucer/Source/Project/jucer_Project.cpp b/extras/Introjucer/Source/Project/jucer_Project.cpp index 09401a1a2d..ec877a97e7 100644 --- a/extras/Introjucer/Source/Project/jucer_Project.cpp +++ b/extras/Introjucer/Source/Project/jucer_Project.cpp @@ -77,7 +77,7 @@ void Project::setTitle (const String& newTitle) getMainGroup().getNameValue() = newTitle; } -const String Project::getDocumentTitle() +String Project::getDocumentTitle() { return getProjectName().toString(); } @@ -209,17 +209,17 @@ static void registerRecentFile (const File& file) StoredSettings::getInstance()->flush(); } -const String Project::loadDocument (const File& file) +Result Project::loadDocument (const File& file) { ScopedPointer xml (XmlDocument::parse (file)); if (xml == nullptr || ! xml->hasTagName (Tags::projectRoot.toString())) - return "Not a valid Jucer project!"; + return Result::fail ("Not a valid Jucer project!"); ValueTree newTree (ValueTree::fromXml (*xml)); if (! newTree.hasType (Tags::projectRoot)) - return "The document contains errors and couldn't be parsed!"; + return Result::fail ("The document contains errors and couldn't be parsed!"); registerRecentFile (file); projectRoot = newTree; @@ -227,15 +227,15 @@ const String Project::loadDocument (const File& file) removeDefunctExporters(); setMissingDefaultValues(); - return String::empty; + return Result::ok(); } -const String Project::saveDocument (const File& file) +Result Project::saveDocument (const File& file) { return saveProject (file, false); } -String Project::saveProject (const File& file, bool isCommandLineApp) +Result Project::saveProject (const File& file, bool isCommandLineApp) { updateProjectSettings(); sanitiseConfigFlags(); @@ -247,7 +247,7 @@ String Project::saveProject (const File& file, bool isCommandLineApp) return saver.save (! isCommandLineApp); } -String Project::saveResourcesOnly (const File& file) +Result Project::saveResourcesOnly (const File& file) { ProjectSaver saver (*this, file); return saver.saveResourcesOnly(); @@ -256,7 +256,7 @@ String Project::saveResourcesOnly (const File& file) //============================================================================== File Project::lastDocumentOpened; -const File Project::getLastDocumentOpened() +File Project::getLastDocumentOpened() { return lastDocumentOpened; } diff --git a/extras/Introjucer/Source/Project/jucer_Project.h b/extras/Introjucer/Source/Project/jucer_Project.h index 40850cad2a..511fbdf1e7 100644 --- a/extras/Introjucer/Source/Project/jucer_Project.h +++ b/extras/Introjucer/Source/Project/jucer_Project.h @@ -43,12 +43,12 @@ public: //============================================================================== // FileBasedDocument stuff.. - const String getDocumentTitle(); - const String loadDocument (const File& file); - const String saveDocument (const File& file); - String saveProject (const File& file, bool isCommandLineApp); - String saveResourcesOnly (const File& file); - const File getLastDocumentOpened(); + String getDocumentTitle(); + Result loadDocument (const File& file); + Result saveDocument (const File& file); + Result saveProject (const File& file, bool isCommandLineApp); + Result saveResourcesOnly (const File& file); + File getLastDocumentOpened(); void setLastDocumentOpened (const File& file); void setTitle (const String& newTitle); diff --git a/extras/audio plugin host/Source/FilterGraph.cpp b/extras/audio plugin host/Source/FilterGraph.cpp index fbf41ab15c..5668827f16 100644 --- a/extras/audio plugin host/Source/FilterGraph.cpp +++ b/extras/audio plugin host/Source/FilterGraph.cpp @@ -233,7 +233,7 @@ void FilterGraph::clear() } //============================================================================== -const String FilterGraph::getDocumentTitle() +String FilterGraph::getDocumentTitle() { if (! getFile().exists()) return "Unnamed"; @@ -241,29 +241,29 @@ const String FilterGraph::getDocumentTitle() return getFile().getFileNameWithoutExtension(); } -const String FilterGraph::loadDocument (const File& file) +Result FilterGraph::loadDocument (const File& file) { XmlDocument doc (file); ScopedPointer xml (doc.getDocumentElement()); if (xml == nullptr || ! xml->hasTagName ("FILTERGRAPH")) - return "Not a valid filter graph file"; + return Result::fail ("Not a valid filter graph file"); restoreFromXml (*xml); - return String::empty; + return Result::ok(); } -const String FilterGraph::saveDocument (const File& file) +Result FilterGraph::saveDocument (const File& file) { ScopedPointer xml (createXml()); if (! xml->writeToFile (file, String::empty)) - return "Couldn't write to the file"; + return Result::fail ("Couldn't write to the file"); - return String::empty; + return Result::ok(); } -const File FilterGraph::getLastDocumentOpened() +File FilterGraph::getLastDocumentOpened() { RecentlyOpenedFilesList recentFiles; recentFiles.restoreFromString (appProperties->getUserSettings() diff --git a/extras/audio plugin host/Source/FilterGraph.h b/extras/audio plugin host/Source/FilterGraph.h index 007863f6cb..37de2b00ab 100644 --- a/extras/audio plugin host/Source/FilterGraph.h +++ b/extras/audio plugin host/Source/FilterGraph.h @@ -114,10 +114,10 @@ public: void restoreFromXml (const XmlElement& xml); //============================================================================== - const String getDocumentTitle(); - const String loadDocument (const File& file); - const String saveDocument (const File& file); - const File getLastDocumentOpened(); + String getDocumentTitle(); + Result loadDocument (const File& file); + Result saveDocument (const File& file); + File getLastDocumentOpened(); void setLastDocumentOpened (const File& file); /** The special channel index used to refer to a filter's midi channel. diff --git a/extras/the jucer/src/model/jucer_JucerDocument.cpp b/extras/the jucer/src/model/jucer_JucerDocument.cpp index bc720331b7..4b0003e011 100644 --- a/extras/the jucer/src/model/jucer_JucerDocument.cpp +++ b/extras/the jucer/src/model/jucer_JucerDocument.cpp @@ -252,7 +252,7 @@ void JucerDocument::setComponentOverlayOpacity (const float alpha) } //============================================================================== -const String JucerDocument::getDocumentTitle() +String JucerDocument::getDocumentTitle() { return className; } @@ -406,7 +406,7 @@ bool JucerDocument::loadFromXml (const XmlElement& xml) } //============================================================================== -const String JucerDocument::loadDocument (const File& file) +Result JucerDocument::loadDocument (const File& file) { const File cppFile (file.withFileExtension (".cpp")); @@ -419,15 +419,15 @@ const String JucerDocument::loadDocument (const File& file) if (xml != nullptr) { if (loadFromXml (*xml)) - return String::empty; + return Result::ok(); - return TRANS("Couldn't parse the XML section of this file correctly"); + return Result::fail (TRANS("Couldn't parse the XML section of this file correctly")); } - return TRANS("Not a valid Jucer cpp file"); + return Result::fail (TRANS("Not a valid Jucer cpp file")); } -const String JucerDocument::saveDocument (const File& file) +Result JucerDocument::saveDocument (const File& file) { const File cppFile (file.withFileExtension (".cpp")); const File hFile (file.withFileExtension (".h")); @@ -435,19 +435,19 @@ const String JucerDocument::saveDocument (const File& file) String templateH, templateCpp; if (! findTemplateFiles (templateH, templateCpp)) - return TRANS("Couldn't find the required Jucer template files...\n\nMake sure the template files directory is set up correctly in the preferences box."); + return Result::fail (TRANS("Couldn't find the required Jucer template files...\n\nMake sure the template files directory is set up correctly in the preferences box.")); const bool ok = writeCodeFiles (hFile, cppFile, templateH, templateCpp); TestComponent::reloadAll(); if (ok) - return String::empty; + return Result::ok(); else - return TRANS("Couldn't write to the file."); + return Result::fail (TRANS("Couldn't write to the file.")); } //============================================================================== -const File JucerDocument::getLastDocumentOpened() +File JucerDocument::getLastDocumentOpened() { return StoredSettings::getInstance()->recentFiles.getFile (0); } diff --git a/extras/the jucer/src/model/jucer_JucerDocument.h b/extras/the jucer/src/model/jucer_JucerDocument.h index eab821ec8f..466e04b996 100644 --- a/extras/the jucer/src/model/jucer_JucerDocument.h +++ b/extras/the jucer/src/model/jucer_JucerDocument.h @@ -157,10 +157,10 @@ public: protected: - const String getDocumentTitle(); - const String loadDocument (const File& file); - const String saveDocument (const File& file); - const File getLastDocumentOpened(); + String getDocumentTitle(); + Result loadDocument (const File& file); + Result saveDocument (const File& file); + File getLastDocumentOpened(); void setLastDocumentOpened (const File& file); diff --git a/modules/juce_gui_extra/documents/juce_FileBasedDocument.cpp b/modules/juce_gui_extra/documents/juce_FileBasedDocument.cpp index 6cbe18757f..46f547aeec 100644 --- a/modules/juce_gui_extra/documents/juce_FileBasedDocument.cpp +++ b/modules/juce_gui_extra/documents/juce_FileBasedDocument.cpp @@ -75,13 +75,13 @@ bool FileBasedDocument::loadFrom (const File& newFile, const File oldFile (documentFile); documentFile = newFile; - String error; + Result result (Result::fail ("The file doesn't exist")); if (newFile.existsAsFile()) { - error = loadDocument (newFile); + result = loadDocument (newFile); - if (error.isEmpty()) + if (result.wasOk()) { setChangedFlag (false); MouseCursor::hideWaitCursor(); @@ -90,10 +90,6 @@ bool FileBasedDocument::loadFrom (const File& newFile, return true; } } - else - { - error = "The file doesn't exist"; - } documentFile = oldFile; MouseCursor::hideWaitCursor(); @@ -105,7 +101,7 @@ bool FileBasedDocument::loadFrom (const File& newFile, TRANS("There was an error while trying to load the file:\n\n") + newFile.getFullPathName() + "\n\n" - + error); + + result.getErrorMessage()); } return false; @@ -171,9 +167,9 @@ FileBasedDocument::SaveResult FileBasedDocument::saveAs (const File& newFile, const File oldFile (documentFile); documentFile = newFile; - String error (saveDocument (newFile)); + const Result result (saveDocument (newFile)); - if (error.isEmpty()) + if (result.wasOk()) { setChangedFlag (false); MouseCursor::hideWaitCursor(); @@ -193,7 +189,7 @@ FileBasedDocument::SaveResult FileBasedDocument::saveAs (const File& newFile, + TRANS("\" to the file:\n\n") + newFile.getFullPathName() + "\n\n" - + error); + + result.getErrorMessage()); } return failedToWriteToFile; diff --git a/modules/juce_gui_extra/documents/juce_FileBasedDocument.h b/modules/juce_gui_extra/documents/juce_FileBasedDocument.h index 1aa92b4582..c841931026 100644 --- a/modules/juce_gui_extra/documents/juce_FileBasedDocument.h +++ b/modules/juce_gui_extra/documents/juce_FileBasedDocument.h @@ -226,21 +226,17 @@ protected: This is used in message boxes, filenames and file choosers, so it should be something sensible. */ - virtual const String getDocumentTitle() = 0; + virtual String getDocumentTitle() = 0; /** This method should try to load your document from the given file. - - If it fails, it should return an error message. If it succeeds, it should return - an empty string. + @returns a Result object to indicate the whether there was an error. */ - virtual const String loadDocument (const File& file) = 0; + virtual Result loadDocument (const File& file) = 0; /** This method should try to write your document to the given file. - - If it fails, it should return an error message. If it succeeds, it should return - an empty string. + @returns a Result object to indicate the whether there was an error. */ - virtual const String saveDocument (const File& file) = 0; + virtual Result saveDocument (const File& file) = 0; /** This is used for dialog boxes to make them open at the last folder you were using. @@ -258,7 +254,7 @@ protected: @see RecentlyOpenedFilesList */ - virtual const File getLastDocumentOpened() = 0; + virtual File getLastDocumentOpened() = 0; /** This is used for dialog boxes to make them open at the last folder you were using.