ShlubluLib  v0.5
ShlubluLib is a lightweight, modular, general purpose, open-source C++ library for Linux and Windows.
Classes | Public Member Functions | List of all members
shlublu::Python::ObjectHandler Class Reference

Handler of CPython object pointers used by Python to handle references counts. More...

#include <Python_ObjectHandler.h>

Classes

class  Hasher
 Key hasher to be used with unordered containers such as std::unordered_map or std::unordered_set. More...
 

Public Member Functions

 ObjectHandler ()
 Empty constructor. More...
 
 ObjectHandler (ObjectHandler const &src)
 Copy constructor. More...
 
 ObjectHandler (ObjectHandler &&src) noexcept
 Move constructor. More...
 
 ObjectHandler (PyObject *pyObj)
 PyObject* constructor. More...
 
ObjectHandleroperator= (ObjectHandler src) noexcept
 Assignment operator. More...
 
void swap (ObjectHandler &other) noexcept
 Exchanges two instances. More...
 
PyObject * get () const
 Returns the object pointer this handler encapsulates. More...
 
uint64_t id () const
 Returns the ID of the use case represented by this handler. More...
 
 operator PyObject * () const
 PyObject* cast operator. More...
 

Detailed Description

Handler of CPython object pointers used by Python to handle references counts.

An instance represents a specific use of a given CPython object and is identified by an ID unique to this use case.

A given CPython object can be involved in several instances should it have several use cases at a given point of time.

Two copies of a same handler have the same object pointers and the same ID as they refer to the same use case.

ID is never zero but for empty instances.

Conversion from and to PyObject * is silent.

See also
PyObject

Typical examples of use

#include <iostream>
using namespace shlublu;
int main(int, char* argv[])
{
int retCode(0);
try
{
Python::init(*argv);
Python::execute("myFloat = 5.5");
Python::ObjectHandler const& myFloat(Python::object(Python::moduleMain, "myFloat")); // Type 'Python::ObjectHandler const&' is explicit for this example though it is best to use `const auto`
std::cout << PyFloat_AsDouble(myFloat) << std::endl; // 'Python::ObjectHandler' converts silently to CPython's 'PyObject *'
const double anotherFloat(3.3);
Python::controlArgument(PyFloat_FromDouble(anotherFloat)); // 'Python::ObjectHandler' is created silently from the CPython's 'PyObject *'
// Python::shutdown() will dispose of this reference to the Python object created from anotherFloat
}
catch (Python::BindingException const& e)
{
std::cerr << "ERROR: " << e.what() << std::endl;
retCode = -1;
}
return retCode;
}

Constructor & Destructor Documentation

◆ ObjectHandler() [1/4]

shlublu::Python::ObjectHandler::ObjectHandler ( )

Empty constructor.

Sets both object pointer and ID to zero.

◆ ObjectHandler() [2/4]

shlublu::Python::ObjectHandler::ObjectHandler ( ObjectHandler const &  src)

Copy constructor.

Both object pointer and ID are copied.

Parameters
srcthe instance to be copied.

◆ ObjectHandler() [3/4]

shlublu::Python::ObjectHandler::ObjectHandler ( ObjectHandler &&  src)
noexcept

Move constructor.

Both object pointer and ID are moved.

Parameters
srcthe instance to move in.

◆ ObjectHandler() [4/4]

shlublu::Python::ObjectHandler::ObjectHandler ( PyObject *  pyObj)

PyObject* constructor.

Typically used as an implicit constructor for objects returned by the CPython API. An ID is allocated automatically at creation time.

Parameters
pyObjthe CPython object pointer this instance will be assigned.
See also
PyObject

Member Function Documentation

◆ operator=()

ObjectHandler& shlublu::Python::ObjectHandler::operator= ( ObjectHandler  src)
noexcept

Assignment operator.

Should src be another instance, both object pointer and ID are copied. Should src be an object pointer, this pointer is copied and an ID is allocated automatically by an implied ObjectHandler.

Parameters
srcother instance or PyObject *
Returns
a reference to *this

◆ swap()

void shlublu::Python::ObjectHandler::swap ( ObjectHandler other)
noexcept

Exchanges two instances.

Both object pointer and ID are swaped. Should src be an object pointer, an implied ObjectHandler is created and assigned an ID.

Parameters
otherother instance to swap with

◆ get()

PyObject* shlublu::Python::ObjectHandler::get ( ) const

Returns the object pointer this handler encapsulates.

Returns
a CPython object pointer

◆ id()

uint64_t shlublu::Python::ObjectHandler::id ( ) const

Returns the ID of the use case represented by this handler.

Returns
a use case ID

◆ operator PyObject *()

shlublu::Python::ObjectHandler::operator PyObject * ( ) const

PyObject* cast operator.

Typically used as an implicit cast operator to pass instances of ObjectHandler to functions of the CPython API.

Returns
the CPython object pointer this handler encapsulates.
See also
PyObject

The documentation for this class was generated from the following file:
shlublu::Python::ObjectHandler
Handler of CPython object pointers used by Python to handle references counts.
Definition: Python_ObjectHandler.h:71
shlublu
shlublu::Python::object
ObjectHandler const & object(ObjectPointer scope, std::string const &objectName)
Retrieves an object by its name from a scope pointer.
Python.h
shlublu::Python::controlArgument
ObjectHandler const & controlArgument(ObjectHandler object)
Places an CPython object under control of Python.
shlublu::Python::execute
void execute(RawCode const &code)
Executes raw code.
shlublu::Python::moduleMain
const std::string moduleMain
Main module ("__main__").
shlublu::Python::init
void init(std::string const &programName, PathEntriesList const &pythonSysPath=PathEntriesList())
Initializes Python.