Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Pointer.h « src « mgizapp - github.com/moses-smt/mgiza.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58de60c0e34e41033b4e345062ff375c7a2d1196 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*

Copyright (C) 1997,1998,1999,2000,2001  Franz Josef Och (RWTH Aachen - Lehrstuhl fuer Informatik VI)

This file is part of GIZA++ ( extension of GIZA ).

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
USA.

*/
#ifndef HEADER_Pointer_DEFINED
#define HEADER_Pointer_DEFINED

#include <assert.h>
#include <iostream>

template<class T>
class SmartPointer
{
 protected:
  T*p;
 public:
  SmartPointer(T*_p=0) 
    : p(_p) {}
  inline T&operator*() const 
    {return *p;}
  inline T*operator->() const 
    {return p;}
  inline operator bool() const 
    {return p!=0;}
  inline T*ptr() const
    { return p; }
};
template<class T> inline ostream &operator<<(ostream&out,const SmartPointer<T>&s)
{if( s.ptr() )return out << *s;else return out <<"nullpointer";}


template<class T>
class SmartPointerConst
{
 protected:
  const T*p;
 public:
  SmartPointerConst(const T*_p=0) 
    : p(_p) {}
  inline const T&operator*() const 
    {return *p;}
  inline const T*operator->() const 
    {return p;}
  inline operator bool() const
    {return p!=0;}
  inline const T*ptr() const
    { return p; }
};
template<class T> inline ostream &operator<<(ostream&out,const SmartPointerConst<T>&s)
{if( s.ptr() )return out << *s;else return out <<"nullpointer";}

template <class T>
class UP : public SmartPointer<T>
{
 public:
  UP(T*_p=0) 
    : SmartPointer<T>(_p) {}
};
template<class T> inline bool operator==(const UP<T>&s1,const UP<T>&s2)
{return s1.ptr()==s2.ptr();}
template<class T>  inline bool operator<(const UP<T>&s1,const UP<T>&s2)
{return s1.ptr() < s2.ptr();}
template<class T> inline int Hash(const UP<T> &wp)
{if(wp.ptr())return Hash(*wp);else return 0;}


template <class T>
class UPConst : public SmartPointerConst<T>
{
 public:
  UPConst(const T*_p=0) 
    : SmartPointerConst<T>(_p) {}
};
template<class T> inline bool operator==(const UPConst<T>&s1,const UPConst<T>&s2)
{return s1.ptr()==s2.ptr();}
template<class T> inline bool operator<(const UPConst<T>&s1,const UPConst<T>&s2)
{return s1.ptr()<s2.ptr();}
template<class T> inline int Hash(const UPConst<T> &wp)
{if(wp.ptr())return Hash(*wp);else return 0;}

	
template <class T>
class MP : public SmartPointer<T>
{
 public:
  MP(T*_p=0) 
    : SmartPointer<T>(_p) {}
};
template <class T> inline bool operator==(const MP<T>&s1,const MP<T>&s2)
{assert(s1);assert(s2);return *s1==*s2;}
template <class T> inline bool operator<(const MP<T>&s1,const MP<T>&s2)
{assert(s1);assert(s2);return *s1 < *s2;}
template <class T> inline int Hash(const MP<T> &wp)
{if(wp.ptr())return Hash(*wp);else return 0;}


template <class T>
class MPConst : public SmartPointerConst<T>
{
 public:
  MPConst(const T*_p=0) 
    : SmartPointerConst<T>(_p) {}
};
template <class T> inline bool operator==(const MPConst<T>&s1,const MPConst<T>&s2)
{assert(s1);assert(s2);return *s1== *s2;}
template <class T> inline bool operator<(const MPConst<T>&s1,const MPConst<T>&s2)
{assert(s1);assert(s2);return *s1 < *s2;}
template <class T> inline int Hash(const MPConst<T> &wp)
{if(wp.ptr())return Hash(*wp);else return 0;}


template <class T> 
class DELP : public SmartPointer<T>
{
 private:
  DELP(const DELP<T>&x);
 public:
  const DELP<T>&operator=(DELP<T>&x)
  {
    delete this->p;
    this->p=x.p;x.p=0;
    return *this;
  }

  ~DELP()
    { delete this->p;this->p=0;}
  DELP(T*_p=0) 
    : SmartPointer<T>(_p) {}
  void set(T*_p)
    {
      delete this->p;
      this->p=_p;
    }
  friend bool operator==(const DELP<T>&s1,const DELP<T>&s2)
    {
      return *(s1.p)== *(s2.p);
    }
  friend bool operator<(const DELP<T>&s1,const DELP<T>&s2)
    {
      return *(s1.p) < *(s2.p);
    }
  friend inline int Hash(const DELP<T> &wp)
    {
      if(wp.p)
	return Hash(*wp.p);
      else 
	return 0;
    }
};
#endif