r18928 - gnucash/trunk/src/gnc - Cutecash: Introduce separate base class GncInstance for QofInstance methods.

Christian Stimming cstim at code.gnucash.org
Thu Mar 18 18:06:42 EDT 2010


Author: cstim
Date: 2010-03-18 18:06:42 -0400 (Thu, 18 Mar 2010)
New Revision: 18928
Trac: http://svn.gnucash.org/trac/changeset/18928

Added:
   gnucash/trunk/src/gnc/GncInstance.hpp
   gnucash/trunk/src/gnc/Transaction.cpp
Modified:
   gnucash/trunk/src/gnc/Account.hpp
   gnucash/trunk/src/gnc/Book.hpp
   gnucash/trunk/src/gnc/CMakeLists.txt
   gnucash/trunk/src/gnc/Cmd.cpp
   gnucash/trunk/src/gnc/Commodity.hpp
   gnucash/trunk/src/gnc/QofEventWrapper.hpp
   gnucash/trunk/src/gnc/Split.cpp
   gnucash/trunk/src/gnc/Split.hpp
   gnucash/trunk/src/gnc/Transaction.hpp
Log:
Cutecash: Introduce separate base class GncInstance for QofInstance methods.

This is the base class for Account, Book, Commodity, Split, Transaction.

Modified: gnucash/trunk/src/gnc/Account.hpp
===================================================================
--- gnucash/trunk/src/gnc/Account.hpp	2010-03-17 21:28:51 UTC (rev 18927)
+++ gnucash/trunk/src/gnc/Account.hpp	2010-03-18 22:06:42 UTC (rev 18928)
@@ -31,7 +31,7 @@
 #include "engine/Account.h"
 }
 
-#include "gnc/WeakPointer.hpp"
+#include "gnc/GncInstance.hpp"
 #include "gnc/Commodity.hpp"
 
 #include <QString>
@@ -50,10 +50,10 @@
  * underlying gnucash ::Account object is still alive or has been
  * deleted.
  */
-class Account : public WeakPointer< ::Account >
+class Account : public GncInstance< ::Account >
 {
 public:
-    typedef WeakPointer< ::Account > base_class;
+    typedef GncInstance< ::Account > base_class;
     Account(element_type* ptr = 0)
             : base_class(ptr)
     { }

Modified: gnucash/trunk/src/gnc/Book.hpp
===================================================================
--- gnucash/trunk/src/gnc/Book.hpp	2010-03-17 21:28:51 UTC (rev 18927)
+++ gnucash/trunk/src/gnc/Book.hpp	2010-03-18 22:06:42 UTC (rev 18928)
@@ -31,7 +31,7 @@
 #include "engine/Account.h"
 }
 
-#include "gnc/WeakPointer.hpp"
+#include "gnc/GncInstance.hpp"
 
 namespace gnc
 {
@@ -44,13 +44,16 @@
  * underlying gnucash ::QofBook object is still alive or has been
  * deleted.
  */
-class Book : public WeakPointer< ::QofBook >
+class Book : public GncInstance< ::QofBook >
 {
 public:
-    typedef WeakPointer< ::QofBook > base_class;
+    typedef GncInstance< ::QofBook > base_class;
     Book(element_type* ptr = 0)
             : base_class(ptr)
     { }
+    Book(const base_class& x)
+            : base_class(x)
+    { }
 
     Account get_root_account();
 };

Modified: gnucash/trunk/src/gnc/CMakeLists.txt
===================================================================
--- gnucash/trunk/src/gnc/CMakeLists.txt	2010-03-17 21:28:51 UTC (rev 18927)
+++ gnucash/trunk/src/gnc/CMakeLists.txt	2010-03-18 22:06:42 UTC (rev 18928)
@@ -19,6 +19,7 @@
   Session.cpp
   Split.cpp
   SplitListModel.cpp
+  Transaction.cpp
   main.cpp
   mainwindow.cpp
   mainwindow-file.cpp

Modified: gnucash/trunk/src/gnc/Cmd.cpp
===================================================================
--- gnucash/trunk/src/gnc/Cmd.cpp	2010-03-17 21:28:51 UTC (rev 18927)
+++ gnucash/trunk/src/gnc/Cmd.cpp	2010-03-18 22:06:42 UTC (rev 18928)
@@ -207,10 +207,11 @@
 
     virtual void undo()
     {
-        m_target.reset(xaccMallocTransaction (m_book.get()));
+        m_target.reset(Transaction::newInstance(m_book));
         m_target.beginEdit();
         m_previousValue.copyTo(m_target);
         m_target.commitEdit();
+        // Could also use m_previousValue.createAsReal()
     }
 
 protected:

Modified: gnucash/trunk/src/gnc/Commodity.hpp
===================================================================
--- gnucash/trunk/src/gnc/Commodity.hpp	2010-03-17 21:28:51 UTC (rev 18927)
+++ gnucash/trunk/src/gnc/Commodity.hpp	2010-03-18 22:06:42 UTC (rev 18928)
@@ -31,17 +31,17 @@
 #include "engine/gnc-commodity.h"
 }
 
