Sanitize LyX singleton access by getting rid of ref() and cref() methods.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25597 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2008-07-14 08:35:00 +00:00
parent f67a9fac2e
commit 79beb91e3a
13 changed files with 141 additions and 239 deletions

View File

@ -286,7 +286,7 @@ Buffer::~Buffer()
}
// Remove any previewed LaTeX snippets associated with this buffer.
thePreviews()->removeLoader(*this);
thePreviews().removeLoader(*this);
delete d;
}
@ -991,7 +991,7 @@ bool Buffer::makeLaTeXFile(FileName const & fname,
}
catch (...) {
lyxerr << "Caught some really weird exception..." << endl;
LyX::cref().exit(1);
lyx_exit(1);
}
ofs.close();

View File

@ -282,7 +282,7 @@ BufferView::BufferView(Buffer & buf)
d->cursor_.setCurrentFont();
if (graphics::Previews::status() != LyXRC::PREVIEW_OFF)
thePreviews()->generateBufferPreviews(buffer_);
thePreviews().generateBufferPreviews(buffer_);
}
@ -296,7 +296,7 @@ BufferView::~BufferView()
LastFilePosSection::FilePos fp;
fp.pit = d->cursor_.bottom().pit();
fp.pos = d->cursor_.bottom().pos();
LyX::ref().session().lastFilePos().save(buffer_.fileName(), fp);
theSession().lastFilePos().save(buffer_.fileName(), fp);
delete d;
}
@ -659,7 +659,7 @@ void BufferView::saveBookmark(unsigned int idx)
// acturately locate a bookmark in a 'live' lyx session.
// pit and pos will be updated with bottom level pit/pos
// when lyx exits.
LyX::ref().session().bookmarks().save(
theSession().bookmarks().save(
buffer_.fileName(),
d->cursor_.bottom().pit(),
d->cursor_.bottom().pos(),

View File

@ -188,7 +188,7 @@ LyX::~LyX()
}
void LyX::exit(int exit_code) const
void lyx_exit(int exit_code)
{
if (exit_code)
// Something wrong happened so better save everything, just in
@ -208,20 +208,6 @@ void LyX::exit(int exit_code) const
}
LyX & LyX::ref()
{
LASSERT(singleton_, /**/);
return *singleton_;
}
LyX const & LyX::cref()
{
LASSERT(singleton_, /**/);
return *singleton_;
}
LyX::LyX()
: first_start(false)
{
@ -230,105 +216,7 @@ LyX::LyX()
}
BufferList & LyX::bufferList()
{
return pimpl_->buffer_list_;
}
BufferList const & LyX::bufferList() const
{
return pimpl_->buffer_list_;
}
Session & LyX::session()
{
LASSERT(pimpl_->session_.get(), /**/);
return *pimpl_->session_.get();
}
Session const & LyX::session() const
{
LASSERT(pimpl_->session_.get(), /**/);
return *pimpl_->session_.get();
}
LyXFunc & LyX::lyxFunc()
{
return pimpl_->lyxfunc_;
}
LyXFunc const & LyX::lyxFunc() const
{
return pimpl_->lyxfunc_;
}
Server & LyX::server()
{
LASSERT(pimpl_->lyx_server_.get(), /**/);
return *pimpl_->lyx_server_.get();
}
Server const & LyX::server() const
{
LASSERT(pimpl_->lyx_server_.get(), /**/);
return *pimpl_->lyx_server_.get();
}
ServerSocket & LyX::socket()
{
LASSERT(pimpl_->lyx_socket_.get(), /**/);
return *pimpl_->lyx_socket_.get();
}
ServerSocket const & LyX::socket() const
{
LASSERT(pimpl_->lyx_socket_.get(), /**/);
return *pimpl_->lyx_socket_.get();
}
frontend::Application & LyX::application()
{
LASSERT(pimpl_->application_.get(), /**/);
return *pimpl_->application_.get();
}
frontend::Application const & LyX::application() const
{
LASSERT(pimpl_->application_.get(), /**/);
return *pimpl_->application_.get();
}
CmdDef & LyX::topLevelCmdDef()
{
return pimpl_->toplevel_cmddef_;
}
Converters & LyX::converters()
{
return pimpl_->converters_;
}
Converters & LyX::systemConverters()
{
return pimpl_->system_converters_;
}
Messages & LyX::getMessages(string const & language)
Messages & LyX::messages(string const & language)
{
map<string, Messages>::iterator it = pimpl_->messages_.find(language);
@ -343,14 +231,9 @@ Messages & LyX::getMessages(string const & language)
}
Messages & LyX::getGuiMessages()
{
return pimpl_->messages_["GUI"];
}
void LyX::setRcGuiLanguage()
void setRcGuiLanguage()
{
LASSERT(singleton_, "");
if (lyxrc.gui_language == "auto")
return;
Language const * language = languages.getLanguage(lyxrc.gui_language);
@ -360,7 +243,7 @@ void LyX::setRcGuiLanguage()
LYXERR(Debug::LOCALE, "Setting LC_ALL to en_US");
if (!setEnv("LC_ALL", "en_US"))
LYXERR(Debug::LOCALE, "\t... failed!");
pimpl_->messages_["GUI"] = Messages();
singleton_->pimpl_->messages_["GUI"] = Messages();
}
@ -377,7 +260,7 @@ int LyX::exec(int & argc, char * argv[])
} catch (ExceptionMessage const & message) {
if (message.type_ == ErrorException) {
Alert::error(message.title_, message.details_);
exit(1);
lyx_exit(1);
} else if (message.type_ == WarningException) {
Alert::warning(message.title_, message.details_);
}
@ -582,7 +465,14 @@ bool LyX::loadFiles()
}
void LyX::execBatchCommands()
void execBatchCommands()
{
LASSERT(singleton_, /**/);
singleton_->execCommands();
}
void LyX::execCommands()
{
// The advantage of doing this here is that the event loop
// is already started. So any need for interaction will be
@ -686,7 +576,7 @@ static void error_handler(int err_sig)
// We have received a signal indicating a fatal error, so
// try and save the data ASAP.
LyX::cref().emergencyCleanup();
emergencyCleanup();
// These lyxerr calls may or may not work:
@ -894,19 +784,19 @@ bool LyX::init()
}
void LyX::emergencyCleanup() const
void emergencyCleanup()
{
// what to do about tmpfiles is non-obvious. we would
// like to delete any we find, but our lyxdir might
// contain documents etc. which might be helpful on
// a crash
pimpl_->buffer_list_.emergencyWriteAll();
singleton_->pimpl_->buffer_list_.emergencyWriteAll();
if (use_gui) {
if (pimpl_->lyx_server_)
pimpl_->lyx_server_->emergencyCleanup();
pimpl_->lyx_server_.reset();
pimpl_->lyx_socket_.reset();
if (singleton_->pimpl_->lyx_server_)
singleton_->pimpl_->lyx_server_->emergencyCleanup();
singleton_->pimpl_->lyx_server_.reset();
singleton_->pimpl_->lyx_socket_.reset();
}
}
@ -1225,25 +1115,29 @@ void LyX::easyParse(int & argc, char * argv[])
FuncStatus getStatus(FuncRequest const & action)
{
return LyX::ref().lyxFunc().getStatus(action);
LASSERT(singleton_, /**/);
return singleton_->pimpl_->lyxfunc_.getStatus(action);
}
void dispatch(FuncRequest const & action)
{
LyX::ref().lyxFunc().dispatch(action);
LASSERT(singleton_, /**/);
singleton_->pimpl_->lyxfunc_.dispatch(action);
}
BufferList & theBufferList()
{
return LyX::ref().bufferList();
LASSERT(singleton_, /**/);
return singleton_->pimpl_->buffer_list_;
}
LyXFunc & theLyXFunc()
{
return LyX::ref().lyxFunc();
LASSERT(singleton_, /**/);
return singleton_->pimpl_->lyxfunc_;
}
@ -1251,7 +1145,8 @@ Server & theServer()
{
// FIXME: this should not be use_gui dependent
LASSERT(use_gui, /**/);
return LyX::ref().server();
LASSERT(singleton_, /**/);
return *singleton_->pimpl_->lyx_server_.get();
}
@ -1259,67 +1154,92 @@ ServerSocket & theServerSocket()
{
// FIXME: this should not be use_gui dependent
LASSERT(use_gui, /**/);
return LyX::ref().socket();
LASSERT(singleton_, /**/);
return *singleton_->pimpl_->lyx_socket_.get();
}
KeyMap & theTopLevelKeymap()
{
return LyX::ref().pimpl_->toplevel_keymap_;
LASSERT(singleton_, /**/);
return singleton_->pimpl_->toplevel_keymap_;
}
Converters & theConverters()
{
return LyX::ref().converters();
LASSERT(singleton_, /**/);
return singleton_->pimpl_->converters_;
}
Converters & theSystemConverters()
{
return LyX::ref().systemConverters();
LASSERT(singleton_, /**/);
return singleton_->pimpl_->system_converters_;
}
Movers & theMovers()
{
return LyX::ref().pimpl_->movers_;
LASSERT(singleton_, /**/);
return singleton_->pimpl_->movers_;
}
Mover const & getMover(string const & fmt)
{
return LyX::ref().pimpl_->movers_(fmt);
LASSERT(singleton_, /**/);
return singleton_->pimpl_->movers_(fmt);
}
void setMover(string const & fmt, string const & command)
{
LyX::ref().pimpl_->movers_.set(fmt, command);
LASSERT(singleton_, /**/);
singleton_->pimpl_->movers_.set(fmt, command);
}
Movers & theSystemMovers()
{
return LyX::ref().pimpl_->system_movers_;
LASSERT(singleton_, /**/);
return singleton_->pimpl_->system_movers_;
}
Messages & getMessages(string const & language)
{
return LyX::ref().getMessages(language);
LASSERT(singleton_, /**/);
return singleton_->messages(language);
}
Messages & getGuiMessages()
{
return LyX::ref().getGuiMessages();
LASSERT(singleton_, /**/);
return singleton_->pimpl_->messages_["GUI"];
}
graphics::Previews * thePreviews()
graphics::Previews & thePreviews()
{
return singleton_ ? 0 : &singleton_->pimpl_->preview_;
LASSERT(singleton_, /**/);
return singleton_->pimpl_->preview_;
}
Session & theSession()
{
LASSERT(singleton_, /**/);
return *singleton_->pimpl_->session_.get();
}
CmdDef & theTopLevelCmdDef()
{
LASSERT(singleton_, /**/);
return singleton_->pimpl_->toplevel_cmddef_;
}
} // namespace lyx

