~pythoneers/ubuntu/lucid/pycxx/ltsppa

« back to all changes in this revision

Viewing changes to Src/Python3/cxxsupport.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-01-03 16:30:10 UTC
  • mfrom: (1.1.9 upstream) (2.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100103163010-ooqjecsrucfafdwo
Tags: 6.1.1-2
Install missing header files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//-----------------------------------------------------------------------------
 
2
//
 
3
// Copyright (c) 1998 - 2007, The Regents of the University of California
 
4
// Produced at the Lawrence Livermore National Laboratory
 
5
// All rights reserved.
 
6
//
 
7
// This file is part of PyCXX. For details,see http://cxx.sourceforge.net/. The
 
8
// full copyright notice is contained in the file COPYRIGHT located at the root
 
9
// of the PyCXX distribution.
 
10
//
 
11
// Redistribution  and  use  in  source  and  binary  forms,  with  or  without
 
12
// modification, are permitted provided that the following conditions are met:
 
13
//
 
14
//  - Redistributions of  source code must  retain the above  copyright notice,
 
15
//    this list of conditions and the disclaimer below.
 
16
//  - Redistributions in binary form must reproduce the above copyright notice,
 
17
//    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
 
18
//    documentation and/or materials provided with the distribution.
 
19
//  - Neither the name of the UC/LLNL nor  the names of its contributors may be
 
20
//    used to  endorse or  promote products derived from  this software without
 
21
//    specific prior written permission.
 
22
//
 
23
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
 
24
// AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
 
25
// IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
 
26
// ARE  DISCLAIMED.  IN  NO  EVENT  SHALL  THE  REGENTS  OF  THE  UNIVERSITY OF
 
27
// CALIFORNIA, THE U.S.  DEPARTMENT  OF  ENERGY OR CONTRIBUTORS BE  LIABLE  FOR
 
28
// ANY  DIRECT,  INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR CONSEQUENTIAL
 
29
// DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
 
30
// SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
 
31
// CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
 
32
// LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
 
33
// OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 
34
// DAMAGE.
 
35
//
 
36
//-----------------------------------------------------------------------------
 
37
 
 
38
#include "CXX/Objects.hxx"
 
39
namespace Py 
 
40
{
 
41
 
 
42
Py_UNICODE unicode_null_string[1] = { 0 };
 
43
 
 
44
Type Object::type() const
 
45
 
46
    return Type( PyObject_Type( p ), true );
 
47
}
 
48
 
 
49
String Object::str() const
 
50
{
 
51
    return String( PyObject_Str( p ), true );
 
52
}
 
53
 
 
54
String Object::repr() const
 
55
 
56
    return String( PyObject_Repr( p ), true );
 
57
}
 
58
 
 
59
std::string Object::as_string() const
 
60
{
 
61
    return static_cast<std::string>( str() );
 
62
}
 
63
 
 
64
List Object::dir() const
 
65
{
 
66
    return List( PyObject_Dir( p ), true );
 
67
}
 
68
 
 
69
bool Object::isType( const Type &t ) const
 
70
 
71
    return type().ptr() == t.ptr();
 
72
}
 
73
 
 
74
Char::operator String() const
 
75
{
 
76
    return String( ptr() );
 
77
}
 
78
 
 
79
String Bytes::decode( const char *encoding, const char *error )
 
80
{
 
81
    return String( PyUnicode_FromEncodedObject( ptr(), encoding, error ), true );
 
82
}
 
83
 
 
84
// Object compares
 
85
bool operator==( const Object &o1, const Object &o2 )
 
86
{
 
87
    int k = PyObject_RichCompareBool( *o1, *o2, Py_EQ );
 
88
    if( PyErr_Occurred() )
 
89
        throw Exception();
 
90
    return k != 0;
 
91
}
 
92
 
 
93
bool operator!=( const Object &o1, const Object &o2 )
 
94
{
 
95
    int k = PyObject_RichCompareBool( *o1, *o2, Py_NE );
 
96
    if( PyErr_Occurred() )
 
97
        throw Exception();
 
98
    return k != 0;
 
99
 
 
100
}
 
101
 
 
102
bool operator>=( const Object &o1, const Object &o2 )
 
103
{
 
104
    int k = PyObject_RichCompareBool( *o1, *o2, Py_GE );
 
105
    if( PyErr_Occurred() )
 
106
        throw Exception();
 
107
    return k != 0;
 
108
}
 
