Interface
The shared attribute is all protocol.
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 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;
};
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; };
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)); }