← Back to team overview

yade-users team mailing list archive

Re: [Question #697142]: Writing new Ip2 functor / order of contact partners

 

Question #697142 on Yade changed:
https://answers.launchpad.net/yade/+question/697142

    Status: Open => Answered

Jan Stránský proposed the following answer:
Hello,

to test the specific instance, you can dynamic_cast the pointer and test
if the cast was successful or not, something like:

/////
Ip2_FrictMat_myMaterial_myPhysics :: go(const shared_ptr<Material>& b1, const shared_ptr<Material>& b2, const shared_ptr<Interaction>& interaction) {
   MyMaterial myMatTest* = dynamic_cast<MyMaterial*>(b2.get());
   if (!myMatTest) { // b2 is not MyMaterial instance
      std::swap(b1,b2);
   }
   /// now b1 is FrictMat and b2 is MyMaterial
   ...
}
/////


A (non-Yade) illustration MWE:
////// g++ test.cpp -o test && ./test
#include<iostream>
#include<memory>
class Base { public: virtual ~Base(){} };
class Derived1 : public Base {};
class Derived2 : public Base {};
int main() {
   std::shared_ptr<Base> b1(new Derived1());
   std::shared_ptr<Base> b2(new Derived2());
   //
   Base* base1 = dynamic_cast<Base*>(b1.get());
   if (base1) std::cout << "base1 is Base\n";
   else std::cout << "base1 is NOT Base\n";
   //
   Base* base2 = dynamic_cast<Base*>(b2.get());
   if (base2) std::cout << "base2 is Base\n";
   else std::cout << "base2 is NOT Base\n";
   //
   Derived1* d11 = dynamic_cast<Derived1*>(b1.get());
   if (d11) std::cout << "b1 is Derived1\n";
   else std::cout << "b1 is NOT Derived1\n";
   //
   Derived2* d21 = dynamic_cast<Derived2*>(b1.get());
   if (d21) std::cout << "b1 is Derived2\n";
   else std::cout << "b1 is NOT Derived2\n";
   //
   //
   Derived1* d12 = dynamic_cast<Derived1*>(b2.get());
   if (d12) std::cout << "b1 is Derived1\n";
   else std::cout << "b1 is NOT Derived1\n";
   //
   Derived2* d22 = dynamic_cast<Derived2*>(b2.get());
   if (d22) std::cout << "b2 is Derived2\n";
   else std::cout << "b2 is NOT Derived2\n";
   return 0;
}
//////

Cheers
Jan

-- 
You received this question notification because your team yade-users is
an answer contact for Yade.