TDimensionAttribute example

Assume that two doubles are way too much overhead to pay (there will be thousands of these attributes floating around). Then it makes sense to store only the data in a single instance of a class derived from TInternalAttribute and have the thousands of instances be of a class derived from TSharedAttribute, so their overhead is merely a pointer to the one internal attribute.

Interface

The shared attribute is all protocol.

      class TDimensionAttribute : public TSharedAttribute {
      public:
          MCollectibleDeclarationsMacro(TDimensionAttribute);
      
                                  TDimensionAttribute();  //defaults to 0x0
                                  TDimensionAttribute(double width, double height);
                                  TDimensionAttribute(const TDimensionAttribute&);
          virtual                 ~TDimensionAttribute();
          TDimensionAttribute&    operator=(const TDimensionAttribute&);
      
          double                  GetWidth() const;
          double                  GetHeight() const;
      };
The internal attribute holds the data. Because clients never see this, the entire class definition could be private. This interface is just the same as when you derive from TAttribute directly. (Finalize is not used in this example.)

      class TInternalDimensionAttribute : public TInternalAttribute {
      public:
          MCollectibleDeclarationsMacro(TInternalDimensionAttribute);
      
                                  TInternalDimensionAttribute();  //defaults to 0x0
                                  TInternalDimensionAttribute(double width, double height);
                                  TInternalDimensionAttribute(
                                      const TInternalDimensionAttribute&);
          virtual                 ~TInternalDimensionAttribute();
          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;
      
          double                  GetWidth() const;
          double                  GetHeight() const;
      
      private:
          const static TToken     fgName;
          double                  fWidth;
          double                  fHeight;
      };

Implementation

Only the particular implementations of interest are provided below. The implementation of TInternalDimensionAttribute is omitted entirely, as it corresponds exactly to that of TThicknessAttribute.

The shared attribute's standard constructors instantiate its corresponding internal attribute and call AdoptMyManager to register it. The manager calls the SetInternalAttribute member function with the internal attribute it decides to use (it might not be the identical one we provided, but it has the same name and value).

      TDimensionAttribute::TDimensionAttribute()
          : TSharedAttribute()
      {
          AdoptByManager(new TInternalDimensionAttribute());
      }
      
      TDimensionAttribute::TDimensionAttribute(double width, double height)
          : TSharedAttribute()
      {
          AdoptByManager(new TInternalDimensionAttribute(width, height));
      }

[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