Georg Baum wrote:
Am Freitag, 17. November 2006 21:06 schrieb Peter Kümmel:
Georg Baum wrote:
Peter Kümmel wrote:
My idea was to use some Loki code for singletons and global static
data.
I don't know Loki, so maybe I am missing something here, but I believe
that
we already have everything needed.
Yes, we have C++.
What do you mean? I believe that Loki is a well designed library of good
quality, but I do not see the problem that it might solve for us. Our
problem is that some global static objects are destructed too late,
because they need other objects. This problem can be solved IMHO by
putting these objects into the LyX singleton, so I don't see why we need
any additional code.
This is what I did. Hopefully, all crash-on-exit problems on windows and
Mac should be solved now.
Abdel.
Author: younes
Date: Sat Nov 18 18:47:12 2006
New Revision: 15967
URL: http://www.lyx.org/trac/changeset/15967
Log:
* lyx_main.[Ch]
- merge exec() and priv_exec()
- LyX constructor is now public.
- there is no global singleton_ object anymore, only a pointer.
- IconvProcessor(): new utf8 to ucs4 convertor.
* main.C: instantiate a LyX object and exec() it. No more use of static
methods.
* docstring.C: use utf8ToUcs4() instead of a static variable.
* unicode.h: declare extern utf8ToUcs4().
Modified:
lyx-devel/trunk/src/lyx_main.C
lyx-devel/trunk/src/lyx_main.h
lyx-devel/trunk/src/main.C
lyx-devel/trunk/src/support/docstring.C
lyx-devel/trunk/src/support/unicode.h
Modified: lyx-devel/trunk/src/lyx_main.C
URL: http://www.lyx.org/trac/file/lyx-devel/trunk/src/lyx_main.C?rev=15967
==============================================================================
--- lyx-devel/trunk/src/lyx_main.C (original)
+++ lyx-devel/trunk/src/lyx_main.C Sat Nov 18 18:47:12 2006
@@ -57,6 +57,7 @@
#include "support/package.h"
#include "support/path.h"
#include "support/systemcall.h"
+#include "support/unicode.h"
#include <boost/bind.hpp>
#include <boost/filesystem/operations.hpp>
@@ -114,6 +115,7 @@
string cl_system_support;
string cl_user_support;
+LyX * singleton_ = 0;
void showFileError(string const & error)
{
@@ -140,6 +142,9 @@
/// The main application class private implementation.
struct LyX::Singletons
{
+ Singletons(): iconv(ucs4_codeset, "UTF-8")
+ {
+ }
/// our function handler
LyXFunc lyxfunc_;
///
@@ -154,10 +159,11 @@
boost::scoped_ptr<frontend::Application> application_;
/// lyx session, containing lastfiles, lastfilepos, and lastopened
boost::scoped_ptr<Session> session_;
+
+ ///
+ IconvProcessor iconv;
};
-
-boost::scoped_ptr<LyX> LyX::singleton_;
LyX::~LyX()
{
@@ -171,35 +177,24 @@
}
-int LyX::exec(int & argc, char * argv[])
-{
- BOOST_ASSERT(!singleton_.get());
- // We must return from this before launching the gui so that
- // other parts of the code can access singleton_ through
- // LyX::ref and LyX::cref.
- singleton_.reset(new LyX);
- // Start the real execution loop.
- return singleton_->priv_exec(argc, argv);
-}
-
-
LyX & LyX::ref()
{
- BOOST_ASSERT(singleton_.get());
- return *singleton_.get();
+ BOOST_ASSERT(singleton_);
+ return *singleton_;
}
LyX const & LyX::cref()
{
- BOOST_ASSERT(singleton_.get());
- return *singleton_.get();
+ BOOST_ASSERT(singleton_);
+ return *singleton_;
}
LyX::LyX()
: first_start(false), geometryOption_(false)
{
+ singleton_ = this;
pimpl_.reset(new Singletons);
}
@@ -288,6 +283,12 @@
{
BOOST_ASSERT(pimpl_->toplevel_keymap_.get());
return *pimpl_->toplevel_keymap_.get();
+}
+
+
+IconvProcessor & LyX::iconvProcessor()
+{
+ return pimpl_->iconv;
}
@@ -317,7 +318,7 @@
}
-int LyX::priv_exec(int & argc, char * argv[])
+int LyX::exec(int & argc, char * argv[])
{
// Here we need to parse the command line. At least
// we need to parse for "-dbg" and "-help"
@@ -387,12 +388,9 @@
lyxerr[Debug::INFO] << "Deleting tmp dir " << package().temp_dir() <<
endl;
if (!destroyDir(package().temp_dir())) {
- // FIXME UNICODE: package().temp_dir() could in theory contain utf8
characters.
- // We cannot use from_utf8() here because this involves the use of
static data
- // that may have been destroyed already on Mac systems.
docstring const msg =
bformat(_("Unable to remove the temporary directory
%1$s"),
- from_ascii(package().temp_dir()));
+ from_utf8(package().temp_dir()));
Alert::warning(_("Unable to remove temporary directory"), msg);
}
}
@@ -1325,4 +1323,10 @@
return LyX::ref().topLevelKeymap();
}
+
+IconvProcessor & utf8ToUcs4()
+{
+ return LyX::ref().iconvProcessor();
+}
+
} // namespace lyx
Modified: lyx-devel/trunk/src/lyx_main.h
URL: http://www.lyx.org/trac/file/lyx-devel/trunk/src/lyx_main.h?rev=15967
==============================================================================
--- lyx-devel/trunk/src/lyx_main.h (original)
+++ lyx-devel/trunk/src/lyx_main.h Sat Nov 18 18:47:12 2006
@@ -25,6 +25,7 @@
class Buffer;
class BufferList;
class ErrorItem;
+class IconvProcessor;
class InsetBase;
class LyXFunc;
class LyXServer;
@@ -41,6 +42,7 @@
class LyX : boost::noncopyable {
public:
+ LyX();
~LyX();
/**
@@ -48,16 +50,14 @@
* -# LyX::exec()
* -# LyX::priv_exec()
* -# lyx::createApplication()
- * -# LyX::exec2()
* Step 3 is omitted if no gui is wanted. We need
lyx::createApplication()
* only to create the QApplication object in the qt frontend. All
* attempts with static and dynamically allocated QApplication
* objects lead either to harmless error messages on exit
* ("Mutex destroy failure") or crashes (OS X).
*/
- static int exec(int & argc, char * argv[]);
- /// Execute LyX (inner execution loop, \sa exec)
- int exec2(int & argc, char * argv[]);
+ int exec(int & argc, char * argv[]);
+
static LyX & ref();
static LyX const & cref();
@@ -95,6 +95,9 @@
kb_keymap & topLevelKeymap();
kb_keymap const & topLevelKeymap() const;
+ ///
+ IconvProcessor & iconvProcessor();
+
LyXView * newLyXView();
/** redraw \c inset in all the BufferViews in which it is currently
@@ -103,11 +106,6 @@
Buffer const * const updateInset(InsetBase const *) const;
private:
- static boost::scoped_ptr<LyX> singleton_;
-
- LyX();
- int priv_exec(int & argc, char * argv[]);
-
/// Do some cleanup in preparation of an exit.
void prepareExit();
Modified: lyx-devel/trunk/src/main.C
URL: http://www.lyx.org/trac/file/lyx-devel/trunk/src/main.C?rev=15967
==============================================================================
--- lyx-devel/trunk/src/main.C (original)
+++ lyx-devel/trunk/src/main.C Sat Nov 18 18:47:12 2006
@@ -43,5 +43,7 @@
// initialize for internationalized version *EK*
lyx::locale_init();
- return lyx::LyX::exec(argc, argv);
+ lyx::LyX the_lyx_instance;
+
+ return the_lyx_instance.exec(argc, argv);
}
Modified: lyx-devel/trunk/src/support/docstring.C
URL:
http://www.lyx.org/trac/file/lyx-devel/trunk/src/support/docstring.C?rev=15967
==============================================================================
--- lyx-devel/trunk/src/support/docstring.C (original)
+++ lyx-devel/trunk/src/support/docstring.C Sat Nov 18 18:47:12 2006
@@ -57,14 +57,6 @@
void utf8_to_ucs4(std::string const & utf8, docstring & ucs4)
{
- // FIXME (Abdel 17/11/06): static data are evil!
- // This function cannot be used in the final exit process on Mac because
- // static data are already destroyed at this stage.
- // One solution would be to instantiate the utf8 to ucs4
IconvProcessor as a
- // singleton inside the LyX main class to ensure that it does not get
- // destroyed too early.
- static IconvProcessor iconv(ucs4_codeset, "UTF-8");
-
size_t n = utf8.size();
// as utf8 is a multi-byte encoding, there would be at most
// n characters:
@@ -76,7 +68,7 @@
// basic_string::data() is not recognized by some old gcc version
// so we use &(ucs4[0]) instead.
char * outbuf = (char *)(&(ucs4[0]));
- int bytes = iconv.convert(utf8.c_str(), n, outbuf, maxoutsize);
+ int bytes = utf8ToUcs4().convert(utf8.c_str(), n, outbuf, maxoutsize);
// adjust to the real converted size
ucs4.resize(bytes/4);
Modified: lyx-devel/trunk/src/support/unicode.h
URL:
http://www.lyx.org/trac/file/lyx-devel/trunk/src/support/unicode.h?rev=15967
==============================================================================
--- lyx-devel/trunk/src/support/unicode.h (original)
+++ lyx-devel/trunk/src/support/unicode.h Sat Nov 18 18:47:12 2006
@@ -56,7 +56,7 @@
boost::scoped_ptr<Private> pimpl_;
};
-// utf8_to_ucs4
+extern IconvProcessor & utf8ToUcs4();
// A single codepoint conversion for utf8_to_ucs4 does not make
// sense, so that function is left out.