Smart Pointers

value_comparsion_semantic_policy Struct Reference

#include <smart_ptr.hpp>

List of all members.


Detailed Description

This makes smart_ptr compare the pointee instead of the pointer address. This is a policy that allows smart_ptr to be used with associated containers, and STL sorting algorithms. When used with comparison operators, this policy makes smart_ptr compare the pointee, instead of the pointer address.
Example Usage.

class automobile
{
public:
        automobile(const string &LicPlate):m_LicPlate(LicPlate){}
        automobile(const automobile& src):m_LicPlate(src.m_LicPlate){}
        automobile& operator=(const automobile& src){m_LicPlate=src.m_LicPlate;}
        virtual ~automobile(){}
        virtual string GetTypeName()const = 0;
        virtual bool operator<(const automobile& s)const
        {
                if (m_LicPlate == s.m_LicPlate)
                        return GetTypeName() < s.GetTypeName();
                return m_LicPlate < s.m_LicPlate;
        }
protected:
        string m_LicPlate;
};

class car : public automobile
{
public:
        car(const string &LicPlate):automobile(LicPlate){}
        car(const car& src):automobile(src.m_LicPlate){}
        car& operator=(const car& src){m_LicPlate=src.m_LicPlate;}
        string GetTypeName()const{return "car";}
};

class truck : public automobile
{
public:
        truck(const string &LicPlate):automobile(LicPlate){}
        truck(const truck& src):automobile(src.m_LicPlate){}
        truck& operator=(const truck& src){m_LicPlate=src.m_LicPlate;}
        string GetTypeName()const{return "truck";}
};

void example_value_comparsion_semantic_policy_usage()
{
        //value_comparsion_semantic_policy is the default policy
        //and does not need to be specified explicitly as in the following
        //example
        typedef smart_ptr<automobile, 
                ownership_default_policy, 
                allocator_default_policy, 
                checking_default_policy, 
                value_comparsion_semantic_policy> SmrtPtrAutomobile;

        std::map<SmrtPtrAutomobile, string>  mAutomobiles1;
        mAutomobiles1[new car("1234")]  = "IBM";
        mAutomobiles1[new truck("8254")] = "MS";
        mAutomobiles1[new car("7777")]  = "Intel";
        mAutomobiles1[new truck("0215")] = "Apple";

        std::map<string, SmrtPtrAutomobile >  mAutomobiles2;
        mAutomobiles2["IBM"     ]       =       SmrtPtrAutomobile(new car("1234"))              ;
        mAutomobiles2["MS"      ]       =       SmrtPtrAutomobile(new truck("8254"))    ;
        mAutomobiles2["Intel"]  =       SmrtPtrAutomobile(new car("7777"))              ;
        mAutomobiles2["Apple"]  =       SmrtPtrAutomobile(new truck("0215"))    ;

        std::set<SmrtPtrAutomobile > sAutomobiles;
        sAutomobiles.insert(new car("1234"));
        sAutomobiles.insert(new truck("8254"));
        sAutomobiles.insert(new car("7777"));
        sAutomobiles.insert(new truck("0215"));
}

Note:
All members are static template functions, so as to avoid requiring the class to be a template class, and to avoid requiring an instance of the class.
See also:
smart_ptr, pointer_comparsion_semantic_policy, no_comparsion_semantic_policy

Definition at line 283 of file smart_ptr.hpp.

Static Public Member Functions

template<class T1, class T2>
static bool less_than (const T1 &a, const T2 &b)
template<class T1, class T2>
static bool greater_than (const T1 &a, const T2 &b)
template<class T1, class T2>
static bool less_than_or_equal (const T1 &a, const T2 &b)
template<class T1, class T2>
static bool greater_than_or_equal (const T1 &a, const T2 &b)
template<class T1, class T2>
static bool is_equal (const T1 &a, const T2 &b)


The documentation for this struct was generated from the following file:
Generated on Wed Mar 29 21:58:59 2006 for Smart Pointers by  doxygen 1.4.6.Axter [Axter-Extended-Version]