]> git.tdb.fi Git - gldbg.git/blob - source/autoconstptr.h
04e1fba6fd91ef8bf564c1e564a27c6c38273362
[gldbg.git] / source / autoconstptr.h
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #ifndef AUTOCONSTPTR_H_
9 #define AUTOCONSTPTR_H_
10
11 /**
12 A smart pointer that only provides non-const access to the contents if the
13 pointer itself is non-const.
14 */
15 template<typename T>
16 class AutoConstPtr
17 {
18 private:
19         T *ptr;
20
21 public:
22         AutoConstPtr(T *p): ptr(p) { }
23         AutoConstPtr &operator=(T *p) { ptr = p; return *this; }
24         T *operator->() { return ptr; }
25         const T *operator->() const { return ptr; }
26         operator T *() { return ptr; }
27         operator const T *() const { return ptr; }
28 };
29
30 #endif