home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,480 of 33,346   
   Wil Evers to Francis Glassborow   
   Re: Layered data structures   
   15 Jul 12 16:05:03   
   
   From: bouncer@dev.null   
      
   Francis Glassborow wrote:   
      
   > 2) If B objects need not reference an identical A object then each B   
   > object should hold its own copy of the A object and destroy that   
   > copy when it has finished with it.   
   >   
   > I guess 2) might raise issues of executable size but that is an   
   > optimisation issue not a design issue.   
      
   Agreed, but how do we solve that optimization issue?  Instead of   
   giving each B its own A instance, we may want some instances of B to   
   *share* an immutable instance of A.  The question is how to express   
   that in B's class definition.  Since the immutability requirement is   
   in B, A's definition should not have to change because of it.   
      
   It might seem having B's constructor take and store a   
   shared_ptr would do the job, but that is not good enough.   
   A shared_ptr does not guarantee the A pointed to is   
   immutable: a single A instance may be owned by several instances   
   of both shared_ptr and shared_ptr.   
      
   I think the key is to make sure the A instance is closed for   
   modification before it is shared.  The following class template is my   
   take at expressing that:   
      
       template    
       class shared_immutable_ptr {   
      
       public :   
           template    
           shared_immutable_ptr(std::unique_ptr consumed)   
           : instance(std::move(consumed))   
           { }   
      
           const T *operator->() const   
           { return instance.operator->(); }   
      
           const T& operator*() const   
           { return *instance; }   
      
           // etc...   
      
       private :   
           std::shared_ptr instance;   
       };   
      
   B can now state its requirements by having a constructor that takes   
   and stores a shared_immutable_ptr.  User code would look something   
   like this:   
      
       std::unique_ptr build_a()   
       {   
           std::unique_ptr result(new A);   
           result->modify();   
           return result;   
       }   
      
       void f()   
       {   
           shared_immutable_ptr shared_a(build_a());   
           B b1(shared_a);   
           B b2(shared_a);   
           // ...   
       }   
      
   Regards,   
      
   - Wil   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca