|
| 1 | +/* python-exiv2 - Python interface to libexiv2 |
| 2 | + * http://github.com/jim-easterbrook/python-exiv2 |
| 3 | + * Copyright (C) 2025 Jim Easterbrook jim@jim-easterbrook.me.uk |
| 4 | + * |
| 5 | + * This file is part of python-exiv2. |
| 6 | + * |
| 7 | + * python-exiv2 is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * python-exiv2 is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with python-exiv2. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | + |
| 22 | +// Macro to wrap metadatum references so they can be invalidated |
| 23 | +%define METADATUM_REFERENCE(datum_type) |
| 24 | +%feature("python:slot", "tp_str", functype="reprfunc") |
| 25 | + datum_type##_pointer::__str__; |
| 26 | +%noexception datum_type##_pointer::operator==; |
| 27 | +%noexception datum_type##_pointer::operator!=; |
| 28 | +%noexception Exiv2::datum_type::operator==; |
| 29 | +%noexception Exiv2::datum_type::operator!=; |
| 30 | +%ignore datum_type##_pointer::##datum_type##_pointer; |
| 31 | +%ignore datum_type##_pointer::size; |
| 32 | +%ignore datum_type##_pointer::operator*; |
| 33 | +%ignore datum_type##_pointer::_invalidate; |
| 34 | +%feature("docstring") datum_type##_pointer " |
| 35 | +Python wrapper for an :class:`" #datum_type "` reference. It has most of |
| 36 | +the methods of :class:`" #datum_type "` allowing easy access to the |
| 37 | +data it points to." |
| 38 | +%inline %{ |
| 39 | +class datum_type##_pointer { |
| 40 | +private: |
| 41 | + Exiv2::datum_type* ptr; |
| 42 | + bool invalidated; |
| 43 | +public: |
| 44 | + datum_type##_pointer(Exiv2::datum_type* ptr) { |
| 45 | + this->ptr = ptr; |
| 46 | + invalidated = false; |
| 47 | + } |
| 48 | + // Dereference operator gives Python access to all datum methods |
| 49 | + Exiv2::datum_type* operator->() const { |
| 50 | + if (invalidated) |
| 51 | + throw std::runtime_error("datum_type pointer is invalid"); |
| 52 | + return ptr; |
| 53 | + } |
| 54 | + Exiv2::datum_type* operator*() const { return ptr; } |
| 55 | + bool operator==(const Exiv2::datum_type &other) const { |
| 56 | + return &other == ptr; |
| 57 | + } |
| 58 | + bool operator!=(const Exiv2::datum_type &other) const { |
| 59 | + return &other != ptr; |
| 60 | + } |
| 61 | + std::string __str__() { |
| 62 | + if (invalidated) |
| 63 | + return "invalid pointer"; |
| 64 | + return "pointer<" + ptr->key() + ": " + ptr->print() + ">"; |
| 65 | + } |
| 66 | + // Invalidate pointer unilaterally |
| 67 | + void _invalidate() { invalidated = true; } |
| 68 | + // Invalidate pointer if what it points to has been deleted |
| 69 | + bool _invalidate(Exiv2::datum_type* deleted) { |
| 70 | + if (deleted == ptr) |
| 71 | + invalidated = true; |
| 72 | + return invalidated; |
| 73 | + } |
| 74 | + // Provide size() C++ method for buffer size check |
| 75 | + size_t size() { |
| 76 | + if (invalidated) |
| 77 | + return 0; |
| 78 | + return ptr->size(); |
| 79 | + } |
| 80 | +}; |
| 81 | +%} |
| 82 | +%extend Exiv2::datum_type { |
| 83 | + bool operator==(const Exiv2::datum_type &other) const { |
| 84 | + return &other == self; |
| 85 | + } |
| 86 | + bool operator!=(const Exiv2::datum_type &other) const { |
| 87 | + return &other != self; |
| 88 | + } |
| 89 | +} |
| 90 | +%typemap(in) const Exiv2::datum_type& { |
| 91 | + datum_type##_pointer* tmp = NULL; |
| 92 | + if (SWIG_IsOK(SWIG_ConvertPtr( |
| 93 | + $input, (void**)&tmp, $descriptor(datum_type##_pointer*), 0))) |
| 94 | + $1 = **tmp; |
| 95 | + else { |
| 96 | + $typemap(in, Exiv2::datum_type&) |
| 97 | + } |
| 98 | +} |
| 99 | +%typemap(out) Exiv2::datum_type& { |
| 100 | + $result = SWIG_NewPointerObj( |
| 101 | + SWIG_as_voidptr(new datum_type##_pointer($1)), |
| 102 | + $descriptor(datum_type##_pointer*), SWIG_POINTER_OWN); |
| 103 | +} |
| 104 | +%enddef // METADATUM_REFERENCE |
0 commit comments