Significant change in the Chrono 'develop' branch

99 views
Skip to first unread message

Radu Serban

unread,
Feb 12, 2016, 5:15:30 AM2/12/16
to ProjectChrono
Dear Chrono users,

Just wanted to give you the heads up about a recent push to the 'develop' branch of the Chrono GitHub repository which will definitely affect your own programs that use Chrono.  These changes relate to switching from our own implementation of smart pointers to the C++11 std::shared_ptr.  The classes ChSharedPtr and ChSmartPtr were replaced throughout with std::shared_ptr.   The class ChShared was obsoleted.

Updating your programs to reflect these Chrono API changes should be relatively straightforward.  Please do not hesitate to contact us on this mailing list with any questions you might have.

I listed below a short set of conversion suggestions/hints (just the very basics, hopefully enough for most cases).

Best regards,
Radu


-----------------------------------

  • Types:
    OLD:
       ChSharedPtr<Foo>
    NEW: 
       std::shared_ptr<Foo>

  • Constructors:
    OLD: 
        ChSharedPtr<Foo> my_var(new Foo(arg1, arg2));
    NEW:
        auto my_var = std::make_shared<Foo>(arg1, arg2);
        or
       
    std::shared_ptr<Foo>  my_var(new Foo(arg1, arg2));

  • Copy constructors
    OLD:
         ChSharedPtr<Foo>  var1;
         ChSharedPtr<Foo> var2(var1);
         ChSharedPtr<Foo> var3 = ChSharedPtr<Foo>(var1);

    NEW:
        std::shared_ptr<Foo> var1;
        std::shared_ptr<Foo> var2(var1);
        auto var3 = std::shared_ptr<Foo>(var1);


  • Casting
    OLD:
        ChSharedPtr<Foo>  var1;
        ChSharedPtr<Bar> var2 = var1.DynamicCastTo<Bar>();
        ChSharedPtr<Bar> var3 = var1.StaticCastTo<Bar>();

    NEW:
        std::shared_ptr<Foo> var1;
        auto var2 = std::dynamic_pointer_cast<Bar>(var1);
        auto var3 = std::static_pointer_cast<Bar>(var1);


    Example:
    OLD:
        if (ChSharedPtr<ChTimestepperHHT> mystepper = my_system.GetTimestepper().DynamicCastTo<ChTimestepperHHT>()) {
                mystepper->SetAlpha(-0.2);
                ...
        }

    NEW:
        if (auto mystepper = std::dynamic_pointer_cast<ChTimestepperHHT>(my_system.GetTimestepper())) {
               mystepper->SetAlpha(-0.2);
               ...
        }

  • Access to the wrapped pointer
    OLD:
        ChSharedPtr<Foo> my_var;
        Foo* my_pointer = my_var.get_ptr();

    NEW:
        std::shared_ptr<Foo> my_var;
        Foo* my_pointer = my_var.get();


  • Testing for NULL (empty) shared pointer  (e.g. in an assert, or a conditional)
    OLD:
         ChSharedPtr<Foo> my_var;
         assert(!my_var.IsNull());

    NEW:
         std::shared_ptr<Foo> my_var;
         assert(my_var);


  • Testing for the type should be done explicitly with dynamic_pointer_cast
    OLD:
               ChSharedPtr<Bar> my_var;
          if (my_var.IsType<Foo>()) {
             ....
          }

    NEW:
               std::shared_ptr<Bar> my_var;
          if (std::dynamic_pointer_cast<Foo>(my_var)) {
              ...
          }


Eugenio Rustico

unread,
May 6, 2016, 9:39:59 AM5/6/16
to ProjectChrono
Hello,

I'd suggest to clean the examples as well: there are still a lot of ChSharedPtr (and one ChSmartpointer) used in chrono_vehicle/, chrono_ogre/ and demos/, and this is confusing for who's playing with the examples.

Best regards,
Eugenio

Radu Serban

unread,
May 6, 2016, 12:39:41 PM5/6/16
to projec...@googlegroups.com

Eugenio,

Chrono_ogre was indeed not cleaned up yet.

But Chrono_vehicle should not have any of these left over.  Could you please point me to where you see those?   Also, in which demos do you still see references to these obsolete classes?

Thanks,

Radu

--
You received this message because you are subscribed to the Google Groups "ProjectChrono" group.
To unsubscribe from this group and stop receiving emails from it, send an email to projectchron...@googlegroups.com.
To post to this group, send email to projec...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ER

unread,
May 6, 2016, 2:49:02 PM5/6/16
to Radu Serban, projec...@googlegroups.com
Hello Radu,

for the list of files:

$ grep "ChSharedPtr" -rl src/

This includes:

src/demos/trackVehicle/demo_trackVehicle.cpp
src/demos/vehicle/demo_SuspensionTest/demo_SuspensionTest.cpp:
src/demos/mpi/demo_domains.cpp
...

Just replace -rl with -rn for the exact lines.

Best regards,
Eugenio
> You received this message because you are subscribed to a topic in the
> Google Groups "ProjectChrono" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/projectchrono/VvT2QVDEsNA/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

Radu Serban

unread,
May 6, 2016, 4:11:27 PM5/6/16
to ER, projec...@googlegroups.com
Eugenio,

Yes, I know how to search in the source tree :-) and I am aware that
grep will show those files.
However, with the exception of chrono_ogre (which we will take care of
eventually), all the other places that still contain the obsolete
ChSharedPtr are *deprecated* and not included in the CMake build
process. They were left in the repository only temporarily, until we
make sure that all the functionality was properly provided elsewhere
(for example, the stuff in demos/trackVehicle is being superseded by
functionality in chrono_vehicle itself).

Thanks,
Radu

ER

unread,
May 6, 2016, 4:24:16 PM5/6/16
to Radu Serban, projec...@googlegroups.com
Indeed, I thought it was an unusual request, sorry :)

I took a code that was written in 2015 before the switch and it didn't
compile because of a ChSharedPtr. Then I grepped in the source tree
and I found that an include was missing (core/ChShared.h, included
e.g. in src/demos/trackVehicle/subsys/ChDriverTrack.h), but even with
that it wouldn't compile. That's when I found out there was no such
header and then this post.

Regards,
Eugenio

Radu Serban

unread,
May 6, 2016, 4:45:26 PM5/6/16
to ER, projec...@googlegroups.com
No worries, Eugenio. I just want to make sure that nothing slipped
through the cracks. As I mentioned, those deprecated pieces will be
eventually removed and chrono_ogre updated. However, you shouldn't be
able to find any reference to the old ChSharedPtr in anything that can
currently be configured through CMake.

I hope that this clarifies everything. We're still in the middle of
cleaning up and rearranging stuff in the develop branch. We plan on
having a release later in the year and hopefully thing will be more
stable after that.

Cheers,

Radu
Reply all
Reply to author
Forward
0 new messages