-#include "gnc/WeakPointer.hpp"
+#include "gnc/GncInstance.hpp"
 #include <QString>
 
 /** Wrapper around a gnucash gnc_commodity pointer */
 namespace gnc
 {
 
-class Commodity : public WeakPointer<gnc_commodity>
+class Commodity : public GncInstance<gnc_commodity>
 {
 public:
-    typedef WeakPointer<gnc_commodity> base_class;
+    typedef GncInstance<gnc_commodity> base_class;
     Commodity(element_type *ptr = 0)
             : base_class(ptr)
     {}

Added: gnucash/trunk/src/gnc/GncInstance.hpp
===================================================================
--- gnucash/trunk/src/gnc/GncInstance.hpp	                        (rev 0)
+++ gnucash/trunk/src/gnc/GncInstance.hpp	2010-03-18 22:06:42 UTC (rev 18928)
@@ -0,0 +1,70 @@
+/*
+ * GncInstance.hpp
+ * Copyright (C) 2010 Christian Stimming
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+#ifndef GNC_GNCINSTANCE_HPP
+#define GNC_GNCINSTANCE_HPP
+
+// gnucash includes
+#include "config.h"
+extern "C"
+{
+#include "qof.h"
+}
+
+#include "gnc/WeakPointer.hpp"
+#include <QString>
+
+namespace gnc
+{
+
+/** Wrapper around the "base class" QofInstance which already offers
+ * some common methods. We cannot name it QofInstance because those
+ * stupid C macros (like QOF_CHECK_TYPE) would always confuse our
+ * namespaced declaration with the C declaration. I hate macros!
+ *
+ * Unfortunately this object has no information about whether the
+ * underlying gnucash ::Transaction object is still alive or has been
+ * deleted.
+ */
+template<class T>
+class GncInstance : public WeakPointer< T >
+{
+public:
+    typedef WeakPointer< T > base_class;
+    GncInstance(T* ptr = 0)
+            : base_class(ptr)
+    { }
+
+    GncInstance< ::QofBook > getBook() const { return qof_instance_get_book (QOF_INSTANCE(base_class::get())); }
+    ::GUID getGUID() const { return qof_entity_get_guid(QOF_INSTANCE(base_class::get())); }
+
+    bool is_dirty() const { return qof_instance_get_dirty(QOF_INSTANCE(base_class::get())); }
+    void set_dirty() { return qof_instance_set_dirty(QOF_INSTANCE(base_class::get())); }
+    void mark_clean() { return qof_instance_mark_clean(QOF_INSTANCE(base_class::get())); }
+
+    //bool check_type(const char* type_id) { return (0 == g_strcmp0(type_id, QOF_INSTANCE(base_class::get())->e_type)); }
+    //Slots getSlots() const { return qof_instance_get_slots(QOF_INSTANCE(get())); }
+};
+
+} // END namespace gnc
+
+#endif

Modified: gnucash/trunk/src/gnc/QofEventWrapper.hpp
===================================================================
--- gnucash/trunk/src/gnc/QofEventWrapper.hpp	2010-03-17 21:28:51 UTC (rev 18927)
+++ gnucash/trunk/src/gnc/QofEventWrapper.hpp	2010-03-18 22:06:42 UTC (rev 18928)
@@ -40,7 +40,7 @@
 QString qofEventToString(QofEventId event_type);
 
 /** Retrieval of the Qof instance class name (for debugging) */
-inline const char* getQofType(QofInstance* obj)
+inline const char* getQofType( ::QofInstance* obj)
 {
     return obj->e_type;
 }
@@ -71,7 +71,7 @@
         qof_event_unregister_handler(m_handler_id);
     }
 
