Commit 9773c153 authored by Albert Astals Cid's avatar Albert Astals Cid

New Object API

Implement the move operators and copy construtor

Almost all the init() functions are gone and we just have simple
constructors now

Also made free() public since you're not supposed to call it anymore,
unless you're being evil and malloc'ing Objects like Array/Dict/XRef

This has a huge reaction chain, most importantly we
don't get objects by passing a pointer Object parameter, we
just get the object as a return value, which is a much clearer API
 -  aobj->copy(&obj);
 +  obj = aobj->copy();
before I was never sure what was being copied into what

Comes with a huge diff, I probably made some mistake in the porting
since there was lots of copy & paste involved
parent 3c29ded4
......@@ -85,10 +85,8 @@ document_private::document_private(byte_array *file_data,
, raw_doc_data_length(0)
, is_locked(false)
{
Object obj;
obj.initNull();
file_data->swap(doc_data);
MemStream *memstr = new MemStream(&doc_data[0], 0, doc_data.size(), &obj);
MemStream *memstr = new MemStream(&doc_data[0], 0, doc_data.size(), Object(objNull));
GooString goo_owner_password(owner_password.c_str());
GooString goo_user_password(user_password.c_str());
doc = new PDFDoc(memstr, &goo_owner_password, &goo_user_password);
......@@ -103,9 +101,7 @@ document_private::document_private(const char *file_data, int file_data_length,
, raw_doc_data_length(file_data_length)
, is_locked(false)
{
Object obj;
obj.initNull();
MemStream *memstr = new MemStream(const_cast<char *>(raw_doc_data), 0, raw_doc_data_length, &obj);
MemStream *memstr = new MemStream(const_cast<char *>(raw_doc_data), 0, raw_doc_data_length, Object(objNull));
GooString goo_owner_password(owner_password.c_str());
GooString goo_user_password(user_password.c_str());
doc = new PDFDoc(memstr, &goo_owner_password, &goo_user_password);
......@@ -314,9 +310,8 @@ std::vector<std::string> document::info_keys() const
return std::vector<std::string>();
}
Object info;
if (!d->doc->getDocInfo(&info)->isDict()) {
info.free();
Object info = d->doc->getDocInfo();
if (!info.isDict()) {
return std::vector<std::string>();
}
......@@ -326,7 +321,6 @@ std::vector<std::string> document::info_keys() const
keys[i] = std::string(info_dict->getKey(i));
}
info.free();
return keys;
}
......
......@@ -172,11 +172,10 @@ ustring page::label() const
page_transition* page::transition() const
{
if (!d->transition) {
Object o;
if (d->page->getTrans(&o)->isDict()) {
Object o = d->page->getTrans();
if (o.isDict()) {
d->transition = new page_transition(&o);
}
o.free();
}
return d->transition;
}
......
......@@ -431,9 +431,8 @@ find_annot_movie_for_action (PopplerDocument *document,
if (link->hasAnnotRef ()) {
Ref *ref = link->getAnnotRef ();
xref->fetch (ref->num, ref->gen, &annotObj);
annotObj = xref->fetch (ref->num, ref->gen);
} else if (link->hasAnnotTitle ()) {
Object annots;
GooString *title = link->getAnnotTitle ();
int i;
......@@ -441,39 +440,36 @@ find_annot_movie_for_action (PopplerDocument *document,
Page *p = document->doc->getPage (i);
if (!p) continue;
if (p->getAnnots (&annots)->isArray ()) {
Object annots = p->getAnnotsObject ();
if (annots.isArray ()) {
int j;
GBool found = gFalse;
for (j = 0; j < annots.arrayGetLength () && !found; ++j) {
if (annots.arrayGet(j, &annotObj)->isDict()) {
Object obj1;
if (!annotObj.dictLookup ("Subtype", &obj1)->isName ("Movie")) {
obj1.free ();
annotObj = annots.arrayGet(j);
if (annotObj.isDict()) {
Object obj1 = annotObj.dictLookup ("Subtype");
if (!obj1.isName ("Movie")) {
continue;
}
obj1.free ();
if (annotObj.dictLookup ("T", &obj1)->isString()) {
obj1 = annotObj.dictLookup ("T");
if (obj1.isString()) {
GooString *t = obj1.getString ();
if (title->cmp(t) == 0)
found = gTrue;
}
obj1.free ();
}
if (!found)
annotObj.free ();
annotObj.setToNull ();
}
if (found) {
annots.free ();
break;
} else {
annotObj.free ();
annotObj.setToNull ();
}
}
annots.free ();
}
}
......@@ -486,7 +482,6 @@ find_annot_movie_for_action (PopplerDocument *document,
annot = NULL;
}
}
annotObj.free ();
return annot;
}
......
......@@ -67,7 +67,6 @@ poppler_attachment_dispose (GObject *obj)
if (priv->obj_stream)
{
priv->obj_stream->free();
delete priv->obj_stream;
priv->obj_stream = NULL;
}
......@@ -127,8 +126,7 @@ _poppler_attachment_new (FileSpec *emb_file)
if (embFile->checksum () && embFile->checksum ()->getLength () > 0)
attachment->checksum = g_string_new_len (embFile->checksum ()->getCString (),
embFile->checksum ()->getLength ());
priv->obj_stream = new Object();
priv->obj_stream->initStream(embFile->stream());
priv->obj_stream = new Object(embFile->stream());
// Copy the stream
embFile->stream()->incRef();
......
......@@ -230,7 +230,6 @@ poppler_document_new_from_data (char *data,
const char *password,
GError **error)
{
Object obj;
PDFDoc *newDoc;
MemStream *str;
GooString *password_g;
......@@ -240,8 +239,7 @@ poppler_document_new_from_data (char *data,
}
// create stream
obj.initNull();
str = new MemStream(data, 0, length, &obj);
str = new MemStream(data, 0, length, Object(objNull));
password_g = poppler_password_to_latin1(password);
newDoc = new PDFDoc(str, password_g, password_g);
......@@ -282,7 +280,6 @@ poppler_document_new_from_stream (GInputStream *stream,
GCancellable *cancellable,
GError **error)
{
Object obj;
PDFDoc *newDoc;
BaseStream *str;
GooString *password_g;
......@@ -300,12 +297,11 @@ poppler_document_new_from_stream (GInputStream *stream,
return NULL;
}
obj.initNull();
if (stream_is_memory_buffer_or_local_file(stream)) {
str = new PopplerInputStream(stream, cancellable, 0, gFalse, 0, &obj);
str = new PopplerInputStream(stream, cancellable, 0, gFalse, 0, Object(objNull));
} else {
CachedFile *cachedFile = new CachedFile(new PopplerCachedFileLoader(stream, cancellable, length), new GooString());
str = new CachedFileStream(cachedFile, 0, gFalse, cachedFile->getLength(), &obj);
str = new CachedFileStream(cachedFile, 0, gFalse, cachedFile->getLength(), Object(objNull));
}
password_g = poppler_password_to_latin1(password);
......@@ -2410,32 +2406,26 @@ get_optional_content_rbgroups (OCGs *ocg)
int i, j;
for (i = 0; i < rb->getLength (); ++i) {
Object obj;
Array *rb_array;
GList *group = NULL;
rb->get (i, &obj);
Object obj = rb->get (i);
if (!obj.isArray ()) {
obj.free ();
continue;
}
rb_array = obj.getArray ();
for (j = 0; j < rb_array->getLength (); ++j) {
Object ref;
OptionalContentGroup *oc;
rb_array->getNF (j, &ref);
Object ref = rb_array->getNF (j);
if (!ref.isRef ()) {
ref.free ();
continue;
}
oc = ocg->findOcgByRef (ref.getRef ());
group = g_list_prepend (group, oc);
ref.free ();
}
obj.free ();
groups = g_list_prepend (groups, group);
}
......@@ -2468,14 +2458,10 @@ get_optional_content_items_sorted (OCGs *ocg, Layer *parent, Array *order)
int i;
for (i = 0; i < order->getLength (); ++i) {
Object orderItem;
order->get (i, &orderItem);
Object orderItem = order->get (i);
if (orderItem.isDict ()) {
Object ref;
order->getNF (i, &ref);
Object ref = order->getNF (i);
if (ref.isRef ()) {
OptionalContentGroup *oc = ocg->findOcgByRef (ref.getRef ());
Layer *layer = layer_new (oc);
......@@ -2483,7 +2469,6 @@ get_optional_content_items_sorted (OCGs *ocg, Layer *parent, Array *order)
items = g_list_prepend (items, layer);
last_item = layer;
}
ref.free ();
} else if (orderItem.isArray () && orderItem.arrayGetLength () > 0) {
if (!last_item) {
last_item = layer_new (NULL);
......@@ -2494,7 +2479,6 @@ get_optional_content_items_sorted (OCGs *ocg, Layer *parent, Array *order)
} else if (orderItem.isString ()) {
last_item->label = _poppler_goo_string_to_utf8 (orderItem.getString ());
}
orderItem.free ();
}
return g_list_reverse (items);
......
......@@ -21,8 +21,8 @@
#include "poppler-input-stream.h"
PopplerInputStream::PopplerInputStream(GInputStream *inputStreamA, GCancellable *cancellableA,
Goffset startA, GBool limitedA, Goffset lengthA, Object *dictA)
: BaseStream(dictA, lengthA)
Goffset startA, GBool limitedA, Goffset lengthA, Object &&dictA)
: BaseStream(std::move(dictA), lengthA)
{
inputStream = (GInputStream *)g_object_ref(inputStreamA);
cancellable = cancellableA ? (GCancellable *)g_object_ref(cancellableA) : NULL;
......@@ -44,13 +44,13 @@ PopplerInputStream::~PopplerInputStream()
}
BaseStream *PopplerInputStream::copy() {
return new PopplerInputStream(inputStream, cancellable, start, limited, length, &dict);
return new PopplerInputStream(inputStream, cancellable, start, limited, length, dict.copy());
}
Stream *PopplerInputStream::makeSubStream(Goffset startA, GBool limitedA,
Goffset lengthA, Object *dictA)
Goffset lengthA, Object &&dictA)
{
return new PopplerInputStream(inputStream, cancellable, startA, limitedA, lengthA, dictA);
return new PopplerInputStream(inputStream, cancellable, startA, limitedA, lengthA, std::move(dictA));
}
void PopplerInputStream::reset()
......
......@@ -31,11 +31,11 @@ class PopplerInputStream: public BaseStream {
public:
PopplerInputStream(GInputStream *inputStream, GCancellable *cancellableA,
Goffset startA, GBool limitedA, Goffset lengthA, Object *dictA);
Goffset startA, GBool limitedA, Goffset lengthA, Object &&dictA);
~PopplerInputStream();
BaseStream *copy() override;
Stream *makeSubStream(Goffset start, GBool limited,
Goffset lengthA, Object *dictA) override;
Goffset lengthA, Object &&dictA) override;
StreamKind getKind() override { return strWeird; }
void reset() override;
void close() override;
......
......@@ -84,11 +84,8 @@ _poppler_movie_new (Movie *poppler_movie)
movie->filename = g_strdup (poppler_movie->getFileName()->getCString());
if (poppler_movie->getShowPoster()) {
Object tmp;
poppler_movie->getPoster(&tmp);
Object tmp = poppler_movie->getPoster();
movie->need_poster = (!tmp.isRef() && !tmp.isStream());
tmp.free();
}
movie->show_controls = poppler_movie->getActivationParameters()->showControls;
......
......@@ -184,12 +184,11 @@ poppler_page_get_transition (PopplerPage *page)
{
PageTransition *trans;
PopplerPageTransition *transition;
Object obj;
g_return_val_if_fail (POPPLER_IS_PAGE (page), NULL);
trans = new PageTransition (page->page->getTrans (&obj));
obj.free ();
Object obj = page->page->getTrans ();
trans = new PageTransition (&obj);
if (!trans->isOk ()) {
delete trans;
......@@ -588,7 +587,6 @@ poppler_page_get_thumbnail_size (PopplerPage *page,
int *width,
int *height)
{
Object thumb;
Dict *dict;
gboolean retval = FALSE;
......@@ -596,10 +594,9 @@ poppler_page_get_thumbnail_size (PopplerPage *page,
g_return_val_if_fail (width != NULL, FALSE);
g_return_val_if_fail (height != NULL, FALSE);
page->page->getThumb (&thumb);
Object thumb = page->page->getThumb ();
if (!thumb.isStream ())
{
thumb.free ();
return FALSE;
}
......@@ -611,8 +608,6 @@ poppler_page_get_thumbnail_size (PopplerPage *page,
dict->lookupInt ("Height", "H", height))
retval = TRUE;
thumb.free ();
return retval;
}
......
......@@ -1213,9 +1213,8 @@ convert_border_style (Object *object, PopplerStructureBorderStyle *values)
g_assert (object->arrayGetLength () == 4);
for (guint i = 0; i < 4; i++)
{
Object item;
values[i] = name_to_enum<PopplerStructureBorderStyle> (object->arrayGet (i, &item));
item.free ();
Object item = object->arrayGet (i);
values[i] = name_to_enum<PopplerStructureBorderStyle> (&item);
}
}
else
......@@ -1261,9 +1260,7 @@ convert_doubles_array (Object *object, gdouble **values, guint *n_values)
for (guint i = 0; i < *n_values; i++)
{
Object item;
doubles[i] = object->arrayGet (i, &item)->getNum ();
item.free ();
doubles[i] = object->arrayGet (i).getNum ();
}
}
......@@ -1273,16 +1270,9 @@ convert_color (Object *object, PopplerColor *color)
g_assert (color != NULL);
g_assert (object->isArray () && object->arrayGetLength () != 3);
Object item;
color->red = object->arrayGet (0, &item)->getNum () * 65535;
item.free ();
color->green = object->arrayGet (1, &item)->getNum () * 65535;
item.free ();
color->blue = object->arrayGet (2, &item)->getNum () * 65535;
item.free ();
color->red = object->arrayGet (0).getNum () * 65535;
color->green = object->arrayGet (1).getNum () * 65535;
color->blue = object->arrayGet (2).getNum () * 65535;
}
/**
......@@ -1377,9 +1367,8 @@ poppler_structure_element_get_border_color (PopplerStructureElement *poppler_str
// One color per side.
for (guint i = 0; i < 4; i++)
{
Object item;
convert_color (value->arrayGet (i, &item), &colors[i]);
item.free ();
Object item = value->arrayGet (i);
convert_color (&item, &colors[i]);
}
}
else
......@@ -1403,9 +1392,7 @@ convert_double_or_4_doubles (Object *object, gdouble *value)
g_assert (object->arrayGetLength () == 4);
for (guint i = 0; i < 4; i++)
{
Object item;
value[i] = object->arrayGet (i, &item)->getNum ();
item.free ();
value[i] = object->arrayGet (i).getNum ();
}
}
else
......@@ -2175,16 +2162,14 @@ poppler_structure_element_get_table_headers (PopplerStructureElement *poppler_st
for (guint i = 0; i < n_values; i++)
{
Object item;
Object item = value->arrayGet (i);
if (value->arrayGet (i, &item)->isString ())
if (item.isString ())
result[i] = _poppler_goo_string_to_utf8 (item.getString ());
else if (item.isName ())
result[i] = g_strdup (item.getName ());
else
g_assert_not_reached ();
item.free ();
}
return result;
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -21,7 +21,7 @@
// Copyright (C) 2008 Hugo Mercier <hmercier31@gmail.com>
// Copyright (C) 2008 Pino Toscano <pino@kde.org>
// Copyright (C) 2008 Tomas Are Haavet <tomasare@gmail.com>
// Copyright (C) 2009-2011, 2013, 2016 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2009-2011, 2013, 2016, 2017 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2012, 2013 Fabio D'Urso <fabiodurso@hotmail.it>
// Copyright (C) 2012, 2015 Tobias Koenig <tokoe@kdab.com>
// Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
......@@ -239,7 +239,7 @@ public:
virtual double *getDash() const { return dash; }
virtual AnnotBorderStyle getStyle() const { return style; }
virtual void writeToObject(XRef *xref, Object *obj1) const = 0;
virtual Object writeToObject(XRef *xref) const = 0;
protected:
AnnotBorder();
......@@ -271,7 +271,7 @@ public:
private:
AnnotBorderType getType() const override { return typeArray; }
void writeToObject(XRef *xref, Object *obj1) const override;
Object writeToObject(XRef *xref) const override;
double horizontalCorner; // (Default 0)
double verticalCorner; // (Default 0)
......@@ -290,7 +290,7 @@ public:
private:
AnnotBorderType getType() const override { return typeBS; }
void writeToObject(XRef *xref, Object *obj1) const override;
Object writeToObject(XRef *xref) const override;
const char *getStyleName() const;
......@@ -324,7 +324,7 @@ public:
AnnotColorSpace getSpace() const { return (AnnotColorSpace) length; }
const double *getValues() const { return values; }
void writeToObject(XRef *xref, Object *dest) const;
Object writeToObject(XRef *xref) const;
private:
......@@ -385,7 +385,7 @@ public:
~AnnotAppearance();
// State is ignored if no subdictionary is present
void getAppearanceStream(AnnotAppearanceType type, const char *state, Object *dest);
Object getAppearanceStream(AnnotAppearanceType type, const char *state);
// Access keys in normal appearance subdictionary (N)
GooString * getStateKey(int i);
......@@ -564,7 +564,7 @@ public:
virtual void draw(Gfx *gfx, GBool printing);
// Get the resource dict of the appearance stream
virtual Object *getAppearanceResDict(Object *dest);
virtual Object getAppearanceResDict();
GBool match(Ref *refA)
{ return ref.num == refA->num && ref.gen == refA->gen; }
......@@ -638,15 +638,15 @@ protected:
double *width, double widthLimit, int *charCount,
GBool noReencode);
void writeString(GooString *str, GooString *appearBuf);
void createForm(double *bbox, GBool transparencyGroup, Object *resDict, Object *aStream);
void createResourcesDict(const char *formName, Object *formStream, const char *stateName,
double opacity, const char *blendMode, Object *resDict);
Object createForm(double *bbox, GBool transparencyGroup, Dict *resDict);
Dict *createResourcesDict(const char *formName, Object &&formStream, const char *stateName,
double opacity, const char *blendMode);
GBool isVisible(GBool printing);
int getRotation() const;
// Updates the field key of the annotation dictionary
// and sets M to the current time
void update(const char *key, Object *value);
void update(const char *key, Object &&value);
// Delete appearance streams and reset appearance state
void invalidateAppearance();
......@@ -698,7 +698,6 @@ public:
AnnotPopup(PDFDoc *docA, Dict *dict, Object *obj);
~AnnotPopup();
Object *getParent(Object *obj) { return parent.fetch (xref, obj); }
Object *getParentNF() { return &parent; }
void setParent(Object *parentA);
void setParent(Annot *parentA);
......@@ -923,7 +922,7 @@ public:
~AnnotFreeText();
void draw(Gfx *gfx, GBool printing) override;
Object *getAppearanceResDict(Object *dest) override;
Object getAppearanceResDict() override;
void setContents(GooString *new_content) override;
void setAppearanceString(GooString *new_string);
......@@ -987,7 +986,7 @@ public:
~AnnotLine();
void draw(Gfx *gfx, GBool printing) override;
Object *getAppearanceResDict(Object *dest) override;
Object getAppearanceResDict() override;
void setContents(GooString *new_content) override;
void setVertices(double x1, double y1, double x2, double y2);
......
......@@ -65,14 +65,13 @@ Array::~Array() {
#endif
}
Object *Array::copy(XRef *xrefA, Object *obj) {
Object Array::copy(XRef *xrefA) {
arrayLocker();
obj->initArray(xrefA);
Array *a = new Array(xrefA);
for (int i = 0; i < length; ++i) {
Object obj1;
obj->arrayAdd(elems[i].copy(&obj1));
a->add(elems[i].copy());
}
return obj;
return Object(a);
}
int Array::incRef() {
......@@ -87,7 +86,7 @@ int Array::decRef() {
return ref;
}
void Array::add(Object *elem) {
void Array::add(Object &&elem) {
arrayLocker();
if (length == size) {
if (length == 0) {
......@@ -97,8 +96,8 @@ void Array::add(Object *elem) {
}
elems = (Object *)greallocn(elems, size, sizeof(Object));
}
elems[length].initNullNoFree();
elem->shallowCopy(&elems[length]);
elems[length].initNullAfterMalloc();
elems[length] = std::move(elem);
++length;
}
......@@ -115,39 +114,36 @@ void Array::remove(int i) {
memmove( elems + i, elems + i + 1, sizeof(elems[0]) * (length - i) );
}
Object *Array::get(int i, Object *obj, int recursion) {
Object Array::get(int i, int recursion) {
if (i < 0 || i >= length) {
#ifdef DEBUG_MEM
abort();
#else
return obj->initNull();
return Object(objNull);
#endif
}
return elems[i].fetch(xref, obj, recursion);
return elems[i].fetch(xref, recursion);
}
Object *Array::getNF(int i, Object *obj) {
Object Array::getNF(int i) {
if (i < 0 || i >= length) {
#ifdef DEBUG_MEM
abort();
#else
return obj->initNull();
return Object(objNull);
#endif
}
return elems[i].copy(obj);
return elems[i].copy();
}
GBool Array::getString(int i, GooString *string)
{
Object obj;
if (getNF(i, &obj)->isString()) {
Object obj = getNF(i);
if (obj.isString()) {
string->clear();
string->append(obj.getString());
obj.free();
return gTrue;
} else {
obj.free();
return gFalse;
}
}
......@@ -57,18 +57,18 @@ public:
int getLength() { return length; }
// Copy array with new xref
Object *copy(XRef *xrefA, Object *obj);
Object copy(XRef *xrefA);
// Add an element
// elem becomes a dead object after this call
void add(Object *elem);
void add(Object &&elem);
// Remove an element by position
void remove(int i);
// Accessors.
Object *get(int i, Object *obj, int resursion = 0);
Object *getNF(int i, Object *obj);
Object get(int i, int resursion = 0);
Object getNF(int i);
GBool getString(int i, GooString *string);
private:
......
......@@ -14,7 +14,7 @@
// under GPL version 2 or later
//
// Copyright (C) 2008 Koji Otani <sho@bbr.jp>
// Copyright (C) 2008, 2009 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2008, 2009, 2017 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2013 Fabio D'Urso <fabiodurso@hotmail.it>
//
// To see a description of the changes please see the Changelog file that
......@@ -116,15 +116,11 @@ CMap *CMap::parse(CMapCache *cache, GooString *collectionA,
}
CMap *CMap::parse(CMapCache *cache, GooString *collectionA, Stream *str) {
Object obj1;
CMap *cMap;
cMap = new CMap(collectionA->copy(), NULL);
if (!str->getDict()->lookup("UseCMap", &obj1)->isNull()) {
CMap *cMap = new CMap(collectionA->copy(), NULL);
Object obj1 = str->getDict()->lookup("UseCMap");
if (!obj1.isNull()) {
cMap->useCMap(cache, &obj1);
}
obj1.free();
str->reset();
cMap->parse2(cache, &getCharFromStream, str);
......
......@@ -17,7 +17,7 @@
// Copyright (C) 2005-2007 Jeff Muizelaar <jeff@infidigm.net>
// Copyright (C) 2005, 2006 Kristian Høgsberg <krh@redhat.com>
// Copyright (C) 2005 Martin Kretzschmar <martink@gnome.org>
// Copyright (C) 2005, 2009, 2012, 2013, 2015 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2005, 2009, 2012, 2013, 2015, 2017 Albert Astals Cid <aacid@kde.org>