os/ossrv/stdcpp/tsrc/Boost_test/ptr_container/src/tut1.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/stdcpp/tsrc/Boost_test/ptr_container/src/tut1.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,355 @@
     1.4 +//
     1.5 +// Boost.Pointer Container
     1.6 +//
     1.7 +//  Copyright Thorsten Ottosen 2003-2005. Use, modification and
     1.8 +//  distribution is subject to the Boost Software License, Version
     1.9 +//  1.0. (See accompanying file LICENSE_1_0.txt or copy at
    1.10 +//  http://www.boost.org/LICENSE_1_0.txt)
    1.11 +//
    1.12 +// For more information, see http://www.boost.org/libs/ptr_container/
    1.13 +//
    1.14 +
    1.15 +//
    1.16 +// This example is intended to get you started.
    1.17 +// Notice how the smart container
    1.18 +//
    1.19 +// 1. takes ownership of objects
    1.20 +// 2. transfers ownership
    1.21 +// 3. applies indirection to iterators 
    1.22 +// 4. clones objects from other smart containers
    1.23 +// 
    1.24 +
    1.25 +//
    1.26 +// First we select which container to use.
    1.27 +//
    1.28 +#include <boost/ptr_container/ptr_deque.hpp>
    1.29 +
    1.30 +//
    1.31 +// we need these later in the example
    1.32 +//
    1.33 +#include <boost/assert.hpp>
    1.34 +#include <string>
    1.35 +#include <exception>
    1.36 +
    1.37 +
    1.38 +//
    1.39 +// Then we define a small polymorphic class
    1.40 +// hierarchy.
    1.41 +// 
    1.42 +
    1.43 +class animal : boost::noncopyable
    1.44 +{
    1.45 +    virtual std::string do_speak() const = 0;
    1.46 +    std::string name_;
    1.47 +
    1.48 +protected:
    1.49 +    //
    1.50 +    // Animals cannot be copied...
    1.51 +    //
    1.52 +    animal( const animal& r ) : name_( r.name_ )           { }
    1.53 +    void operator=( const animal& );
    1.54 +
    1.55 +private:
    1.56 +    //
    1.57 +    // ...but due to advances in genetics, we can clone them!
    1.58 +    //
    1.59 +
    1.60 +    virtual animal* do_clone() const = 0;
    1.61 +        
    1.62 +public:
    1.63 +    animal( const std::string& name ) : name_(name)        { }
    1.64 +    virtual ~animal() throw()                              { }
    1.65 +    
    1.66 +    std::string speak() const
    1.67 +    {
    1.68 +        return do_speak();
    1.69 +    }
    1.70 +
    1.71 +    std::string name() const
    1.72 +    {
    1.73 +        return name_;
    1.74 +    }
    1.75 +
    1.76 +    animal* clone() const
    1.77 +    {
    1.78 +        return do_clone();
    1.79 +    }
    1.80 +};
    1.81 +
    1.82 +//
    1.83 +// An animal is still not Clonable. We need this last hook.
    1.84 +//
    1.85 +// Notice that we pass the animal by const reference
    1.86 +// and return by pointer.
    1.87 +//
    1.88 +
    1.89 +animal* new_clone( const animal& a )
    1.90 +{
    1.91 +    return a.clone();
    1.92 +}
    1.93 +
    1.94 +//
    1.95 +// We do not need to define 'delete_clone()' since
    1.96 +// since the default is to call the default 'operator delete()'.
    1.97 +//
    1.98 +
    1.99 +const std::string muuuh = "Muuuh!";
   1.100 +const std::string oiink = "Oiiink";
   1.101 +
   1.102 +class cow : public animal
   1.103 +{
   1.104 +    virtual std::string do_speak() const
   1.105 +    {
   1.106 +        return muuuh;
   1.107 +    }
   1.108 +
   1.109 +    virtual animal* do_clone() const
   1.110 +    {
   1.111 +        return new cow( *this );
   1.112 +    }
   1.113 +
   1.114 +public:
   1.115 +    cow( const std::string& name ) : animal(name)          { }
   1.116 +};
   1.117 +
   1.118 +class pig : public animal
   1.119 +{
   1.120 +    virtual std::string do_speak() const
   1.121 +    {
   1.122 +        return oiink;
   1.123 +    }
   1.124 +
   1.125 +    virtual animal* do_clone() const
   1.126 +    {
   1.127 +        return new pig( *this );
   1.128 +    }
   1.129 +    
   1.130 +public:
   1.131 +    pig( const std::string& name ) : animal(name)          { }
   1.132 +};
   1.133 +
   1.134 +//
   1.135 +// Then we, of course, need a place to put all
   1.136 +// those animals.
   1.137 +//
   1.138 +
   1.139 +class farm
   1.140 +{
   1.141 +    //
   1.142 +    // This is where the smart containers are handy
   1.143 +    //
   1.144 +    typedef boost::ptr_deque<animal> barn_type;
   1.145 +    barn_type                        barn;
   1.146 +
   1.147 +    //
   1.148 +    // An error type
   1.149 +    //
   1.150 +    struct farm_trouble : public std::exception           { };
   1.151 +
   1.152 +public:
   1.153 +    // 
   1.154 +    // We would like to make it possible to
   1.155 +    // iterate over the animals in the farm
   1.156 +    //
   1.157 +    typedef barn_type::iterator  animal_iterator;
   1.158 +
   1.159 +    //
   1.160 +    // We also need to count the farm's size...
   1.161 +    //
   1.162 +    typedef barn_type::size_type size_type;
   1.163 +    
   1.164 +    //
   1.165 +    // And we also want to transfer an animal
   1.166 +    // safely around. The easiest way to think
   1.167 +    // about '::auto_type' is to imagine a simplified
   1.168 +    // 'std::auto_ptr<T>' ... this means you can expect
   1.169 +    // 
   1.170 +    //   T* operator->()
   1.171 +    //   T* release()
   1.172 +    //   deleting destructor
   1.173 +    //
   1.174 +    // but not more.
   1.175 +    //
   1.176 +    typedef barn_type::auto_type  animal_transport;
   1.177 +
   1.178 +    // 
   1.179 +    // Create an empty farm.
   1.180 +    //
   1.181 +    farm()                                                 { }
   1.182 +    
   1.183 +    //
   1.184 +    // We need a constructor that can make a new
   1.185 +    // farm by cloning a range of animals.
   1.186 +    //
   1.187 +    farm( animal_iterator begin, animal_iterator end )
   1.188 +     : 
   1.189 +        //
   1.190 +        // Objects are always cloned before insertion
   1.191 +        // unless we explicitly add a pointer or 
   1.192 +        // use 'release()'. Therefore we actually
   1.193 +        // clone all animals in the range
   1.194 +        //
   1.195 +        barn( begin, end )                               { }
   1.196 +    
   1.197 +    //
   1.198 +    // ... so we need some other function too
   1.199 +    //
   1.200 +
   1.201 +    animal_iterator begin()
   1.202 +    {
   1.203 +        return barn.begin();
   1.204 +    }
   1.205 +
   1.206 +    animal_iterator end()
   1.207 +    {
   1.208 +        return barn.end();
   1.209 +    }
   1.210 +    
   1.211 +    //
   1.212 +    // Here it is quite ok to have an 'animal*' argument.
   1.213 +    // The smart container will handle all ownership
   1.214 +    // issues.
   1.215 +    //
   1.216 +    void buy_animal( animal* a )
   1.217 +    {
   1.218 +        barn.push_back( a );
   1.219 +    }
   1.220 +
   1.221 +    //
   1.222 +    // The farm can also be in economical trouble and
   1.223 +    // therefore be in the need to sell animals.
   1.224 +    //
   1.225 +    animal_transport sell_animal( animal_iterator to_sell )
   1.226 +    {
   1.227 +        if( to_sell == end() )
   1.228 +            throw farm_trouble();
   1.229 +
   1.230 +        //
   1.231 +        // Here we remove the animal from the barn,
   1.232 +        // but the animal is not deleted yet...it's
   1.233 +        // up to the buyer to decide what
   1.234 +        // to do with it.
   1.235 +        //
   1.236 +        return barn.release( to_sell );
   1.237 +    }
   1.238 +
   1.239 +    //
   1.240 +    // How big a farm do we have?
   1.241 +    //
   1.242 +    size_type size() const
   1.243 +    {
   1.244 +        return barn.size();
   1.245 +    }
   1.246 +
   1.247 +    //
   1.248 +    // If things are bad, we might choose to sell all animals :-(
   1.249 +    //
   1.250 +    std::auto_ptr<barn_type> sell_farm()
   1.251 +    {
   1.252 +        return barn.release();
   1.253 +    }
   1.254 +
   1.255 +    //
   1.256 +    // However, if things are good, we might buy somebody
   1.257 +    // else's farm :-)
   1.258 +    //
   1.259 +
   1.260 +    void buy_farm( std::auto_ptr<barn_type> other )
   1.261 +    {
   1.262 +        //
   1.263 +        // This line inserts all the animals from 'other'
   1.264 +        // and is guaranteed either to succeed or to have no
   1.265 +        // effect
   1.266 +        //
   1.267 +        barn.transfer( barn.end(), // insert new animals at the end
   1.268 +                         *other );     // we want to transfer all animals,
   1.269 +                                       // so we use the whole container as argument
   1.270 +        //
   1.271 +        // You might think you would have to do
   1.272 +        //
   1.273 +        // other.release();
   1.274 +        //
   1.275 +        // but '*other' is empty and can go out of scope as it wants
   1.276 +        //
   1.277 +        BOOST_ASSERT( other->empty() );
   1.278 +    }
   1.279 +    
   1.280 +}; // class 'farm'.
   1.281 +
   1.282 +#include <boost/test/included/test_exec_monitor.hpp>
   1.283 +int test_main(int,char *[])
   1.284 +{
   1.285 +    //
   1.286 +    // First we make a farm
   1.287 +    //
   1.288 +    farm animal_farm;
   1.289 +    BOOST_ASSERT( animal_farm.size() == 0u );
   1.290 +    
   1.291 +    animal_farm.buy_animal( new pig("Betty") );
   1.292 +    animal_farm.buy_animal( new pig("Benny") );
   1.293 +    animal_farm.buy_animal( new pig("Jeltzin") );
   1.294 +    animal_farm.buy_animal( new cow("Hanz") );
   1.295 +    animal_farm.buy_animal( new cow("Mary") );
   1.296 +    animal_farm.buy_animal( new cow("Frederik") );
   1.297 +    BOOST_ASSERT( animal_farm.size() == 6u );
   1.298 +
   1.299 +    //
   1.300 +    // Then we make another farm...it will actually contain
   1.301 +    // a clone of the other farm.
   1.302 +    //
   1.303 +    farm new_farm( animal_farm.begin(), animal_farm.end() );
   1.304 +    BOOST_ASSERT( new_farm.size() == 6u );
   1.305 +
   1.306 +    //
   1.307 +    // Is it really clones in the new farm?
   1.308 +    //
   1.309 +    BOOST_ASSERT( new_farm.begin()->name() == "Betty" );
   1.310 +    
   1.311 +    //
   1.312 +    // Then we search for an animal, Mary (the Crown Princess of Denmark),
   1.313 +    // because we would like to buy her ...
   1.314 +    //
   1.315 +    typedef farm::animal_iterator iterator;
   1.316 +    iterator to_sell;
   1.317 +    for( iterator i   = animal_farm.begin(),
   1.318 +                  end = animal_farm.end();
   1.319 +         i != end; ++i )
   1.320 +    {
   1.321 +        if( i->name() == "Mary" )
   1.322 +        {
   1.323 +            to_sell = i;
   1.324 +            break;
   1.325 +        }
   1.326 +    }
   1.327 +
   1.328 +    farm::animal_transport mary = animal_farm.sell_animal( to_sell );
   1.329 +
   1.330 +
   1.331 +    if( mary->speak() == muuuh )
   1.332 +        //
   1.333 +        // Great, Mary is a cow, and she may live longer
   1.334 +        //
   1.335 +        new_farm.buy_animal( mary.release() );
   1.336 +    else
   1.337 +        //
   1.338 +        // Then the animal would be destroyed (!)
   1.339 +        // when we go out of scope.
   1.340 +        //
   1.341 +        ;
   1.342 +
   1.343 +    //
   1.344 +    // Now we can observe some changes to the two farms...
   1.345 +    //
   1.346 +    BOOST_ASSERT( animal_farm.size() == 5u );
   1.347 +    BOOST_ASSERT( new_farm.size()    == 7u );
   1.348 +
   1.349 +    //
   1.350 +    // The new farm has however underestimated how much
   1.351 +    // it cost to feed Mary and its owner is forced to sell the farm...
   1.352 +    //
   1.353 +    animal_farm.buy_farm( new_farm.sell_farm() );
   1.354 +
   1.355 +    BOOST_ASSERT( new_farm.size()    == 0u );
   1.356 +    BOOST_ASSERT( animal_farm.size() == 12u );     
   1.357 +	return 0;
   1.358 +}