After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 645852 - Uncopiable structs/structs with explicit copy constructor
Uncopiable structs/structs with explicit copy constructor
Status: RESOLVED DUPLICATE of bug 733110
Product: vala
Classification: Core
Component: Structs
unspecified
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks: 684925
 
 
Reported: 2011-03-27 19:01 UTC by Maciej (Matthew) Piechotka
Modified: 2014-07-26 08:03 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Maciej (Matthew) Piechotka 2011-03-27 19:01:23 UTC
Sometimes it would be nice to have the structs that are not copiable or have copy contructor and have destructor. Using it instead of class allows the gcc in RAII to optimize members to registers.

For example (I know it is oversimplification):

  struct DatabaseTransaction {
    public DatabaseTransaction (Connection conn) {
      this.conn = conn;
      conn.begin ();
    }

    public string query (string q) {
      return conn.send (q);
    }

    ~DatabaseTransaction () {
      conn.commit ();
    }

    private Connection conn;
  }
Comment 1 Luca Bruno 2011-05-11 09:52:23 UTC
You can specify copy_function to CCode and then add your copy() method like this:

[CCode (copy_function = "database_transaction_copy")]
struct DatabaseTransaction {
  public DatabaseTransaction copy () {
  }
}

But maybe I have misunderstood your request.
Comment 2 Maciej (Matthew) Piechotka 2011-05-14 14:40:02 UTC
Sorry for late response - I'm during my exam session.

I've tried it before posting bug and it doesn't work:

% cat test.vala
[CCode (copy_function = "database_transaction_copy")]
struct DatabaseTransaction {
  public DatabaseTransaction () {
    depth = 0;
  }

  public DatabaseTransaction copy () {
    stdout.printf ("copy\n");
    DatabaseTransaction tran = this;
    tran.depth += 1;
    return tran;
  }

  public int depth;
}

int main () {
  DatabaseTransaction c1 = DatabaseTransaction ();
  DatabaseTransaction c2 = c1;
  stdout.printf ("%d\n", c2.depth);
  return 0;
}
%  valac test.vala
test.vala:7.3-7.33: warning: method `DatabaseTransaction.copy' never used
  public DatabaseTransaction copy () {
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Compilation succeeded - 1 warning(s)
% ./test
0
Comment 3 Maciej (Matthew) Piechotka 2013-02-09 11:08:22 UTC
(In reply to comment #2)
>     DatabaseTransaction tran = this;

This line is incorrect but it should result in infinite recursion.
Comment 4 Maciej (Matthew) Piechotka 2014-07-26 08:03:42 UTC

*** This bug has been marked as a duplicate of bug 733110 ***