TThicknessAttribute example

The paper thickness is defined by a single short int. Because the storage needed to specify the paper thickness is actually less than the amount of storage needed to point to an internal attribute, you simply store the data in the attribute itself.

Below is the complete interface of TThicknessAttribute.

Interface

      class TThicknessAttribute : public TAttribute {
      public:
          MCollectibleDeclarationsMacro(TThicknessAttribute);
          
                                  TThicknessAttribute();  //defaults to 0
                                  TThicknessAttribute(short thickness);
                                  TThicknessAttribute(const TThicknessAttribute&);
          virtual                 ~TThicknessAttribute();
          TThicknessAttribute&    operator=(const TThicknessAttribute&);
      
          virtualTStream&         operator>>=(TStream& toStream) const;
          virtualTStream&         operator<<=(TStream& fromStream);
      
          virtual long            Hash() const;
          virtual Boolean         IsEqual(const MCollectible*) const;
          
          virtual const TToken&   GetName() const;
          virtual const TToken&   GetCategory() const;
          virtual short           GetThickness() const;
      
      private:
          const static TToken     fgName;
          short                   fThickness; 
          enum { kOriginalVersion };
      };
NOTE All attributes are immutable, so there is no SetThickness member function because the thickness can not change. Also, while this example also shows you one way to override GetName, the default implementation would have been adequate.

Implementation

The following example shows the complete implementation of TThicknessAttribute. Most of it is generic. Note that IsEqual checks NamesMatch before coercing the attribute, but does not perform any other checking.

See Chapter 2, "Streams" for specific information on streams. See Chapter 1, "Collections" for a general discussion of implementing Hash and IsEqual.

      MCollectibleDefinitionsMacro(TThicknessAttribute, kOriginalVersion);
      const TToken TThicknessAttribute::fgName = TToken(TStandardText("Thickness"));
      
      TThicknessAttribute::TThicknessAttribute()
          : TAttribute()
      {
          fThickness = 0;
      }
      
      TThicknessAttribute::TThicknessAttribute(short thickness)
          : TAttribute()
      {
          fThickness = thickness;
      }
      
      TThicknessAttribute::TThicknessAttribute(const TThicknessAttribute& copyFrom)
          : TAttribute(copyFrom)
      {
          fThickness = copyFrom.fThickness;
      }
      
      TThicknessAttribute::~TThicknessAttribute()
      {
      }
      
      TThicknessAttribute& TThicknessAttribute::operator=(const TThicknessAttribute&comesFrom)
      {
          if (&comesFrom != this) {
              TAttribute::operator=(comesFrom);
              fThickness = comesFrom.fThickness;}
          }
      
          return *this;
      }
      
      TStream& TThicknessAttribute::operator>>=(TStream& toStream) const
      {
          TAttribute::operator>>=(toStream);
          fThickness >>= toStream;
          return toStream;
      }
      
      TStream& TThicknessAttribute::operator<<=(TStream& fromStream)
      {
          TAttribute::operator<<=(fromStream);
          fThickness <<= fromStream;
          return fromStream;
      }
      
      long TThicknessAttribute::Hash() const
      {
          return fgName.Hash() ^ gCategory.Hash() ^ fThickness;
      }
      
      Boolean TThicknessAttribute::IsEqual(const MCollectible* obj) const
      {
          return NamesMatch((TAttribute*)obj) &&
              (fThickness == ((TThicknessAttribute*)obj)->fThickness);
      }
      
      const TToken& TThicknessAttribute::GetName() const
      {
          return fgName;
      }
      
      const TToken& TThicknessAttribute::GetCategory() const
      {
          return gPaperCategory;
      }
          
      short TThicknessAttribute::GetThickness() const
      {
          return fThickness;
      }


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker