#246 Smart pointer const overload support

closed-accepted
None
5
2010-10-04
2010-09-15
Anonymous
No

The attached patch adds a feature for smart pointers which have both a const and non-const operator->(). Calls to const methods are delegated via the const operator->() while calls to non-const methods use the non-const operator->(). Our specific use case is with a object-database system where the smart pointer is a pinning reference on an object. For example when a non-const method is run the object or rather its memory area, it is automatically flagged as dirty. This is also used to implement object versioning where non-const method calls cause a new object version to be created.

The patch adds this overload support for normal method calls, member variable getting and setting, static methods and methods added using %extend. The new behavior is only enabled if a smart pointer class has both a const and non-const operator->(), if not the exact same code is generated as before. The patch also includes a test case for the new functionality, it as well as the other tests runs without problem.

An example of a smart pointer where calls to A::get() is handled normally, while calls to A::set() marks the object as dirty with the object store.

struct A {
int get() const;
void set(int a);
};

struct ptr {
A *a;

const A *operator->() const {
return a;
}

A *operator->() {
dirty(a);
return a;
}
};

Discussion

  • William Fulton

    William Fulton - 2010-09-25

    Thanks, this looks good. A couple of questions before trying it out:
    - Does it work with const member variables, eg: const X a; const X *b; X *const c;
    - Does it work with Java or C#, which uses different code in cwrap.c which python/scripting languages don't call.

     
  • Comment has been marked as spam. 
    Undo

    You can see all pending comments posted by this user  here

    Anonymous - 2010-09-30

    Patch with Java and Python test cases

     
  • Comment has been marked as spam. 
    Undo

    You can see all pending comments posted by this user  here

    Anonymous - 2010-09-30

    - It works fine with const member variables, getting a member variable is assumed to be a const access and uses the const overload, setting it is a modifying operation and uses the non-const overload.

    - I have tested it with Java, I don't have a C# compiler handy but the Java unit test should be easy to translate to C#. When compiling the Java test a warning that operator->() will be ignored and that operator->() const will be used instead is printed, but the generated code dispatches to the correct overload anyway.

    I've attached an updated patch with additional tests for the pointer types you suggested, it also includes the identical unit test ported to Java.

     
  • William Fulton

    William Fulton - 2010-10-04

    Thanks, applied for swig-2.0.1.

     
  • William Fulton

    William Fulton - 2010-10-04
    • assigned_to: nobody --> wsfulton
    • status: open --> closed-accepted
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks