/******************************************************************************1Copyright (C) 2007 Joel B. Mohler <[email protected]>23Distributed under the terms of the GNU General Public License (GPL)4as published by the Free Software Foundation; either version 2 of5the License, or (at your option) any later version.6http://www.gnu.org/licenses/78******************************************************************************/910/**11* @file ccobject.h12*13* @author Joel B. Mohler <[email protected]>14*15* @brief These functions provide assistance for constructing and destructing16* C++ objects from pyrex.17*18*/1920#ifndef __SAGE_CCOBJECT_H__21#define __SAGE_CCOBJECT_H__2223#ifdef __cplusplus2425#include <iostream>26#include <sstream>2728/* Ok, we are in C++ mode. Lets define some templated functions for construction29and destruction of allocated memory chunks. */3031/* Allocate and Construct */32template <class T>33T* New(){34return new T();35}3637/* Construct */38template <class T>39T* Construct(void* mem){40return new(mem) T();41}4243/* Construct with one parameter */44template <class T, class P>45T* Construct_p(void* mem, const P &d){46return new(mem) T(d);47}4849/* Construct with two parameters */50template <class T, class P, class Q>51T* Construct_pp(void* mem, const P &d, const Q &e){52return new(mem) T(d, e);53}5455/* Construct with three parameters */56template <class T, class P, class Q, class R>57T* Construct_ppp(void* mem, const P &d, const Q &e, const R &f){58return new(mem) T(d, e, f);59}6061/* Construct with four parameters */62template <class T, class P, class Q, class R, class S>63T* Construct_pppp(void* mem, const P &d, const Q &e, const R &f, const S &g){64return new(mem) T(d, e, f, g);65}6667/* Destruct */68template <class T>69void Destruct(T* mem){70mem->~T();71}7273/* Deallocate and Destruct */74template <class T>75void Delete(T* mem){76delete mem;77}7879template <class T>80bool _equal(T lhs, T rhs)81{82return lhs == rhs;83}8485template <class T>86void _from_str(T* dest, const char* src){87std::istringstream out(src);88out >> *dest;89}9091template <class T>92void _from_str_len(T* dest, const char* src, unsigned int len){93std::istringstream out(std::string(src, len));94out >> *dest;95}9697template <class T>98PyObject* _to_PyString(const T *x)99{100std::ostringstream instore;101instore << (*x);102std::string instr = instore.str();103// using PyString_FromString truncates the output if whitespace is104// encountered so we use Py_BuildValue and specify the length105return Py_BuildValue("s#",instr.c_str(), instr.size());106}107108#endif109110#endif // ndef __SAGE_CCOBJECT_H__111112113114115