Modified the subs lib to use TR1's shared_ptr.

Originally committed to SVN as r2034.
This commit is contained in:
Rodrigo Braz Monteiro 2008-03-13 03:42:27 +00:00
parent 3ab038d373
commit a3755cc6e4
23 changed files with 176 additions and 96 deletions

View file

@ -227,6 +227,10 @@
RelativePath=".\include\aegilib\tokenizer.h"
>
</File>
<File
RelativePath=".\include\aegilib\tr1.h"
>
</File>
<File
RelativePath=".\include\aegilib\utils.h"
>

View file

@ -47,15 +47,15 @@ namespace Aegilib {
class Action {
private:
ActionType type;
void* data;
shared_ptr<void> data;
int par1;
public:
Action();
Action(ActionType type,void* data,int par1);
Action(ActionType type,shared_ptr<void> data,int par1);
ActionType GetType() { return type; }
void* GetData() { return data; }
shared_ptr<void> GetData() { return data; }
int GetLineNumber() { return par1; }
};
};

View file

@ -34,6 +34,7 @@
//
#pragma once
#include "tr1.h"
#include "exception.h"
#include "model.h"
#include "view.h"

View file

@ -35,6 +35,7 @@
#pragma once
#include "aegistring.h"
#include "tr1.h"
namespace Aegilib {
// Prototypes
@ -49,7 +50,7 @@ namespace Aegilib {
virtual String GetName() const = 0;
virtual StringArray GetReadExtensions() const = 0;
virtual StringArray GetWriteExtensions() const = 0;
virtual FormatHandler* GetHandler(Model &model) const = 0;
virtual shared_ptr<FormatHandler> GetHandler(Model &model) const = 0;
virtual bool CanStoreText() const { return false; }
virtual bool CanStoreImages() const { return false; }
@ -65,5 +66,6 @@ namespace Aegilib {
virtual int GetTimingPrecision() const { return 10; } // In milliseconds
virtual int GetMaxTime() const { return 36000000-10; } // In milliseconds, default 9h 59min 59.99s
};
typedef shared_ptr<Format> FormatPtr;
};

View file

@ -35,15 +35,18 @@
#pragma once
#include "aegistring.h"
#include "tr1.h"
namespace Aegilib {
// Format handler interface
class FormatHandler {
public:
protected:
virtual ~FormatHandler() {}
public:
virtual void Load(wxInputStream &file,const String encoding) = 0;
};
typedef shared_ptr<FormatHandler> FormatHandlerPtr;
};

View file

@ -41,18 +41,18 @@ namespace Aegilib {
// Format manager class
class FormatManager {
private:
static std::vector<const Format*> formats;
static std::vector<const FormatPtr> formats;
FormatManager() {}
public:
static void AddFormat(const Format *format);
static void AddFormat(const FormatPtr format);
static void InitializeFormats();
static void ClearFormats();
static int GetFormatCount();
static const Format* GetFormatByIndex(const int index);
static const Format* GetFormatFromFilename(const String &filename,bool read);
static const Format* GetFormatFromName(const String &name);
static const FormatPtr GetFormatByIndex(const int index);
static const FormatPtr GetFormatFromFilename(const String &filename,bool read);
static const FormatPtr GetFormatFromName(const String &name);
};
};

View file

@ -43,6 +43,7 @@ namespace Aegilib {
// Prototypes
class View;
typedef shared_ptr<View> ViewPtr;
class Notification;
class Format;
@ -50,11 +51,12 @@ namespace Aegilib {
// Stores the subtitle data
class Model {
friend class Manipulator;
typedef std::list<View*> ViewList;
typedef std::list<ViewPtr> ViewList;
typedef std::list<const Manipulator> ActionStack;
typedef shared_ptr<Format> FormatPtr;
private:
std::list<Section*> sections;
std::list<SectionPtr> sections;
ActionStack undoStack;
ActionStack redoStack;
ViewList listeners;
@ -66,14 +68,14 @@ namespace Aegilib {
public:
const Format& GetFormat() const;
void AddListener(View *listener);
void AddListener(ViewPtr listener);
void Load(wxInputStream &input,const Format *format=NULL,const String encoding=L"");
void Save(wxOutputStream &output,const Format *format=NULL,const String encoding=L"UTF-8");
void Load(wxInputStream &input,const FormatPtr format=FormatPtr(),const String encoding=L"");
void Save(wxOutputStream &output,const FormatPtr format=FormatPtr(),const String encoding=L"UTF-8");
void LoadFile(const String filename,const String encoding=L"");
void SaveFile(const String filename,const String encoding=L"UTF-8");
Section* GetSection(String name) const;
SectionPtr GetSection(String name) const;
void AddSection(String name);
bool CanUndo(const String owner=L"") const;

View file

@ -36,6 +36,7 @@
#pragma once
#include "aegistring.h"
#include "section_entry.h"
#include "tr1.h"
#include <list>
#include <map>
@ -43,14 +44,15 @@ namespace Aegilib {
// Section class
class Section {
friend class shared_ptr<Section>;
private:
std::list<SectionEntry*> entries;
std::list<SectionEntryPtr> entries;
std::map<String,String> properties;
String name;
public:
Section(String name);
~Section();
~Section() {}
const String& GetName() const { return name; }
String SetName(const String& newName) { name = newName; }
@ -62,7 +64,8 @@ namespace Aegilib {
size_t PropertyCount() const;
String GetPropertyName(size_t index) const;
void AddEntry(SectionEntry *entry);
void AddEntry(SectionEntryPtr entry);
};
typedef shared_ptr<Section> SectionPtr;
};

View file

@ -35,6 +35,7 @@
#pragma once
#include "aegistring.h"
#include "tr1.h"
namespace Aegilib {
@ -49,25 +50,31 @@ namespace Aegilib {
// Prototypes
class SectionEntryPlain;
typedef shared_ptr<SectionEntryPlain> SectionEntryPlainPtr;
class SectionEntryDialogue;
typedef shared_ptr<SectionEntryDialogue> SectionEntryDialoguePtr;
class SectionEntryStyle;
typedef shared_ptr<SectionEntryStyle> SectionEntryStylePtr;
class SectionEntryFile;
typedef shared_ptr<SectionEntryFile> SectionEntryFilePtr;
class SectionEntryRaw;
typedef shared_ptr<SectionEntryRaw> SectionEntryRawPtr;
class SectionEntry;
typedef shared_ptr<SectionEntry> SectionEntryPtr;
// Section entry class
class SectionEntry {
protected:
virtual ~SectionEntry() {}
const String& EmptyString() const;
public:
virtual ~SectionEntry() {}
virtual SectionEntryType GetType() const =0;
virtual SectionEntryPlain *GetAsPlain() { return NULL; }
virtual SectionEntryDialogue *GetAsDialogue() { return NULL; }
virtual SectionEntryStyle *GetAsStyle() { return NULL; }
virtual SectionEntryFile *GetAsFile() { return NULL; }
virtual SectionEntryRaw *GetAsRaw() { return NULL; }
static const SectionEntryPlainPtr GetAsPlain(const SectionEntryPtr &ptr);
static const SectionEntryDialoguePtr GetAsDialogue(const SectionEntryPtr &ptr);
static const SectionEntryStylePtr GetAsStyle(const SectionEntryPtr &ptr);
static const SectionEntryFilePtr GetAsFile(const SectionEntryPtr &ptr);
static const SectionEntryRawPtr GetAsRaw(const SectionEntryPtr &ptr);
};
};

View file

@ -35,6 +35,7 @@
#pragma once
#include "aegistring.h"
#include "tr1.h"
// Prototypes
class wxStringTokenizer;
@ -44,7 +45,7 @@ namespace Aegilib {
// Tokenizer class
class Tokenizer {
private:
wxStringTokenizer *tkn;
shared_ptr<wxStringTokenizer> tkn;
public:
Tokenizer(String string,String token);

View file

@ -0,0 +1,57 @@
// 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/AEGILIB
//
// Website: http://www.aegisub.net
// Contact: mailto:amz@aegisub.net
//
#pragma once
//////////////////////////////////////////
// Include the Technical Report 1 headers
// This is necessary because some compilers put them on different places
#include <tr1/memory>
#include <tr1/array>
namespace Aegilib {
using std::tr1::shared_ptr;
using std::tr1::weak_ptr;
using std::tr1::array;
using std::tr1::dynamic_pointer_cast;
using std::tr1::static_pointer_cast;
// Null deleter for use with shared_ptr
class NullDeleter {
public:
void operator()(void const *) const { }
};
};

View file

@ -46,7 +46,7 @@ Action::Action()
//////////////////////////////
// Initialization constructor
Action::Action(ActionType _type,void* _data,int _par1)
Action::Action(ActionType _type,shared_ptr<void> _data,int _par1)
{
type = _type;
data = _data;

View file

@ -41,12 +41,12 @@ using namespace Aegilib;
////////
// List
std::vector<const Format*> FormatManager::formats;
std::vector<const FormatPtr> FormatManager::formats;
////////////////
// Add a format
void FormatManager::AddFormat(const Format *format)
void FormatManager::AddFormat(const FormatPtr format)
{
formats.push_back(format);
}
@ -56,7 +56,7 @@ void FormatManager::AddFormat(const Format *format)
// Initialzie all built-in formats
void FormatManager::InitializeFormats()
{
formats.push_back(new FormatASS);
formats.push_back(FormatPtr(new FormatASS));
}
@ -78,20 +78,20 @@ int FormatManager::GetFormatCount()
////////////
// By index
const Format* FormatManager::GetFormatByIndex(const int index)
const FormatPtr FormatManager::GetFormatByIndex(const int index)
{
try {
return formats.at(index);
}
catch (...) {
return NULL;
return FormatPtr();
}
}
///////////////
// By filename
const Format* FormatManager::GetFormatFromFilename(const String &filename,bool read)
const FormatPtr FormatManager::GetFormatFromFilename(const String &filename,bool read)
{
size_t len = formats.size();
for (size_t i=0;i<len;i++) {
@ -103,18 +103,18 @@ const Format* FormatManager::GetFormatFromFilename(const String &filename,bool r
if (filename.EndsWith(exts[j])) return formats[i];
}
}
return NULL;
return FormatPtr();
}
//////////////////
// By format name
const Format* FormatManager::GetFormatFromName(const String &name)
const FormatPtr FormatManager::GetFormatFromName(const String &name)
{
size_t len = formats.size();
for (size_t i=0;i<len;i++) {
if (name == formats[i]->GetName()) return formats[i];
}
return NULL;
return FormatPtr();
}

View file

@ -86,7 +86,7 @@ void FormatHandlerASS::Load(wxInputStream &file,const String encoding)
int version = 1;
wxString curGroup = L"-";
wxString prevGroup = L"-";
Section *section = NULL;
SectionPtr section = SectionPtr();
// Read file
while (reader.HasMoreLines()) {
@ -108,7 +108,7 @@ void FormatHandlerASS::Load(wxInputStream &file,const String encoding)
if (cur[0] == L'[') continue;
// Create and insert line
SectionEntry *entry = MakeEntry(cur,section,version);
SectionEntryPtr entry = MakeEntry(cur,section,version);
if (entry) section->AddEntry(entry);
}
@ -127,11 +127,11 @@ void FormatHandlerASS::Load(wxInputStream &file,const String encoding)
///////////////
// Create line
SectionEntry *FormatHandlerASS::MakeEntry(const String &data,Section *section,int version)
SectionEntryPtr FormatHandlerASS::MakeEntry(const String &data,SectionPtr section,int version)
{
// Variables
const String group = section->GetName();
SectionEntry *final = NULL;
SectionEntryPtr final;
// Attachments
if (group == _T("Fonts") || group == _T("Graphics")) {
@ -142,12 +142,8 @@ SectionEntry *FormatHandlerASS::MakeEntry(const String &data,Section *section,in
else if (group == _T("Events")) {
// Dialogue lines
if ((data.Left(9) == _T("Dialogue:") || data.Left(8) == _T("Comment:"))) {
DialogueASS *diag = new DialogueASS(data,version);
shared_ptr<DialogueASS> diag (new DialogueASS(data,version));
final = diag;
// Debug
wxString out = diag->GetStartTime().GetString(2,1) + _T(",") + diag->GetEndTime().GetString(2,1) + _T(",") + diag->GetText();
std::cout << out.mb_str(wxConvUTF8) << std::endl;
}
// Format lines
@ -155,7 +151,7 @@ SectionEntry *FormatHandlerASS::MakeEntry(const String &data,Section *section,in
section->SetProperty(_T("Format"),data.Mid(7).Trim(true).Trim(false));
}
// Garbage
// Garbage/hard comments
else {
// TODO
}
@ -164,12 +160,8 @@ SectionEntry *FormatHandlerASS::MakeEntry(const String &data,Section *section,in
// Styles
else if (group == _T("V4+ Styles")) {
if (data.Left(6) == _T("Style:")) {
StyleASS *style = new StyleASS(data,version);
shared_ptr<StyleASS> style (new StyleASS(data,version));
final = style;
// Debug
wxString out = style->GetName() + _T(": ") + style->GetFontName() + _T(", ") + wxString::Format(_T("(%i,%i,%i)"),style->GetColour(0).GetRed(),style->GetColour(0).GetGreen(),style->GetColour(0).GetBlue());
std::cout << out.mb_str(wxConvUTF8) << std::endl;
}
if (data.Left(7) == _T("Format:")) {
section->SetProperty(_T("Format"),data.Mid(7).Trim(true).Trim(false));
@ -179,17 +171,17 @@ SectionEntry *FormatHandlerASS::MakeEntry(const String &data,Section *section,in
// Script info
else if (group == _T("Script Info")) {
// Discard comments
if (data.Left(1) == _T(";")) return NULL;
if (data.Left(1) == _T(";")) return SectionEntryPtr();
// Parse property
size_t pos = data.Find(_T(':'));
if (pos == wxNOT_FOUND) return NULL;
if (pos == wxNOT_FOUND) return SectionEntryPtr();
wxString key = data.Left(pos).Trim(true).Trim(false);
wxString value = data.Mid(pos+1).Trim(true).Trim(false);
// Insert property
section->SetProperty(key,value);
return NULL;
return SectionEntryPtr();
}
// Return entry

View file

@ -48,7 +48,7 @@ namespace Aegilib {
// Advanced Substation Alpha format handler
class FormatHandlerASS : public FormatHandler {
private:
SectionEntry *MakeEntry(const String &data,Section *section,int version);
SectionEntryPtr MakeEntry(const String &data,SectionPtr section,int version);
void ProcessGroup(String cur,String &curGroup,int &version);
Model &model;
@ -65,7 +65,7 @@ namespace Aegilib {
String GetName() const { return L"Advanced Substation Alpha"; }
StringArray GetReadExtensions() const;
StringArray GetWriteExtensions() const;
FormatHandler* GetHandler(Model &model) const { return new FormatHandlerASS(model); }
FormatHandlerPtr GetHandler(Model &model) const { return FormatHandlerPtr(new FormatHandlerASS(model)); }
bool CanStoreText() const { return true; }
bool CanUseTime() const { return true; }

View file

@ -39,7 +39,7 @@ using namespace Aegilib;
/////////////////////////////////////////////////////////
// Adds a listener to be notified whenever things change
void Model::AddListener(View *listener)
void Model::AddListener(ViewPtr listener)
{
wxASSERT(listener);
listeners.push_back(listener);
@ -86,7 +86,7 @@ Manipulator Model::CreateAntiManipulator(const Manipulator &src)
//////////////////
// Load subtitles
void Model::Load(wxInputStream &input,const Format *format,const String encoding)
void Model::Load(wxInputStream &input,const FormatPtr format,const String encoding)
{
// Autodetect format
if (format == NULL) {
@ -97,20 +97,17 @@ void Model::Load(wxInputStream &input,const Format *format,const String encoding
}
// Get handler
FormatHandler *handler = format->GetHandler(*this);
FormatHandlerPtr handler = format->GetHandler(*this);
if (!handler) throw Exception(Exception::No_Format_Handler);
// Load
handler->Load(input,encoding);
// Clean up
delete handler;
}
//////////////////
// Save subtitles
void Model::Save(wxOutputStream &output,const Format *format,const String encoding)
void Model::Save(wxOutputStream &output,const FormatPtr format,const String encoding)
{
(void) output;
(void) format;
@ -123,7 +120,7 @@ void Model::Save(wxOutputStream &output,const Format *format,const String encodi
// Load a file
void Model::LoadFile(const String filename,const String encoding)
{
const Format *handler = FormatManager::GetFormatFromFilename(filename,true);
const FormatPtr handler = FormatManager::GetFormatFromFilename(filename,true);
wxFileInputStream stream(filename);
Load(stream,handler,encoding);
}
@ -133,7 +130,7 @@ void Model::LoadFile(const String filename,const String encoding)
// Save a file
void Model::SaveFile(const String filename,const String encoding)
{
const Format *handler = FormatManager::GetFormatFromFilename(filename,true);
const FormatPtr handler = FormatManager::GetFormatFromFilename(filename,true);
wxFileOutputStream stream(filename);
Save(stream,handler,encoding);
}
@ -141,13 +138,13 @@ void Model::SaveFile(const String filename,const String encoding)
//////////////////
// Gets a section
Section* Model::GetSection(String name) const
SectionPtr Model::GetSection(String name) const
{
std::list<Section*>::const_iterator cur;
std::list<SectionPtr>::const_iterator cur;
for (cur=sections.begin();cur!=sections.end();cur++) {
if ((*cur)->GetName() == name) return *cur;
}
return NULL;
return SectionPtr();
}
@ -155,7 +152,7 @@ Section* Model::GetSection(String name) const
// Inserts a new section
void Model::AddSection(String name)
{
Section *prev = GetSection(name);
SectionPtr prev = GetSection(name);
if (prev) throw Exception(Exception::Section_Already_Exists);
sections.push_back(new Section(name));
sections.push_back(SectionPtr(new Section(name)));
}

View file

@ -5,6 +5,7 @@
#include <vector>
#include <list>
#include <map>
#include "tr1.h"
// wxWidgets
#ifdef _MSC_VER

View file

@ -46,16 +46,9 @@ Section::Section(String _name)
}
//////////////
// Destructor
Section::~Section()
{
}
///////////////////
// Append an entry
void Section::AddEntry(SectionEntry *entry)
void Section::AddEntry(SectionEntryPtr entry)
{
entries.push_back(entry);
}

View file

@ -35,6 +35,8 @@
#include "section_entry.h"
#include "section_entry_dialogue.h"
#include "section_entry_style.h"
using namespace Aegilib;
@ -45,3 +47,25 @@ const String& SectionEntry::EmptyString() const
static const String str = _T("");
return str;
}
const SectionEntryPlainPtr SectionEntry::GetAsPlain(const SectionEntryPtr &ptr)
{
(void) ptr; return SectionEntryPlainPtr();
}
const SectionEntryDialoguePtr SectionEntry::GetAsDialogue(const SectionEntryPtr &ptr)
{
return dynamic_pointer_cast<SectionEntryDialogue>(ptr);
}
const SectionEntryStylePtr SectionEntry::GetAsStyle(const SectionEntryPtr &ptr)
{
return dynamic_pointer_cast<SectionEntryStyle>(ptr);
}
const SectionEntryFilePtr SectionEntry::GetAsFile(const SectionEntryPtr &ptr)
{
(void) ptr; return SectionEntryFilePtr();
}
const SectionEntryRawPtr SectionEntry::GetAsRaw(const SectionEntryPtr &ptr)
{
(void) ptr; return SectionEntryRawPtr();
}

View file

@ -53,7 +53,6 @@ TextFileReader::TextFileReader(wxInputStream &stream,Aegilib::String enc,bool _t
: file(stream)
{
// Setup
customConv = false;
trim = _trim;
// Set encoding
@ -66,8 +65,6 @@ TextFileReader::TextFileReader(wxInputStream &stream,Aegilib::String enc,bool _t
//////////////
// Destructor
TextFileReader::~TextFileReader() {
// Clean up conversion
if (customConv) delete conv;
}
@ -77,11 +74,9 @@ void TextFileReader::SetEncodingConfiguration() {
// Set encoding configuration
swap = false;
Is16 = false;
customConv = false;
conv = NULL;
conv = shared_ptr<wxMBConv>();
if (encoding == _T("UTF-8")) {
conv = new wxMBConvUTF8;
customConv = true;
conv = shared_ptr<wxMBConv> (new wxMBConvUTF8);
}
else if (encoding == _T("UTF-16LE")) {
Is16 = true;
@ -91,15 +86,13 @@ void TextFileReader::SetEncodingConfiguration() {
swap = true;
}
else if (encoding == _T("UTF-7")) {
conv = new wxCSConv(encoding);
customConv = true;
conv = shared_ptr<wxMBConv>(new wxCSConv(encoding));
}
else if (encoding == _T("Local")) {
conv = wxConvCurrent;
conv = shared_ptr<wxMBConv> (wxConvCurrent,NullDeleter());
}
else {
conv = new wxCSConv(encoding);
customConv = true;
conv = shared_ptr<wxMBConv> (new wxCSConv(encoding));
}
}

View file

@ -47,10 +47,9 @@ namespace Aegilib {
private:
wxString encoding;
wxInputStream &file;
wxMBConv *conv;
shared_ptr<wxMBConv> conv;
bool Is16;
bool swap;
bool customConv;
bool trim;
void SetEncodingConfiguration();

View file

@ -42,11 +42,10 @@ using namespace Aegilib;
// Constructor
Tokenizer::Tokenizer(String string,String token)
{
tkn = new wxStringTokenizer(string,token,wxTOKEN_RET_EMPTY_ALL);
tkn = shared_ptr<wxStringTokenizer> (new wxStringTokenizer(string,token,wxTOKEN_RET_EMPTY_ALL));
}
Tokenizer::~Tokenizer()
{
delete tkn;
}

View file

@ -38,5 +38,7 @@ using namespace Aegilib;
void View::Register(Model &model)
{
model.AddListener(this);
(void) model;
// TODO: change this
//model.AddListener(this);
}