109
 
 
110
bool operator<=( const Object &o1, const Object &o2 )
 
111
{
 
112
    int k = PyObject_RichCompareBool( *o1, *o2, Py_LE );
 
113
    if( PyErr_Occurred() )
 
114
        throw Exception();
 
115
    return k != 0;
 
116
}
 
117
 
 
118
bool operator<( const Object &o1, const Object &o2 )
 
119
{
 
120
    int k = PyObject_RichCompareBool( *o1, *o2, Py_LT );
 
121
    if( PyErr_Occurred() )
 
122
        throw Exception();
 
123
    return k != 0;
 
124
}
 
125
 
 
126
bool operator>( const Object &o1, const Object &o2 )
 
127
{
 
128
    int k = PyObject_RichCompareBool( *o1, *o2, Py_GT );
 
129
    if( PyErr_Occurred() )
 
130
        throw Exception();
 
131
    return k != 0;
 
132
}
 
133
 
 
134
// iterator compares
 
135
bool operator==( const Sequence::iterator &left, const Sequence::iterator &right )
 
136
{
 
137
    return left.eql( right );
 
138
}
 
139
 
 
140
bool operator!=( const Sequence::iterator &left, const Sequence::iterator &right )
 
141
{
 
142
    return left.neq( right );
 
143
}
 
144
 
 
145
bool operator<( const Sequence::iterator &left, const Sequence::iterator &right )
 
146
{
 
147
    return left.lss( right );
 
148
}
 
149
 
 
150
bool operator>( const Sequence::iterator &left, const Sequence::iterator &right )
 
151
{
 
152
    return left.gtr( right );
 
153
}
 
154
 
 
155
bool operator<=( const Sequence::iterator &left, const Sequence::iterator &right )
 
156
{
 
157
    return left.leq( right );
 
158
}
 
159
 
 
160
bool operator>=( const Sequence::iterator &left, const Sequence::iterator &right )
 
161
{
 
162
    return left.geq( right );
 
163
}
 
164
 
 
165
// const_iterator compares
 
166
bool operator==( const Sequence::const_iterator &left, const Sequence::const_iterator &right )
 
167
{
 
168
    return left.eql( right );
 
169
}
 
170
 
 
171
bool operator!=( const Sequence::const_iterator &left, const Sequence::const_iterator &right )
 
172
{
 
173
    return left.neq( right );
 
174
}
 
175
 
 
176
bool operator<( const Sequence::const_iterator &left, const Sequence::const_iterator &right )
 
177
{
 
178
    return left.lss( right );
 
179
}
 
180
 
 
181
bool operator>( const Sequence::const_iterator &left, const Sequence::const_iterator &right )
 
182
{
 
183
    return left.gtr( right );
 
184
}
 
185
 
 
186
bool operator<=( const Sequence::const_iterator &left, const Sequence::const_iterator &right )
 
187
{
 
188
    return left.leq( right );
 
189
}
 
190
 
 
191
bool operator>=( const Sequence::const_iterator &left, const Sequence::const_iterator &right )
 
192
{
 
193
    return left.geq( right );
 
194
}
 
195
 
 
196
// For mappings:
 
197
bool operator==( const Mapping::iterator &left, const Mapping::iterator &right )
 
198
{
 
199
    return left.eql( right );
 
200
}
 
201
 
 
202
bool operator!=( const Mapping::iterator &left, const Mapping::iterator &right )
 
203
{
 
204
    return left.neq( right );
 
205
}
 
206
 
 
207
// now for const_iterator
 
208
bool operator==( const Mapping::const_iterator &left, const Mapping::const_iterator &right )
 
209
{
 
210
    return left.eql( right );
 
211
}
 
212
 
 
213
bool operator!=( const Mapping::const_iterator &left, const Mapping::const_iterator &right )
 
214
{
 
215
    return left.neq( right );
 
216
}
 
217
 
 
218
// TMM: 31May'01 - Added the #ifndef so I can exclude iostreams.
 
219
#ifndef CXX_NO_IOSTREAMS
 
220
// output
 
221
 
 
222
std::ostream &operator<<( std::ostream &os, const Object &ob )
 
223
{
 
224
    return( os << static_cast<std::string>( ob.str() ) );
 
225
}  
 
226
#endif
 
227
 
 
228
} // Py