Menu

[025cc2]: / DirectPython11 / DPUtils.hpp  Maximize  Restore  History

Download this file

132 lines (95 with data), 3.3 kB

  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
/*
Copyright (C) 2010, Heikki Salo
All rights reserved.
Distributed under the BSD license:
http://www.opensource.org/licenses/bsd-license.php
*/
#ifndef DP_UTILS_HPP
#define DP_UTILS_HPP
#include "stdafx.h"
typedef std::vector<D3D11_INPUT_ELEMENT_DESC> InputElementVector;
void ConvertLayoutDesc(python::object&, InputElementVector*);
python::object LayoutDescToPy(const InputElementVector&);
UINT GetElementByteSize(const InputElementVector&);
UINT GetFormatByteSize(DXGI_FORMAT, std::string* fstr=0);
void ConvertPyToC(python::object&, void*, const InputElementVector&);
python::list ConvertCToPy(void*, const InputElementVector&);
python::object CreateNamedTuple(const char* className, const char* attributes);
std::string GetLayoutFormatStr(const InputElementVector&);
void DPWarn(const char*);
inline XMVECTOR PyToXMV(const python::object& v)
{
return XMVectorSet(python::extract<float>(v[0]), python::extract<float>(v[1]),
python::extract<float>(v[2]), 0.0f);
}
//For Python 2.x - Python 3.x compability.
class pyunicode : boost::noncopyable {
public:
~pyunicode() { Py_XDECREF(value); }
pyunicode(python::object& argument) {
#if PY_VERSION_HEX >= 0x03000000
value = PyObject_Str(argument.ptr());
#else
value = PyObject_Unicode(argument.ptr());
#endif
if (value == 0)
DPThrow("Can't convert to unicode");
}
Py_ssize_t size() const { return PyUnicode_GET_SIZE(value); }
const Py_UNICODE* c_str() const { return PyUnicode_AS_UNICODE(value); }
const Py_UNICODE& operator [] (UINT index) const { return PyUnicode_AS_UNICODE(value)[index]; }
private:
PyObject* value;
};
//Basic smart pointer class and some specializations.
#ifndef STRICT
#error "scoped_object needs STRICT-handles."
#endif
template<typename T>
struct DPReleaser { };
template<>
struct DPReleaser<HFONT> {
void operator () (HFONT arg) { if (arg) DeleteObject(arg); }
};
//Assume COM-interface.
template<typename T>
struct DPReleaser<T*> {
void operator () (T* arg) { if (arg) arg->Release(); }
};
template<typename T>
class scoped_object {
public:
~scoped_object() { releaser(value); }
scoped_object(T v = T()) : value(v) { }
T get() { return value; }
T* operator & () { return &value; }
operator T () { return value; }
T operator -> () { return value; }
private:
T value;
DPReleaser<T> releaser;
const scoped_object& operator = (scoped_object&);
scoped_object(const scoped_object&);
};
//Simple formatter helper class.
class DPFormatter {
public:
template<typename T>
std::ostringstream& operator << (const T& value) {
stream << value;
return stream;
}
void raise() {
DPThrow(stream.str().c_str());
//PyErr_SetString(PyExc_RuntimeError, stream.str().c_str());
//python::throw_error_already_set();
}
void warn() {
if (PyErr_WarnEx(PyExc_RuntimeWarning, stream.str().c_str(), 1) == -1) {
python::throw_error_already_set();
}
}
private:
std::ostringstream stream;
};
#endif
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.