Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/c_lib/include/ccobject.h
4024 views
1
/******************************************************************************
2
Copyright (C) 2007 Joel B. Mohler <[email protected]>
3
4
Distributed under the terms of the GNU General Public License (GPL)
5
as published by the Free Software Foundation; either version 2 of
6
the License, or (at your option) any later version.
7
http://www.gnu.org/licenses/
8
9
******************************************************************************/
10
11
/**
12
* @file ccobject.h
13
*
14
* @author Joel B. Mohler <[email protected]>
15
*
16
* @brief These functions provide assistance for constructing and destructing
17
* C++ objects from pyrex.
18
*
19
*/
20
21
#ifndef __SAGE_CCOBJECT_H__
22
#define __SAGE_CCOBJECT_H__
23
24
#ifdef __cplusplus
25
26
#include <iostream>
27
#include <sstream>
28
29
/* Ok, we are in C++ mode. Lets define some templated functions for construction
30
and destruction of allocated memory chunks. */
31
32
/* Allocate and Construct */
33
template <class T>
34
T* New(){
35
return new T();
36
}
37
38
/* Construct */
39
template <class T>
40
T* Construct(void* mem){
41
return new(mem) T();
42
}
43
44
/* Construct with one parameter */
45
template <class T, class P>
46
T* Construct_p(void* mem, const P &d){
47
return new(mem) T(d);
48
}
49
50
/* Construct with two parameters */
51
template <class T, class P, class Q>
52
T* Construct_pp(void* mem, const P &d, const Q &e){
53
return new(mem) T(d, e);
54
}
55
56
/* Construct with three parameters */
57
template <class T, class P, class Q, class R>
58
T* Construct_ppp(void* mem, const P &d, const Q &e, const R &f){
59
return new(mem) T(d, e, f);
60
}
61
62
/* Construct with four parameters */
63
template <class T, class P, class Q, class R, class S>
64
T* Construct_pppp(void* mem, const P &d, const Q &e, const R &f, const S &g){
65
return new(mem) T(d, e, f, g);
66
}
67
68
/* Destruct */
69
template <class T>
70
void Destruct(T* mem){
71
mem->~T();
72
}
73
74
/* Deallocate and Destruct */
75
template <class T>
76
void Delete(T* mem){
77
delete mem;
78
}
79
80
template <class T>
81
bool _equal(T lhs, T rhs)
82
{
83
return lhs == rhs;
84
}
85
86
template <class T>
87
void _from_str(T* dest, const char* src){
88
std::istringstream out(src);
89
out >> *dest;
90
}
91
92
template <class T>
93
void _from_str_len(T* dest, const char* src, unsigned int len){
94
std::istringstream out(std::string(src, len));
95
out >> *dest;
96
}
97
98
template <class T>
99
PyObject* _to_PyString(const T *x)
100
{
101
std::ostringstream instore;
102
instore << (*x);
103
std::string instr = instore.str();
104
// using PyString_FromString truncates the output if whitespace is
105
// encountered so we use Py_BuildValue and specify the length
106
return Py_BuildValue("s#",instr.c_str(), instr.size());
107
}
108
109
#endif
110
111
#endif // ndef __SAGE_CCOBJECT_H__
112
113
114
115