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,465 of 33,346   
   DeMarcus to All   
   How do you solve the general missing-val   
   08 Jul 12 09:57:09   
   
   From: use_my_alias_here@hotmail.com   
      
   Hi,   
      
   Sometimes you need to inform that the value is missing. Some use   
   pointers to objects where NULL represents the missing value. Another   
   solution is to use boost::optional.   
      
   However, sometimes you need to provide more second level information   
   than just missing value. How do you solve that?   
      
   I came up with an idea below. Is that a good solution or are there   
   better ways to solve it? Does any library have anything similar? What   
   can be improved? Any feedback is welcome.   
      
      
   #include    
   #include    
      
   template   
   class Multivalence   
   {   
   public:   
        Multivalence() : valence_(init) {}   
        Multivalence( const T& t ) : object_(t), valence_(value) {}   
        Multivalence( V valence ) : valence_(valence)   
        {   
           assert( valence != value && "This valence is automatically set "   
              "when constructing with an object" );   
        }   
      
        T& operator*()   
        {   
           assert( valence_ == value && "Reference to invalid object" );   
           return object_;   
        }   
      
        const T& operator*() const   
        {   
           assert( valence_ == value && "Reference to invalid object" );   
           return object_;   
        }   
      
        operator V() const   
        {   
           return valence_;   
        }   
      
   private:   
        T object_;   
        V valence_;   
      
   };   
      
   enum Signal   
   {   
        NOT_AVAILABLE,   
        AVAILABLE,   
        WEAK,   
        DISTORTED   
   };   
      
   typedef Multivalence<   
        double,   
        Signal,   
        NOT_AVAILABLE,   
        AVAILABLE> SignalDouble;   
      
   SignalDouble getSample()   
   {   
        //return SignalDouble();            // Missing value.   
        return SignalDouble( 8.75 );        // Good value.   
        //return SignalDouble( DISTORTED ); // Distorted value.   
   }   
      
   int main( int argc, char* argv[] )   
   {   
        SignalDouble sample = getSample();   
      
        switch( sample )   
        {   
           case NOT_AVAILABLE:   
              std::cout << "No sample available" << std::endl;   
              break;   
      
           case AVAILABLE:   
              std::cout << "Sample: " << *sample << std::endl;   
              break;   
      
           case WEAK:   
              std::cout << "Signal too weak" << std::endl;   
              break;   
      
           case DISTORTED:   
              std::cout << "Signal distorted" << std::endl;   
              break;   
        }   
      
        return 0;   
   }   
      
      
   Thanks,   
   Daniel   
      
      
   --   
         [ 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