"Fossies" - the Fresh Open Source Software Archive

Member "muscle/html/muscle-by-example/examples/refcount/example_3_conversions.cpp" (8 Jun 2019, 2904 Bytes) of package /linux/privat/muscle7.30.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include "system/SetupSystem.h"  // for CompleteSetupSystem
    2 #include "util/RefCount.h"
    3 
    4 using namespace muscle;
    5 
    6 static void PrintExampleDescription()
    7 {
    8    printf("\n");
    9    printf("This example demonstrates which Ref assignment-conversions are allowed and which are compile-time errors\n");
   10    printf("(the rules are very similar to the rules enforced by the compiler for raw pointers)\n");
   11    printf("\n");
   12 }
   13 
   14 /** An example of a class we want to allocate objects of from the heap,
   15   * but still avoid any risk of memory leaks.
   16   */
   17 class MyBaseClass : public RefCountable
   18 {
   19 public:
   20    MyBaseClass() {}
   21    ~MyBaseClass() {}
   22 };
   23 DECLARE_REFTYPES(MyBaseClass);  // defines MyBaseClassRef and ConstMyBaseClassRef
   24 
   25 /** A subclass of MyBaseClass */
   26 class MySubClass : public MyBaseClass
   27 {
   28 public:
   29    MySubClass() {}
   30    ~MySubClass() {}
   31 };
   32 DECLARE_REFTYPES(MySubClass);  // defines MySubClassRef and ConstMySubClassRef
   33 
   34 int main(int argc, char ** argv)
   35 {
   36    CompleteSetupSystem css;
   37 
   38    PrintExampleDescription();
   39 
   40    MyBaseClassRef mc1(new MyBaseClass);
   41 
   42    MyBaseClassRef mc2 = mc1;       // Copying a Ref is ok
   43    ConstMyBaseClassRef mc3 = mc1;  // Initializing a ConstRef from a Ref (adding Const) is ok
   44 
   45 #ifdef COMMENTED_OUT_BECAUSE_THIS_WONT_COMPILE
   46    MyBaseClassRef mc4 = mc3; // Initializing a Ref from a ConstRef is a compile-time-error!
   47 #endif
   48 
   49    // But if you absolutely MUST cast away const (and like to live dangerously), you can:
   50    MyBaseClassRef mc4 = CastAwayConstFromRef(mc3);  // danger will robinson!
   51 
   52    MySubClassRef sc1(new MySubClass);
   53 
   54    MyBaseClassRef mc5 = sc1;   // Initializing a base-class ref from a subclass-ref is ok
   55 
   56    RefCountableRef rc1 = sc1;  // Initializing a RefCountableRef from any Ref is always okay
   57    RefCountableRef rc2 = sc1.GetRefCountableRef(); // another way to do the same thing
   58 
   59 #ifdef COMMENTED_OUT_BECAUSE_THIS_WONT_COMPILE
   60    MySubClassRef sc2 = mc1;   // Initializing a sub-class ref from a baseclass-ref is a compile-time-error!
   61 #endif
   62 
   63    // But if you really want to down-cast a baseclass-ref to a subclass-ref, you can do it;
   64    MySubClassRef sc2(mc1.GetRefCountableRef(), true);
   65    // Note that sc2 may end up being a NULL Ref if the implicit dynamic_cast<SubClass*>(mc1()) call failed!
   66 
   67    // Another way to do the same thing
   68    MySubClassRef sc3;
   69    if (sc3.SetFromRefCountableRef(mc5.GetRefCountableRef()) == B_NO_ERROR)
   70    {
   71       printf("SetFromRefCountableRef succeeded, sc3 now points to MySubClass object %p\n", sc3());
   72    }
   73    else printf("SetFromRefCountableRef failed!  mc5 wasn't pointing to a MySubClass object!\n");
   74 
   75    // And if you're feeling super-aggressive, you can even do it without the
   76    // call to dynamic_cast -- but beware:  if you're wrong about the validity
   77    // of the downcast, you'll get undefined behavior here!
   78    MySubClassRef sc4;
   79    sc4.SetFromRefCountableRefUnchecked(mc5);
   80 
   81    printf("\n");
   82    return 0;
   83 }