From c7f8ba6ca865afc7048b03ef77d5b91bdc0a3879 Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro Date: Sat, 22 Nov 2008 19:52:38 +0000 Subject: [PATCH] Some interface clean up. Originally committed to SVN as r2477. --- athenasub/athenasub_2008.vcproj | 4 ++ athenasub/include/athenasub/interfaces.h | 26 +-------- athenasub/src/action.cpp | 10 ++++ athenasub/src/action.h | 6 +-- athenasub/src/actionlist.cpp | 8 +-- athenasub/src/actionlist.h | 2 +- athenasub/src/controller.cpp | 2 +- athenasub/src/controller.h | 2 +- athenasub/src/format_handler.cpp | 68 ++++++++++++++++++++++++ athenasub/src/format_handler.h | 12 ++--- athenasub/src/text_reader_cache.cpp | 2 +- 11 files changed, 101 insertions(+), 41 deletions(-) create mode 100644 athenasub/src/format_handler.cpp diff --git a/athenasub/athenasub_2008.vcproj b/athenasub/athenasub_2008.vcproj index d4f569ed2..ecae2d67d 100644 --- a/athenasub/athenasub_2008.vcproj +++ b/athenasub/athenasub_2008.vcproj @@ -319,6 +319,10 @@ + + diff --git a/athenasub/include/athenasub/interfaces.h b/athenasub/include/athenasub/interfaces.h index 7650aa55d..786cf0455 100644 --- a/athenasub/include/athenasub/interfaces.h +++ b/athenasub/include/athenasub/interfaces.h @@ -66,6 +66,7 @@ namespace Athenasub { class IDeltaCoder; class CController; class CAction; + class IAction; // Smart pointers @@ -83,6 +84,7 @@ namespace Athenasub { typedef shared_ptr Notification; typedef shared_ptr Section; typedef shared_ptr DeltaCoder; + typedef shared_ptr Action; // Const smart pointers @@ -100,27 +102,6 @@ namespace Athenasub { // Model class IModel { - friend class CFormatHandler; - friend class CActionList; - friend class CController; - friend class CAction; - - protected: - virtual void ProcessActionList(CActionList &actionList,int type=0) = 0; - - virtual void Undo(const String owner="") = 0; - virtual void Redo(const String owner="") = 0; - virtual void ActivateStack(ActionStack stack,bool isUndo,const String &owner) = 0; - - virtual void DispatchNotifications(Notification notification) const = 0; - - virtual void Clear() = 0; - virtual void Load(Reader &input,Format format=Format()) = 0; - - virtual Section AddSection(String name) = 0; - virtual Section GetMutableSection(String name) = 0; - virtual Section GetMutableSectionByIndex(size_t index) = 0; - public: virtual ~IModel() {} @@ -335,8 +316,6 @@ namespace Athenasub { // Action interface - class IAction; - typedef shared_ptr Action; class IAction { public: virtual ~IAction() {} @@ -415,7 +394,6 @@ namespace Athenasub { class ILibAthenaSub { public: virtual ~ILibAthenaSub() {} - virtual Model CreateModel()=0; }; typedef shared_ptr LibAthenaSub; diff --git a/athenasub/src/action.cpp b/athenasub/src/action.cpp index c383ecb0e..01b9e6ad7 100644 --- a/athenasub/src/action.cpp +++ b/athenasub/src/action.cpp @@ -48,6 +48,16 @@ CAction::CAction(Model _model) if (!model) THROW_ATHENA_EXCEPTION(Exception::Internal_Error); } +Model CAction::GetModel() const +{ + return model; +} + +Section CAction::GetSection(String name) const +{ + return static_pointer_cast(model)->GetMutableSection(name); +} + ///////////////////////////// Insert line ///////////////////////////// diff --git a/athenasub/src/action.h b/athenasub/src/action.h index 548653574..2251b1bdb 100644 --- a/athenasub/src/action.h +++ b/athenasub/src/action.h @@ -43,13 +43,13 @@ namespace Athenasub { // Action base class class CAction : public IAction { private: - mutable Model model; + Model model; protected: CAction(Model model); - Model GetModel() const { return model; } - Section GetSection(String name) const { return model->GetMutableSection(name); } + Model GetModel() const; + Section GetSection(String name) const; }; // Insert line diff --git a/athenasub/src/actionlist.cpp b/athenasub/src/actionlist.cpp index c199a974e..1289a7fc0 100644 --- a/athenasub/src/actionlist.cpp +++ b/athenasub/src/actionlist.cpp @@ -41,7 +41,7 @@ using namespace Athenasub; /////////////// // Constructor CActionList::CActionList(weak_ptr _model,String _actionName,const String _owner,bool _undoAble) -: model(_model), owner(_owner), undoAble(_undoAble) +: model(dynamic_pointer_cast(Model(_model))), owner(_owner), undoAble(_undoAble) { valid = false; Start(_actionName); @@ -96,7 +96,7 @@ void CActionList::Start(const String name) void CActionList::Finish() { if (valid) { - Model(model)->ProcessActionList(*this); + shared_ptr(model)->ProcessActionList(*this); actions.clear(); valid = false; } @@ -125,7 +125,7 @@ void CActionList::RemoveLine(int position,const String section) // Insert a "modify line" action Entry CActionList::ModifyLine(int position,const String section) { - Section sect = Model(model)->GetMutableSection(section); + Section sect = shared_ptr(model)->GetMutableSection(section); Entry entry = sect->GetEntry(position)->Clone(); Action action = Action (new ActionModify(model.lock(),entry,position,section,false)); AddAction(action); @@ -138,7 +138,7 @@ Entry CActionList::ModifyLine(int position,const String section) std::vector CActionList::ModifyLines(Selection selection,const String section) { // Get section - Section sect = Model(model)->GetMutableSection(section); + Section sect = shared_ptr(model)->GetMutableSection(section); // Generate entries std::vector entries(selection->GetCount()); diff --git a/athenasub/src/actionlist.h b/athenasub/src/actionlist.h index f19e5dfcb..faff26ed3 100644 --- a/athenasub/src/actionlist.h +++ b/athenasub/src/actionlist.h @@ -56,7 +56,7 @@ namespace Athenasub { private: String actionName; String owner; - weak_ptr model; + weak_ptr model; std::list actions; bool valid; bool undoAble; diff --git a/athenasub/src/controller.cpp b/athenasub/src/controller.cpp index 82c4926da..b10e8080f 100644 --- a/athenasub/src/controller.cpp +++ b/athenasub/src/controller.cpp @@ -46,7 +46,7 @@ using namespace Athenasub; /////////////// // Constructor CController::CController(Model _model) -: model(_model) +: model(dynamic_pointer_cast(_model)) { } diff --git a/athenasub/src/controller.h b/athenasub/src/controller.h index 8af2d3614..11847bf4e 100644 --- a/athenasub/src/controller.h +++ b/athenasub/src/controller.h @@ -43,7 +43,7 @@ namespace Athenasub { friend class CModel; private: - Model model; + shared_ptr model; CController (Model model); public: diff --git a/athenasub/src/format_handler.cpp b/athenasub/src/format_handler.cpp new file mode 100644 index 000000000..2a894a839 --- /dev/null +++ b/athenasub/src/format_handler.cpp @@ -0,0 +1,68 @@ +// Copyright (c) 2008, Rodrigo Braz Monteiro +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of the Aegisub Group nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// ----------------------------------------------------------------------------- +// +// AEGISUB/ATHENASUB +// +// Website: http://www.aegisub.net +// Contact: mailto:amz@aegisub.net +// + +#include "format_handler.h" +#include "model.h" +using namespace Athenasub; + + +Section CFormatHandler::AddSection(IModel &model,String name) const +{ + return (dynamic_cast(&model))->AddSection(name); +} + +ConstSection CFormatHandler::GetSection(const IModel &model,String name) const +{ + return model.GetSection(name); +} + +ConstSection CFormatHandler::GetSectionByIndex(const IModel &model,size_t index) const +{ + return model.GetSectionByIndex(index); +} + +Section CFormatHandler::GetSection(IModel &model,String name) const +{ + return (dynamic_cast(&model))->GetMutableSection(name); +} + +Section CFormatHandler::GetSectionByIndex(IModel &model,size_t index) const +{ + return (dynamic_cast(&model))->GetMutableSectionByIndex(index); +} + +size_t CFormatHandler::GetSectionCount(const IModel &model) const { + return model.GetSectionCount(); +} diff --git a/athenasub/src/format_handler.h b/athenasub/src/format_handler.h index 526c57f40..065562c3e 100644 --- a/athenasub/src/format_handler.h +++ b/athenasub/src/format_handler.h @@ -45,12 +45,12 @@ namespace Athenasub { protected: virtual ~CFormatHandler() {} - Section AddSection(IModel &model,String name) const { return model.AddSection(name); } - ConstSection GetSection(const IModel &model,String name) const { return model.GetSection(name); } - ConstSection GetSectionByIndex(const IModel &model,size_t index) const { return model.GetSectionByIndex(index); } - Section GetSection(IModel &model,String name) const { return model.GetMutableSection(name); } - Section GetSectionByIndex(IModel &model,size_t index) const { return model.GetMutableSectionByIndex(index); } - size_t GetSectionCount(const IModel &model) const { return model.GetSectionCount(); } + Section AddSection(IModel &model,String name) const; + ConstSection GetSection(const IModel &model,String name) const; + ConstSection GetSectionByIndex(const IModel &model,size_t index) const; + Section GetSection(IModel &model,String name) const; + Section GetSectionByIndex(IModel &model,size_t index) const; + size_t GetSectionCount(const IModel &model) const; public: //CFormatHandler(IModel& _model) : model(_model) {} diff --git a/athenasub/src/text_reader_cache.cpp b/athenasub/src/text_reader_cache.cpp index 81189f084..99dc79ef5 100644 --- a/athenasub/src/text_reader_cache.cpp +++ b/athenasub/src/text_reader_cache.cpp @@ -48,7 +48,7 @@ Athenasub::TextReaderCache::TextReaderCache(shared_ptr src) String TextReaderCache::ReadLineFromFile() { if (bufferPos == buffer.size()) { - LoadMore(1000000); + LoadMore(1); } if (bufferPos == buffer.size()) { return "";