View File

@ -22,6 +22,8 @@ class BufferList;
class CmdDef;
class Converters;
class ErrorItem;
class FuncRequest;
class FuncStatus;
class KeyMap;
class LyXFunc;
class Messages;
@ -52,57 +54,9 @@ public:
/// Execute LyX.
int exec(int & argc, char * argv[]);
/// Try to exit LyX properly.
/// \p exit_code is 0 by default, if a non zero value is passed,
/// emergencyCleanup() will be called before exiting.
void exit(int exit_code = 0) const;
static LyX & ref();
static LyX const & cref();
/// in the case of failure
void emergencyCleanup() const;
///
BufferList & bufferList();
BufferList const & bufferList() const;
///
Session & session();
Session const & session() const;
///
LyXFunc & lyxFunc();
LyXFunc const & lyxFunc() const;
///
Server & server();
Server const & server() const;
///
ServerSocket & socket();
ServerSocket const & socket() const;
///
frontend::Application & application();
frontend::Application const & application() const;
///
CmdDef & topLevelCmdDef();
///
Converters & converters();
Converters & systemConverters();
///
Messages & getMessages(std::string const & language);
///
Messages & getGuiMessages();
///
void setRcGuiLanguage();
///
frontend::LyXView * newLyXView();
/// Execute batch commands if available.
void execBatchCommands();
private:
/// noncopyable
LyX(LyX const &);
@ -120,6 +74,9 @@ private:
*/
int init(int & argc, char * argv[]);
/// Execute batch commands if available.
void execCommands();
/// Load files passed at command-line.
/// return true on success false if we encounter an error
/**
@ -150,6 +107,9 @@ private:
/// shows up a parsing error on screen
void printError(ErrorItem const &);
///
Messages & messages(std::string const & language);
/// Use the Pimpl idiom to hide the internals.
// Mostly used for singletons.
struct Impl;
@ -158,13 +118,35 @@ private:
/// has this user started lyx for the first time?
bool first_start;
friend FuncStatus getStatus(FuncRequest const & action);
friend void dispatch(FuncRequest const & action);
friend BufferList & theBufferList();
friend LyXFunc & theLyXFunc();
friend Server & theServer();
friend ServerSocket & theServerSocket();
friend Converters & theConverters();
friend Converters & theSystemConverters();
friend Messages & getMessages(std::string const & language);
friend Messages & getGuiMessages();
friend KeyMap & theTopLevelKeymap();
friend Movers & theMovers();
friend Mover const & getMover(std::string const & fmt);
friend void setMover(std::string const & fmt, std::string const & command);
friend Movers & theSystemMovers();
friend frontend::Application * theApp();
friend KeyMap & theTopLevelKeymap();
friend graphics::Previews * thePreviews();
friend graphics::Previews & thePreviews();
friend Session & theSession();
friend CmdDef & theTopLevelCmdDef();
/// Set the language defined by the user.
friend void setRcGuiLanguage();
/// in the case of failure
friend void emergencyCleanup();
/// Execute batch commands if available.
friend void execBatchCommands();
/// Try to exit LyX properly.
/// \p exit_code is 0 by default, if a non zero value is passed,
/// emergencyCleanup() will be called before exiting.
friend void lyx_exit(int exit_code = 0);
};
} // namespace lyx

View File

@ -240,9 +240,9 @@ void LyXFunc::handleKeyFunc(FuncCode action)
void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
{
LASSERT(lyx_view_, /**/);
if (!LyX::ref().session().bookmarks().isValid(idx))
if (!theSession().bookmarks().isValid(idx))
return;
BookmarksSection::Bookmark const & bm = LyX::ref().session().bookmarks().bookmark(idx);
BookmarksSection::Bookmark const & bm = theSession().bookmarks().bookmark(idx);
LASSERT(!bm.filename.empty(), /**/);
string const file = bm.filename.absFilename();
// if the file is not opened, open it.
@ -512,12 +512,12 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
case LFUN_BOOKMARK_GOTO: {
const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
enable = LyX::ref().session().bookmarks().isValid(num);
enable = theSession().bookmarks().isValid(num);
break;
}
case LFUN_BOOKMARK_CLEAR:
enable = LyX::ref().session().bookmarks().size() > 0;
enable = theSession().bookmarks().size() > 0;
break;
// this one is difficult to get right. As a half-baked
@ -534,10 +534,10 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
case LFUN_CALL: {
FuncRequest func;
string name = to_utf8(cmd.argument());
if (LyX::ref().topLevelCmdDef().lock(name, func)) {
if (theTopLevelCmdDef().lock(name, func)) {
func.origin = cmd.origin;
flag = getStatus(func);
LyX::ref().topLevelCmdDef().release(name);
theTopLevelCmdDef().release(name);
} else {
// catch recursion or unknown command definiton
// all operations until the recursion or unknown command
@ -1177,7 +1177,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
break;
}
case ERT_CODE: {
data = InsetERT::params2string(InsetCollapsable::Open);
data = InsetERT::params2string(InsetCollapsable::Open, docstring());
break;
}
case EXTERNAL_CODE: {
@ -1345,10 +1345,10 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
case LFUN_CALL: {
FuncRequest func;
if (LyX::ref().topLevelCmdDef().lock(argument, func)) {
if (theTopLevelCmdDef().lock(argument, func)) {
func.origin = cmd.origin;
dispatch(func);
LyX::ref().topLevelCmdDef().release(argument);
theTopLevelCmdDef().release(argument);
} else {
if (func.action == LFUN_UNKNOWN_ACTION) {
// unknown command definition
@ -1529,9 +1529,6 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
actOnUpdatedPrefs(lyxrc_orig, lyxrc);
// Set the language defined by the user.
LyX::ref().setRcGuiLanguage();
theApp()->resetGui();
/// We force the redraw in any case because there might be
@ -1549,7 +1546,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
break;
case LFUN_BOOKMARK_CLEAR:
LyX::ref().session().bookmarks().clear();
theSession().bookmarks().clear();
break;
default:

View File

@ -324,6 +324,7 @@ public:
SessionInfoSection const & sessionInfo() const { return session_info; }
private:
friend class LyX;
/// uncopiable
Session(Session const &);
void operator=(Session const &);
@ -350,6 +351,10 @@ private:
SessionInfoSection session_info;
};
}
/// This is a singleton class. Get the instance.
/// Implemented in LyX.cpp.
Session & theSession();
} // lyx
#endif

View File

@ -22,7 +22,6 @@
using namespace std;
using lyx::lyxerr;
using lyx::LyX;
namespace boost {
@ -45,7 +44,7 @@ void assertion_failed(char const * expr, char const * function,
// FIXME: by default we exit here but we could also inform the user
// about the assertion and do the emergency cleanup without exiting.
// FIXME: do we have a list of exit codes defined somewhere?
LyX::cref().exit(1);
lyx::lyx_exit(1);
}
} // namespace boost

View File

@ -388,7 +388,7 @@ Inset * createInsetHelper(Buffer & buf, FuncRequest const & cmd)
if (message.type_ == ErrorException) {
// This should never happen!
Alert::error(message.title_, message.details_);
LyX::cref().exit(1);
lyx_exit(1);
} else if (message.type_ == WarningException) {
Alert::warning(message.title_, message.details_);
return 0;

View File

@ -788,7 +788,7 @@ bool GuiApplication::dispatch(FuncRequest const & cmd)
case LFUN_WINDOW_CLOSE:
// update bookmark pit of the current buffer before window close
for (size_t i = 0; i < LyX::ref().session().bookmarks().size(); ++i)
for (size_t i = 0; i < theSession().bookmarks().size(); ++i)
theLyXFunc().gotoBookmark(i+1, false, false);
current_view_->close();
break;
@ -904,7 +904,7 @@ bool GuiApplication::dispatch(FuncRequest const & cmd)
void GuiApplication::resetGui()
{
// Set the language defined by the user.
LyX::ref().setRcGuiLanguage();
setRcGuiLanguage();
// Read menus
if (!readUIFile(toqstr(lyxrc.ui_file)))
@ -1042,7 +1042,7 @@ void GuiApplication::exit(int status)
void GuiApplication::execBatchCommands()
{
// Set the language defined by the user.
LyX::ref().setRcGuiLanguage();
setRcGuiLanguage();
// Read menus
if (!readUIFile(toqstr(lyxrc.ui_file)))
@ -1054,7 +1054,7 @@ void GuiApplication::execBatchCommands()
if (d->global_menubar_)
d->menus_.fillMenuBar(d->global_menubar_, 0, true);
LyX::ref().execBatchCommands();
lyx::execBatchCommands();
}
QAbstractItemModel * GuiApplication::languageModel()
@ -1088,7 +1088,7 @@ void GuiApplication::restoreGuiSession()
if (!lyxrc.load_session)
return;
Session & session = LyX::ref().session();
Session & session = theSession();
vector<FileName> const & lastopened = session.lastOpened().getfiles();
// do not add to the lastfile list since these files are restored from
// last session, and should be already there (regular files), or should
@ -1165,7 +1165,7 @@ bool GuiApplication::notify(QObject * receiver, QEvent * event)
catch (ExceptionMessage const & e) {
switch(e.type_) {
case ErrorException:
LyX::cref().emergencyCleanup();
emergencyCleanup();
setQuitOnLastWindowClosed(false);
closeAllViews();
Alert::error(e.title_, e.details_);
@ -1196,13 +1196,13 @@ bool GuiApplication::notify(QObject * receiver, QEvent * event)
"\n\nException: ");
s += from_ascii(e.what());
Alert::error(_("Software exception Detected"), s);
LyX::cref().exit(1);
lyx_exit(1);
}
catch (...) {
docstring s = _("LyX has caught some really weird exception, it will "
"now attempt to save all unsaved documents and exit.");
Alert::error(_("Software exception Detected"), s);
LyX::cref().exit(1);
lyx_exit(1);
}
return false;

View File

@ -1242,12 +1242,12 @@ Buffer * GuiView::loadDocument(FileName const & filename, bool tolastfiles)
// scroll to the position when the file was last closed
if (lyxrc.use_lastfilepos) {
LastFilePosSection::FilePos filepos =
LyX::ref().session().lastFilePos().load(filename);
theSession().lastFilePos().load(filename);
view()->moveToPosition(filepos.pit, filepos.pos, 0, 0);
}
if (tolastfiles)
LyX::ref().session().lastFiles().add(filename);
theSession().lastFiles().add(filename);
setBusy(false);
return newBuffer;
@ -1666,7 +1666,7 @@ bool GuiView::saveBuffer(Buffer & b)
return renameBuffer(b, docstring());
if (b.save()) {
LyX::ref().session().lastFiles().add(b.fileName());
theSession().lastFiles().add(b.fileName());
return true;
}
@ -1706,12 +1706,12 @@ bool GuiView::closeBuffer(Buffer & buf, bool tolastopened)
{
// goto bookmark to update bookmark pit.
//FIXME: we should update only the bookmarks related to this buffer!
for (size_t i = 0; i < LyX::ref().session().bookmarks().size(); ++i)
for (size_t i = 0; i < theSession().bookmarks().size(); ++i)
theLyXFunc().gotoBookmark(i+1, false, false);
if (buf.isClean() || buf.paragraphs().empty()) {
if (buf.masterBuffer() == &buf && tolastopened)
LyX::ref().session().lastOpened().add(buf.fileName());
theSession().lastOpened().add(buf.fileName());
theBufferList().release(&buf);
return true;
}
@ -1753,7 +1753,7 @@ bool GuiView::closeBuffer(Buffer & buf, bool tolastopened)
// if master/slave are both open, do not save slave since it
// will be automatically loaded when the master is loaded
if (buf.masterBuffer() == &buf && tolastopened)
LyX::ref().session().lastOpened().add(buf.fileName());
theSession().lastOpened().add(buf.fileName());
if (buf.parent())
// Don't close child documents.

View File

@ -656,7 +656,7 @@ void MenuDefinition::expandGraphicsGroups(BufferView const * bv)
void MenuDefinition::expandLastfiles()
{
LastFilesSection::LastFiles const & lf = LyX::cref().session().lastFiles().lastFiles();
LastFilesSection::LastFiles const & lf = theSession().lastFiles().lastFiles();
LastFilesSection::LastFiles::const_iterator lfit = lf.begin();
int ii = 1;
@ -699,7 +699,7 @@ void MenuDefinition::expandDocuments()
void MenuDefinition::expandBookmarks()
{
lyx::BookmarksSection const & bm = LyX::cref().session().bookmarks();
lyx::BookmarksSection const & bm = theSession().bookmarks();
for (size_t i = 1; i <= bm.size(); ++i) {
if (bm.isValid(i)) {

View File

@ -59,7 +59,7 @@ private:
/// This is a singleton class. Get the instance.
/// Implemented in LyX.cpp.
graphics::Previews * thePreviews();
graphics::Previews & thePreviews();
} // namespace lyx

View File

@ -76,8 +76,7 @@ namespace {
graphics::PreviewLoader & getPreviewLoader(Buffer const & buffer)
{
graphics::Previews const & previews = graphics::Previews::get();
return previews.loader(buffer);
return thePreviews().loader(buffer);
}