-    static void event_handler (QofInstance *entity,  QofEventId event_type,
+    static void event_handler (::QofInstance *entity,  QofEventId event_type,
                                gpointer user_data, gpointer event_data)
     {
         QofEventWrapper* wrapper = static_cast<QofEventWrapper *>(user_data);
@@ -79,7 +79,7 @@
     }
 
 private:
-    void private_event_handler (QofInstance *entity,  QofEventId event_type,
+    void private_event_handler (::QofInstance *entity,  QofEventId event_type,
                                 gpointer event_data)
     {
 //         qDebug() << "private_event_handler, id=" << qofEventToString(event_type)

Modified: gnucash/trunk/src/gnc/Split.cpp
===================================================================
--- gnucash/trunk/src/gnc/Split.cpp	2010-03-17 21:28:51 UTC (rev 18927)
+++ gnucash/trunk/src/gnc/Split.cpp	2010-03-18 22:06:42 UTC (rev 18928)
@@ -29,8 +29,6 @@
 namespace gnc
 {
 
-Book Split::getBook() const { return xaccSplitGetBook(get()); }
-
 Account Split::getAccount() const { return xaccSplitGetAccount(get()); }
 void Split::setAccount(Account& acc) { xaccSplitSetAccount(get(), acc.get()); }
 void Split::setAccount(::Account* acc) { xaccSplitSetAccount(get(), acc); }
@@ -50,6 +48,22 @@
         , value(s.getValue())
 {}
 
+TmpSplit::TmpSplit(::Account* account)
+{
+    clear(account);
+}
+
+void TmpSplit::clear(::Account* account)
+{
+    account = account;
+    parent = NULL;
+    memo.clear();
+    action.clear();
+    reconcile = '\0';
+    amount = Numeric::zero();
+    value = Numeric::zero();
+}
+
 void TmpSplit::copyInto(Transaction& t)
 {
     Split s(xaccMallocSplit(t.getBook().get()));

Modified: gnucash/trunk/src/gnc/Split.hpp
===================================================================
--- gnucash/trunk/src/gnc/Split.hpp	2010-03-17 21:28:51 UTC (rev 18927)
+++ gnucash/trunk/src/gnc/Split.hpp	2010-03-18 22:06:42 UTC (rev 18928)
@@ -34,7 +34,7 @@
 #include <QString>
 #include <QList>
 
-#include "gnc/WeakPointer.hpp"
+#include "gnc/GncInstance.hpp"
 #include "gnc/Numeric.hpp"
 
 namespace gnc
@@ -54,15 +54,14 @@
  * underlying gnucash ::Split object is still alive or has been
  * deleted.
  */
-class Split : public WeakPointer< ::Split >
+class Split : public GncInstance< ::Split >
 {
 public:
-    typedef WeakPointer< ::Split > base_class;
+    typedef GncInstance< ::Split > base_class;
     Split(element_type* ptr = 0)
             : base_class(ptr)
     { }
 
-    Book getBook() const;
     Account getAccount() const;
     void setAccount(Account& acc);
     void setAccount(::Account* acc);
@@ -116,9 +115,33 @@
 {
 public:
     TmpSplit(const Split& s, const TmpTransaction* parent_trans);
-    TmpSplit()
-    {}
+    TmpSplit(::Account* account = NULL);
+
+    void clear(::Account* account = NULL);
     void copyInto(Transaction& t);
+
+    ::Account* getAccount() const { return account; }
+    void setAccount(::Account* v) { account = v;}
+
+    const TmpTransaction* getParent() const { return parent; }
+    void setParent(const TmpTransaction* v) { parent = v; }
+
+    QString getMemo() const { return memo; }
+    void setMemo(const QString& v) { memo = v; }
+
+    QString getAction() const { return action; }
+    void setAction(const QString& v) { action = v; }
+
+    char getReconcile() const { return reconcile; }
+    void setReconcile(char v) { reconcile = v; }
+
+    Numeric getAmount() const { return amount; }
+    void setAmount(const Numeric& v) { amount = v; }
+
+    Numeric getValue() const { return value; }
+    void setValue(const Numeric& v) { value = v; }
+
+private:
     ::Account* account;
     const TmpTransaction* parent;
     QString memo;

Added: gnucash/trunk/src/gnc/Transaction.cpp
===================================================================
--- gnucash/trunk/src/gnc/Transaction.cpp	                        (rev 0)
+++ gnucash/trunk/src/gnc/Transaction.cpp	2010-03-18 22:06:42 UTC (rev 18928)
@@ -0,0 +1,112 @@
+/*
+ * Transaction.cpp
+ * Copyright (C) 2010 Christian Stimming
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+#include "Transaction.hpp"
+#include "gnc/Split.hpp"
+
+namespace gnc
+{
+
+Split Transaction::findSplitByAccount(const Account& acc) const
+{
+    return xaccTransFindSplitByAccount(get(), acc.get());
+}
+Split Transaction::getSplit(int i) const
+{
+    return xaccTransGetSplit(get(), i);
+}
+void Transaction::appendSplit(Split& split)
+{
+    xaccSplitSetParent(split.get(), get());
+}
+int Transaction::getSplitIndex(const Split& split) const
+{
+    return xaccTransGetSplitIndex(get(), split.get());
+}
+Transaction::element_type* Transaction::newInstance(const Book& b)
+{
+    return xaccMallocTransaction (b.get());
+}
+
+// ////////////////////////////////////////////////////////////
+
+TmpTransaction::TmpTransaction()
+{
+    clear();
+}
+TmpTransaction::TmpTransaction(const Transaction& t)
+        : num(t.getNum())
+        , description(t.getDescription())
+        , notes(t.getNotes())
+        , commodity(t.getCurrency())
+        , datePosted(t.getDatePosted())
+        , dateTimeEntered(t.getDateEntered())
+{
+    SplitQList slist = Split::fromGList(t.getSplitList());
+    Q_FOREACH(Split s, slist)
+    {
+        splits.push_back(TmpSplit(s, this));
+    }
+}
+void TmpTransaction::clear()
+{
+    num.clear();
+    description.clear();
+    notes.clear();
+    commodity.reset();
+    datePosted = QDate();
+    dateTimeEntered = QDateTime();
+    splits.clear();
+}
+void TmpTransaction::copyTo(Transaction& t) const
+{
+    t.setNum(num);
+    t.setDescription(description);
+    if (!notes.isEmpty())
+        t.setNotes(notes);
+    t.setCurrency(commodity);
+    t.setDatePosted(datePosted);
+    t.setDateEntered(dateTimeEntered);
+    Q_FOREACH(TmpSplit s, splits)
+    {
+        s.copyInto(t);
+    }
+}
+void TmpTransaction::createAsReal() const
+{
+    Q_ASSERT (!splits.isEmpty());
+    Account acc(splits.front().getAccount());
+    Q_ASSERT (acc);
+    Book book(acc.getBook());
+    Q_ASSERT (book);
+    Transaction trans(Transaction::newInstance(book));
+    trans.beginEdit();
+    copyTo(trans);
+    trans.commitEdit();
+}
+void TmpTransaction::push_back(const TmpSplit& s)
+{
+    splits.push_back(s);
+    splits.back().setParent(this);
+}
+
+} // END namespace gnc

Modified: gnucash/trunk/src/gnc/Transaction.hpp
===================================================================
--- gnucash/trunk/src/gnc/Transaction.hpp	2010-03-17 21:28:51 UTC (rev 18927)
+++ gnucash/trunk/src/gnc/Transaction.hpp	2010-03-18 22:06:42 UTC (rev 18928)
@@ -35,7 +35,7 @@
 #include "gnc/Book.hpp"
 #include "gnc/Commodity.hpp"
 #include "gnc/Numeric.hpp"
-#include "gnc/WeakPointer.hpp"
+#include "gnc/GncInstance.hpp"
 
 #include <QString>
 #include <QList>
@@ -43,6 +43,9 @@
 namespace gnc
 {
 
+class Split;
+class TmpSplit;
+
 /** Wrapper around a gnucash ::Transaction pointer with C++ methods for
  * easier setter and getter access.
  *
@@ -50,16 +53,14 @@
  * underlying gnucash ::Transaction object is still alive or has been
  * deleted.
  */
-class Transaction : public WeakPointer< ::Transaction >
+class Transaction : public GncInstance< ::Transaction >
 {
 public:
-    typedef WeakPointer< ::Transaction > base_class;
+    typedef GncInstance< ::Transaction > base_class;
     Transaction(element_type* ptr = 0)
             : base_class(ptr)
     { }
 
-    Book getBook() const { return xaccTransGetBook(get()); }
-
     void beginEdit() { xaccTransBeginEdit(get()); }
     void commitEdit() { xaccTransCommitEdit(get()); }
     void rollbackEdit() { xaccTransRollbackEdit(get()); }
@@ -76,10 +77,10 @@
     void setNotes(const QString& v) { xaccTransSetNotes(get(), v.toUtf8()); }
 
     int countSplits() const { return xaccTransCountSplits(get()); }
-    Split findSplitByAccount(const Account& acc) const { return xaccTransFindSplitByAccount(get(), acc.get()); }
-    void appendSplit(Split& split) { xaccSplitSetParent(split.get(), get()); }
-    Split getSplit(int i) const { return xaccTransGetSplit(get(), i); }
-    int getSplitIndex(const Split& split) const { return xaccTransGetSplitIndex(get(), split.get()); }
+    Split findSplitByAccount(const Account& acc) const;
+    void appendSplit(Split& split);
+    Split getSplit(int i) const;
+    int getSplitIndex(const Split& split) const;
     ::SplitList* getSplitList() const { return xaccTransGetSplitList(get()); }
 
     Commodity getCurrency() const { return xaccTransGetCurrency(get()); }
@@ -94,43 +95,45 @@
     QDate getDatePosted() const { return toQDate(xaccTransGetDatePostedGDate(get())); }
     QDateTime getDateEntered() const { return toQDateTime(xaccTransRetDateEnteredTS(get())); }
 
+    static element_type* newInstance(const Book& b);
 };
 
 class TmpTransaction
 {
 public:
-    TmpTransaction(const Transaction& t)
-            : num(t.getNum())
-            , description(t.getDescription())
-            , notes(t.getNotes())
-            , commodity(t.getCurrency())
-            , datePosted(t.getDatePosted())
-            , dateTimeEntered(t.getDateEntered())
-    {
-        SplitQList slist = Split::fromGList(t.getSplitList());
-        Q_FOREACH(Split s, slist)
-        {
-            splits.push_back(TmpSplit(s, this));
-        }
-    }
-    void copyTo(Transaction& t)
-    {
-        t.setNum(num);
-        t.setDescription(description);
-        if (!notes.isEmpty())
-            t.setNotes(notes);
-        t.setCurrency(commodity);
-        t.setDatePosted(datePosted);
-        t.setDateEntered(dateTimeEntered);
-        Q_FOREACH(TmpSplit s, splits)
-        {
-            s.copyInto(t);
-        }
-    }
+    typedef QList<TmpSplit> TmpSplitQList;
+
+    TmpTransaction();
+    TmpTransaction(const Transaction& t);
+
+    void clear();
+    void copyTo(Transaction& t) const;
+    void createAsReal() const;
+
+    QString getNum() const { return num; }
+    void setNum(const QString& v) { num = v; }
+
+    QString getDescription() const { return description; }
+    void setDescription(const QString& v) { description = v; }
+
+    void push_back(const TmpSplit& s);
+    const TmpSplitQList& getSplits() const { return splits; }
+    TmpSplitQList& getSplits() { return splits; }
+
+    Commodity getCommodity() const { return commodity; }
+    void setCommodity(const Commodity& v) { commodity = v; }
+
+    QDate getDatePosted() const { return datePosted; }
+    void setDatePosted(const QDate& v) { datePosted = v; }
+
+    QDateTime getDateEntered() const { return dateTimeEntered; }
+    void setDateEntered(const QDateTime& v) { dateTimeEntered = v; }
+
+private:
     QString num;
     QString description;
     QString notes;
-    QList<TmpSplit> splits;
+    TmpSplitQList splits;
     Commodity commodity;
     QDate datePosted;
     QDateTime dateTimeEntered;



More information about the gnucash-changes mailing list