краткость С++
От: zip_ Россия  
Дата: 25.11.04 17:39
Оценка: :)
вот во что могут превратиться две строчки кода типа

struct size {
  unsigned w;
  unsigned h;
}


->


#ifndef __z_size_inc__
#define __z_size_inc__

#include <cassert>
#include <algorithm> // for std::swap
#include <limits> // std::numeric_limits

namespace z {

template<typename>
class basic_size;

typedef basic_size<unsigned> size;

template<typename T>
class basic_size {
public:
    explicit basic_size(T w=0, T h=0):
        w_(w), h_(h) {}

    template<typename U>
    explicit basic_size(const basic_size<U>& s):
        w_(s.w_), h_(s.h_) {}

    template<typename U>
    basic_size& operator=(const basic_size<U>& s) { 
        basic_size<T>(s).swap(*this); 
        return *this; 
    }


    bool empty() const
        { return w_==0 && h_==0; }

    T w() const
        { return w_; }

    T h() const
        { return h_; }


    void setw(T w)
        { w_=w; }

    void seth(T h)
        { h_=h; }

    void set(T w, T h)
        { setw(w); seth(h); }


    template<typename U>
    bool operator==(const basic_size<U>& s) const
        { return w_==s.w_ && h_==s.h_; }

    template<typename U>
    bool operator!=(const basic_size<U>& s) const 
        { return !(*this==s); }


    const basic_size& operator+=(T d) {
        assert((std::numeric_limits<T>::max()-d>=w_) &&
            (std::numeric_limits<T>::max()-d>=h_));
        w_+=d; h_+=d; 
        return *this; 
    }

    template<typename U>
    const basic_size& operator+=(const basic_size<U>& s) {
        assert((std::numeric_limits<T>::max()-s.w_>=w_) &&
            (std::numeric_limits<T>::max()-s.h_>=h_));
        w_+=s.w_; h_+=s.h_; 
        return *this; 
    }


    const basic_size& operator-=(T d) { 
        assert(d<w_ && d<h_);
        w_-=d; h_-=d; 
        return *this; 
    }

    template<typename U>
    const basic_size& operator-=(const basic_size<U>& s) { 
        assert(s.w_<w_ && s.h_<h_);
        w_-=s.w_; h_-=s.h_;
        return *this; 
    }


    const basic_size& operator*=(T k) { 
        w_*=k; h_*=k; 
        return *this; 
    }

    template<typename U>
    const basic_size& operator*=(const basic_size<U>& s) { 
        w_*=s.w_; h_*=s.h_; 
        return *this; 
    }


    const basic_size& operator/=(T k) { 
        assert(k!=0);    
        w_/=k; h_/=k; 
        return *this; 
    }

    template<typename U>
    const basic_size& operator/=(const basic_size<U>& s) { 
        assert(s.w_!=0 && s.h_!=0);
        w_/=s.w_; h_/=s.h_; 
        return *this; 
    }


    void swap(basic_size& s) {
        std::swap(w_, s.w_);
        std::swap(h_, s.h_);
    }

private:

    template<typename U>
    friend class basic_size;

    T w_;
    T h_;
};

template<typename T>
basic_size<T> operator+(const basic_size<T>& s, T d) 
    { return basic_size<T>(s)+=d; }

template<typename T, typename U>
basic_size<T> operator+(const basic_size<T>& s1, const basic_size<U>& s2) 
    { return basic_size<T>(s1)+=s2; }

template<typename T>
basic_size<T> operator-(const basic_size<T>& s, T d) 
    { return basic_size<T>(s)-=d; }

template<typename T, typename U>
basic_size<T> operator-(const basic_size<T>& s1, const basic_size<U>& s2) 
    { return basic_size<T>(s1)-=s2; }

template<typename T>
basic_size<T> operator*(const basic_size<T>& s, T d) 
    { return basic_size<T>(s)*=d; }

template<typename T, typename U>
basic_size<T> operator*(const basic_size<T>& s1, const basic_size<U>& s2) 
    { return basic_size<T>(s1)*=s2; }

template<typename T>
basic_size<T> operator/(const basic_size<T>& s, T d) 
    { return basic_size<T>(s)/=d; }

template<typename T, typename U>
basic_size<T> operator/(const basic_size<T>& s1, const basic_size<U>& s2) 
    { return basic_size<T>(s1)/=s2; }

} // namespace z

#endif //__z_size_inc__


гляньте, может еще чего упустил.

ненавижу этот язык, пойду что ли напьюсь, а то писать еще point, rect, region etc..

 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.