File: MolSupplier.h

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (97 lines) | stat: -rw-r--r-- 2,633 bytes parent folder | download | duplicates (2)
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
//
//  Copyright (C) 2005-2019 Greg Landrumm and Rational Discovery LLC
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
#include <RDGeneral/export.h>
#ifndef RD_WRAP_MOLSUPPLIER_H
#define RD_WRAP_MOLSUPPLIER_H
//! Template functions for wrapping suppliers as python iterators.

#include <RDBoost/python.h>
#include <GraphMol/RDKitBase.h>
#include <RDGeneral/FileParseException.h>

namespace RDKit {
// Note that this returns a pointer to the supplier itself, so be careful
// that it doesn't get deleted by python!
template <typename T>
T *MolSupplIter(T *suppl) {
  suppl->reset();
  return suppl;
}

template <typename T>
ROMol *MolForwardSupplNext(T *suppl) {
  ROMol *res = nullptr;
  if (!suppl->atEnd()) {
    try {
      res = suppl->next();
    } catch (const FileParseException &) {
      throw;
    } catch (...) {
      res = nullptr;
    }
  } else {
    PyErr_SetString(PyExc_StopIteration, "End of supplier hit");
    throw boost::python::error_already_set();
  }
  if (suppl->atEnd() && suppl->getEOFHitOnRead()) {
    PyErr_SetString(PyExc_StopIteration, "End of supplier hit");
    throw boost::python::error_already_set();
  }
  return res;
}

template <typename T>
ROMol *MolSupplNext(T *suppl) {
  ROMol *res = nullptr;
  if (!suppl->atEnd()) {
    try {
      res = suppl->next();
    } catch (const FileParseException &) {
      throw;
    } catch (...) {
      res = nullptr;
    }
  } else {
    PyErr_SetString(PyExc_StopIteration, "End of supplier hit");
    throw boost::python::error_already_set();
  }

  return res;
}  // namespace RDKit

template <typename T>
ROMol *MolSupplGetItem(T *suppl, int idx) {
  ROMol *res = nullptr;
  if (idx < 0) {
    idx = suppl->length() + idx;
    if (idx < 0) {
      PyErr_SetString(PyExc_IndexError, "invalid index");
      throw boost::python::error_already_set();
    }
  }
  try {
    res = (*suppl)[idx];
  } catch (...) {
    // it's kind of doofy that we can't just catch the FileParseException
    // that is thrown when we run off the end of the supplier, but it seems
    // that the cross-shared library exception handling thing is just too
    // hard for me as of boost 1.35.0 (also 1.34.1) and g++  4.1.x on linux
    // this approach works as well:
    if (suppl->atEnd()) {
      PyErr_SetString(PyExc_IndexError, "invalid index");
      throw boost::python::error_already_set();
    } else {
      res = nullptr;
    }
  }
  return res;
}
}  // namespace RDKit
#endif