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
|
/*
* This file is part of libArcus
*
* Copyright (C) 2016 Ultimaker b.v. <a.hiemstra@ultimaker.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3.0 as published
* by the Free Software Foundation, either version 3 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 Lesser General Public License v3.0 for more details.
* You should have received a copy of the GNU Lesser General Public License v3.0
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Convert a python str object to a std::string.
%MappedType std::string
{
%TypeHeaderCode
#include <string>
%End
%ConvertFromTypeCode
// convert an std::string to a Python (unicode) string
PyObject* newstring;
newstring = PyUnicode_DecodeUTF8(sipCpp->c_str(), sipCpp->length(), NULL);
if(newstring == NULL) {
PyErr_Clear();
newstring = PyBytes_FromString(sipCpp->c_str());
}
return newstring;
%End
%ConvertToTypeCode
// Allow a Python string (or a unicode string) whenever a string is
// expected.
// If argument is a Unicode string, just decode it to UTF-8
// If argument is a Python string, assume it's UTF-8
if (sipIsErr == NULL)
return (PyBytes_Check(sipPy) || PyUnicode_Check(sipPy));
if (sipPy == Py_None)
{
*sipCppPtr = new std::string;
return 1;
}
if (PyUnicode_Check(sipPy))
{
PyObject* s = PyUnicode_AsEncodedString(sipPy, "UTF-8", "");
*sipCppPtr = new std::string(PyBytes_AS_STRING(s));
Py_DECREF(s);
return 1;
}
if (PyBytes_Check(sipPy))
{
*sipCppPtr = new std::string(PyBytes_AS_STRING(sipPy));
return 1;
}
return 0;
%End
};
// Convert a MessagePtr (aka std::shared_ptr<google::protobuf::Message>) to a PythonMessage*
%MappedType MessagePtr
{
%TypeHeaderCode
#include <memory>
#include "PythonMessage.h"
%End
%ConvertFromTypeCode
// Convert a Protobuf message to a Python object
if(!(*sipCpp))
{
PyErr_SetString(PyExc_ValueError, "Unknown message type");
return NULL;
}
const sipTypeDef* message_type = sipFindType("PythonMessage");
PythonMessage* message = new PythonMessage(*sipCpp);
sipTransferObj = Py_None;
PyObject* msg = sipConvertFromType(message, message_type, sipTransferObj);
if(!msg)
{
delete message;
return NULL;
}
return msg;
%End
%ConvertToTypeCode
// Convert a Python object to a Protobuf message
const sipTypeDef* message_type = sipFindType("PythonMessage");
if(sipIsErr == NULL)
{
return sipCanConvertToType(sipPy, message_type, SIP_NOT_NONE);
}
if(sipCanConvertToType(sipPy, message_type, SIP_NOT_NONE))
{
int iserr = 0;
int state = 0;
PythonMessage* message = reinterpret_cast<PythonMessage*>(sipConvertToType(sipPy, message_type, NULL, 0, &state, &iserr));
if(iserr != 0)
{
PyErr_SetString(PyExc_ValueError, "Could not convert to Message");
return 0;
}
MessagePtr msg = message->getSharedMessage();
*sipCppPtr = new MessagePtr(msg);
sipReleaseType(message, message_type, state);
}
return sipGetState(sipTransferObj);
%End
};
%UnitCode
#include "Types.h"
%End
namespace SocketState
{
enum SocketState
{
Initial,
Connecting,
Connected,
Opening,
Listening,
Closing,
Closed,
Error
};
};
|