DocumentClass const & BufferParams::documentClass() const
{
- return *doc_class_;
+ return *doc_class_.get();
}
-DocumentClass const * BufferParams::documentClassPtr() const
+DocumentClassConstPtr BufferParams::documentClassPtr() const
{
return doc_class_;
}
-void BufferParams::setDocumentClass(DocumentClass const * const tc)
+void BufferParams::setDocumentClass(DocumentClassConstPtr tc)
{
// evil, but this function is evil
- doc_class_ = const_cast<DocumentClass *>(tc);
+ doc_class_ = const_pointer_cast<DocumentClass>(tc);
}
en = cite_engine_.end();
for (; it != en; ++it)
mods.push_back(*it);
- doc_class_ = &(DocumentClassBundle::get().makeDocumentClass(*baseClass(), mods));
+ doc_class_ = getDocumentClass(*baseClass(), mods);
if (!local_layout.empty()) {
TextClass::ReturnValues success =
#define BUFFERPARAMS_H
#include "Citation.h"
+#include "DocumentClassPtr.h"
#include "Format.h"
#include "LayoutModuleList.h"
#include "OutputParams.h"
DocumentClass const & documentClass() const;
/// \return A pointer to the DocumentClass currently in use: the BaseClass
/// as modified by modules.
- DocumentClass const * documentClassPtr() const;
+ DocumentClassConstPtr documentClassPtr() const;
/// This bypasses the baseClass and sets the textClass directly.
/// Should be called with care and would be better not being here,
/// but it seems to be needed by CutAndPaste::putClipboard().
- void setDocumentClass(DocumentClass const * const);
+ void setDocumentClass(DocumentClassConstPtr);
/// List of modules in use
LayoutModuleList const & getModules() const { return layout_modules_; }
/// List of default modules the user has removed
/// the type of cite engine (authoryear or numerical)
CiteEngineType cite_engine_type_;
///
- DocumentClass * doc_class_;
+ DocumentClassPtr doc_class_;
///
LayoutModuleList layout_modules_;
/// this is for modules that are required by the document class but that
}
-void BufferView::updateDocumentClass(DocumentClass const * const olddc)
+void BufferView::updateDocumentClass(DocumentClassConstPtr olddc)
{
message(_("Converting document to new document class..."));
switch (act) {
case LFUN_BUFFER_PARAMS_APPLY: {
- DocumentClass const * const oldClass = buffer_.params().documentClassPtr();
+ DocumentClassConstPtr oldClass = buffer_.params().documentClassPtr();
cur.recordUndoFullDocument();
istringstream ss(to_utf8(cmd.argument()));
Lexer lex;
}
case LFUN_LAYOUT_MODULES_CLEAR: {
- DocumentClass const * const oldClass =
- buffer_.params().documentClassPtr();
+ DocumentClassConstPtr oldClass = buffer_.params().documentClassPtr();
cur.recordUndoFullDocument();
buffer_.params().clearLayoutModules();
buffer_.params().makeDocumentClass();
"conflicts with installed modules.");
break;
}
- DocumentClass const * const oldClass = params.documentClassPtr();
+ DocumentClassConstPtr oldClass = params.documentClassPtr();
cur.recordUndoFullDocument();
buffer_.params().addLayoutModule(argument);
buffer_.params().makeDocumentClass();
break;
// Save the old, possibly modular, layout for use in conversion.
- DocumentClass const * const oldDocClass =
- buffer_.params().documentClassPtr();
+ DocumentClassConstPtr oldDocClass = buffer_.params().documentClassPtr();
cur.recordUndoFullDocument();
buffer_.params().setBaseClass(argument);
buffer_.params().makeDocumentClass();
}
case LFUN_LAYOUT_RELOAD: {
- DocumentClass const * const oldClass = buffer_.params().documentClassPtr();
+ DocumentClassConstPtr oldClass = buffer_.params().documentClassPtr();
LayoutFileIndex bc = buffer_.params().baseClassID();
LayoutFileList::get().reset(bc);
buffer_.params().setBaseClass(bc);
#ifndef BUFFER_VIEW_H
#define BUFFER_VIEW_H
+#include "DocumentClassPtr.h"
#include "update_flags.h"
+#include "support/shared_ptr.h"
#include "support/strfwd.h"
#include "support/types.h"
void updateHoveredInset() const;
///
- void updateDocumentClass(DocumentClass const * const olddc);
+ void updateDocumentClass(DocumentClassConstPtr olddc);
///
int width_;
///
typedef pair<pit_type, int> PitPosPair;
-typedef limited_stack<pair<ParagraphList, DocumentClass const *> > CutStack;
+typedef limited_stack<pair<ParagraphList, DocumentClassConstPtr> > CutStack;
CutStack theCuts(10);
// persistent selection, cleared until the next selection
pair<PitPosPair, pit_type>
pasteSelectionHelper(Cursor const & cur, ParagraphList const & parlist,
- DocumentClass const * const oldDocClass, ErrorList & errorlist)
+ DocumentClassConstPtr oldDocClass, ErrorList & errorlist)
{
Buffer const & buffer = *cur.buffer();
pit_type pit = cur.pit();
// Make a copy of the CaP paragraphs.
ParagraphList insertion = parlist;
- DocumentClass const * const newDocClass =
- buffer.params().documentClassPtr();
+ DocumentClassConstPtr newDocClass = buffer.params().documentClassPtr();
// Now remove all out of the pars which is NOT allowed in the
// new environment and set also another font if that is required.
void putClipboard(ParagraphList const & paragraphs,
- DocumentClass const * const docclass, docstring const & plaintext)
+ DocumentClassConstPtr docclass, docstring const & plaintext)
{
// For some strange reason gcc 3.2 and 3.3 do not accept
// Buffer buffer(string(), false);
- // This needs to be static to avoid a memory leak. When a Buffer is
- // constructed, it constructs a BufferParams, which in turn constructs
- // a DocumentClass, via new, that is never deleted. If we were to go to
- // some kind of garbage collection there, or a shared_ptr, then this
- // would not be needed.
+ // This used to need to be static to avoid a memory leak. It no longer needs
+ // to be so, but the alternative is to construct a new one of these (with a
+ // new temporary directory, etc) every time, and then to destroy it. So maybe
+ // it's worth just keeping this one around.
static Buffer * buffer = theBufferList().newInternalBuffer(
FileName::tempName("clipboard.internal").absFileName());
buffer->setUnnamed(true);
void copySelectionHelper(Buffer const & buf, Text const & text,
pit_type startpit, pit_type endpit,
- int start, int end, DocumentClass const * const dc, CutStack & cutstack)
+ int start, int end, DocumentClassConstPtr dc, CutStack & cutstack)
{
ParagraphList const & pars = text.paragraphs();
it->setInsetOwner(0);
}
- DocumentClass * d = const_cast<DocumentClass *>(dc);
- cutstack.push(make_pair(copy_pars, d));
+ cutstack.push(make_pair(copy_pars, dc));
}
} // namespace anon
}
-void switchBetweenClasses(DocumentClass const * const oldone,
- DocumentClass const * const newone, InsetText & in, ErrorList & errorlist)
+void switchBetweenClasses(DocumentClassConstPtr oldone,
+ DocumentClassConstPtr newone, InsetText & in, ErrorList & errorlist)
{
errorlist.clear();
void pasteParagraphList(Cursor & cur, ParagraphList const & parlist,
- DocumentClass const * const docclass, ErrorList & errorList)
+ DocumentClassConstPtr docclass, ErrorList & errorList)
{
if (cur.inTexted()) {
Text * text = cur.text();
#ifndef CUTANDPASTE_H
#define CUTANDPASTE_H
+#include "DocumentClassPtr.h"
+
#include "support/docstring.h"
#include "frontends/Clipboard.h"
/// Paste the paragraph list \p parlist at the position given by \p cur.
/// Does not handle undo. Does only work in text, not mathed.
void pasteParagraphList(Cursor & cur, ParagraphList const & parlist,
- DocumentClass const * const textclass, ErrorList & errorList);
+ DocumentClassConstPtr textclass, ErrorList & errorList);
/** Needed to switch between different classes. This works
* for a list of paragraphs beginning with the specified par.
* It changes layouts and character styles.
*/
-void switchBetweenClasses(DocumentClass const * const c1,
- DocumentClass const * const c2, InsetText & in, ErrorList &);
+void switchBetweenClasses(DocumentClassConstPtr c1,
+ DocumentClassConstPtr c2, InsetText & in, ErrorList &);
/// Get the current selection as a string. Does not change the selection.
/// Does only work if the whole selection is in mathed.
--- /dev/null
+// -*- C++ -*-
+/**
+ * \file DocumentClassPtr.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Richard Heck
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#ifndef DOCUMENT_CLASS_PTR_H
+#define DOCUMENT_CLASS_PTR_H
+
+#include "support/shared_ptr.h"
+
+namespace lyx {
+class DocumentClass;
+
+typedef shared_ptr<DocumentClass> DocumentClassPtr;
+typedef shared_ptr<DocumentClass const> DocumentClassConstPtr;
+}
+
+#endif // DISPATCH_RESULT_H
DepTable.h \
DispatchResult.h \
DocIterator.h \
+ DocumentClassPtr.h \
Encoding.h \
ErrorList.h \
Exporter.h \
}
-/////////////////////////////////////////////////////////////////////////
-//
-// DocumentClassBundle
-//
-/////////////////////////////////////////////////////////////////////////
-
-DocumentClassBundle::~DocumentClassBundle()
-{
- for (size_t i = 0; i != documentClasses_.size(); ++i)
- delete documentClasses_[i];
- documentClasses_.clear();
-}
-
-DocumentClass & DocumentClassBundle::newClass(LayoutFile const & baseClass)
-{
- DocumentClass * dc = new DocumentClass(baseClass);
- documentClasses_.push_back(dc);
- return *documentClasses_.back();
-}
-
-
-DocumentClassBundle & DocumentClassBundle::get()
-{
- static DocumentClassBundle singleton;
- return singleton;
-}
-
-
-DocumentClass & DocumentClassBundle::makeDocumentClass(
+DocumentClassPtr getDocumentClass(
LayoutFile const & baseClass, LayoutModuleList const & modlist)
{
- DocumentClass & doc_class = newClass(baseClass);
+ DocumentClassPtr doc_class =
+ DocumentClassPtr(new DocumentClass(baseClass));
LayoutModuleList::const_iterator it = modlist.begin();
LayoutModuleList::const_iterator en = modlist.end();
for (; it != en; ++it) {
frontend::Alert::warning(_("Package not available"), msg, true);
}
FileName layout_file = libFileSearch("layouts", lm->getFilename());
- if (!doc_class.read(layout_file, TextClass::MODULE)) {
+ if (!doc_class->read(layout_file, TextClass::MODULE)) {
docstring const msg =
bformat(_("Error reading module %1$s\n"), from_utf8(modName));
frontend::Alert::warning(_("Read Error"), msg);
#include "Citation.h"
#include "ColorCode.h"
#include "Counters.h"
+#include "DocumentClassPtr.h"
#include "FloatList.h"
#include "FontInfo.h"
#include "Layout.h"
#include "support/docstring.h"
#include "support/types.h"
-#include <boost/noncopyable.hpp>
-
#include <list>
#include <map>
#include <set>
/// In the main LyX code, DocumentClass objects are created only by
/// DocumentClassBundle, for which see below.
///
-class DocumentClass : public TextClass, boost::noncopyable {
+class DocumentClass : public TextClass {
public:
///
virtual ~DocumentClass() {}
private:
/// The only class that can create a DocumentClass is
/// DocumentClassBundle, which calls the protected constructor.
- friend class DocumentClassBundle;
+ friend DocumentClassPtr
+ getDocumentClass(LayoutFile const &, LayoutModuleList const &);
///
static InsetLayout plain_insetlayout_;
};
-/// DocumentClassBundle is a container for DocumentClass objects, so that
-/// they stay in memory for use by Insets, CutAndPaste, and the like, even
-/// when their associated Buffers are destroyed.
-/// FIXME Some sort of garbage collection or reference counting wouldn't
-/// be a bad idea here. It might be enough to check when a Buffer is closed
-/// (or makeDocumentClass is called) whether the old DocumentClass is in use
-/// anywhere.
-///
-/// This is a singleton class. Its sole instance is accessed via
-/// DocumentClassBundle::get().
-class DocumentClassBundle : boost::noncopyable {
-public:
- /// \return The sole instance of this class.
- static DocumentClassBundle & get();
- /// \return A new DocumentClass based on baseClass, with info added
- /// from the modules in modlist.
- DocumentClass & makeDocumentClass(LayoutFile const & baseClass,
+/// The only way to make a DocumentClass is to call this function.
+/// The shared_ptr is needed because DocumentClass objects can be kept
+/// in memory long after their associated Buffer is destroyed, mostly
+/// on the CutStack.
+DocumentClassPtr getDocumentClass(LayoutFile const & baseClass,
LayoutModuleList const & modlist);
-private:
- /// control instantiation
- DocumentClassBundle() {}
- /// clean up
- ~DocumentClassBundle();
- /// \return Reference to a new DocumentClass equal to baseClass
- DocumentClass & newClass(LayoutFile const & baseClass);
- ///
- std::vector<DocumentClass *> documentClasses_;
-};
-
/// convert page sides option to text 1 or 2
std::ostream & operator<<(std::ostream & os, PageSides p);
namespace lyx
{
using std::tr1::shared_ptr;
+ using std::tr1::const_pointer_cast;
}
#else
namespace lyx
{
using boost::shared_ptr;
+ using boost::const_pointer_cast;
}
#endif
// This is needed since a module cannot be read on its own, only as
// part of a document class.
LayoutFile const & baseClass = LayoutFileList::get()[textclass.name()];
- typedef map<string, DocumentClass *> ModuleMap;
+ typedef map<string, DocumentClassPtr > ModuleMap;
static ModuleMap modules;
static bool init = true;
if (init) {
baseClass.load();
- DocumentClassBundle & bundle = DocumentClassBundle::get();
LyXModuleList::const_iterator const end = theModuleList.end();
LyXModuleList::const_iterator it = theModuleList.begin();
for (; it != end; ++it) {
if (!m.moduleCanBeAdded(module, &baseClass))
continue;
m.push_back(module);
- modules[module] = &bundle.makeDocumentClass(baseClass, m);
+ modules[module] = getDocumentClass(baseClass, m);
}
init = false;
}
continue;
if (findInsetLayoutWithoutModule(textclass, name, command))
continue;
- DocumentClass const * c = it->second;
+ DocumentClassConstPtr c = it->second;
Layout const * layout = findLayoutWithoutModule(*c, name, command);
InsetLayout const * insetlayout = layout ? 0 :
findInsetLayoutWithoutModule(*c, name, command);