/*
 * Copyright (c) 6th November 2011, Luca Risolia
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <iostream>
#include <stdexcept>

using namespace std;

template<class T>
class Ptr_to_T {
    T* array;
    int size, pos;
    Ptr_to_T(T* v, int s, int pos);
public:
    Ptr_to_T(T* p, T* v, int s);
    Ptr_to_T(T* p);
    /* No boundary checking will be done in the below operators */
    Ptr_to_T& operator--();
    Ptr_to_T operator--(int);
    Ptr_to_T& operator++();
    Ptr_to_T operator++(int);
    /* All the checks will be done when dereferncing the pointer */
    T* operator->() const;
    T& operator*() const;
};

template<class T> Ptr_to_T<T>::Ptr_to_T(T* p, T* v, int s) : array(v), size(s), pos(p - v) { }

template<class T> Ptr_to_T<T>::Ptr_to_T(T* p) : array(p), size(1), pos(0) { }

template<class T> Ptr_to_T<T>::Ptr_to_T(T* v, int s, int pos) : array(v), size(s), pos(pos) { }

template<class T> T* Ptr_to_T<T>::operator->() const {
    if (!array)
        throw std::invalid_argument("invalid pointer dereference");
    if (pos < 0 || pos >= size)
        throw std::range_error("out of bounds");
    return &array[pos];
}

template<class T> T& Ptr_to_T<T>::operator *() const {
    if (!array)
        throw std::invalid_argument("invalid pointer dereference");
    if (pos < 0 || pos >= size)
        throw std::range_error("out of bounds");
    return array[pos];
}

template<class T> Ptr_to_T<T>& Ptr_to_T<T>::operator--() {
    --pos;
    return *this;
}

template<class T> Ptr_to_T<T> Ptr_to_T<T>::operator--(int) {
    Ptr_to_T t(array, size, pos);
    pos--;
    return t;
}

template<class T> Ptr_to_T<T>& Ptr_to_T<T>::operator++() {
    ++pos;
    return *this;
}

template<class T> Ptr_to_T<T> Ptr_to_T<T>::operator++(int) {
    Ptr_to_T t(array, size, pos);
    pos++;
    return t;
}

struct U {
    int a;

    U(int x) : a(x) { }
};

int main(int argc, char** argv) {
    U v[] = {1, 2, 3, 4, 5};
    Ptr_to_T<U> p(&v[3], v, 5);
    const Ptr_to_T<U> q(&v[1]), r(0);
    cout << (*p++).a << '\n';
    cout << p->a << '\n';
    //q = p;
    cout << q->a << '\n';
    try {
        cout << (*r).a << '\n';
    } catch (const std::exception& e) {
        cout << e.what() << '\n';
    }
    return 0;
}
