File: hdf_id_helper.cpp

package info (click to toggle)
libvbz-hdf-plugin 1.0.2-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,384 kB
  • sloc: cpp: 28,289; python: 392; ansic: 40; sh: 21; makefile: 19; xml: 16
file content (83 lines) | stat: -rw-r--r-- 1,757 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
#include "hdf_id_helper.h"

#define THROW(_ex) throw _ex

#define THROW_ON_ERROR(code) \
    if (code < 0) THROW(Exception())

namespace ont { namespace hdf5 {

static_assert(std::is_same<Id,hid_t>::value, "Mismatched ID types");

IdRef IdRef::claim(Id id)
{
    if (id < 0 || H5Iis_valid(id) <= 0) {
        THROW(Exception());
    }
    return IdRef(id);
}

IdRef IdRef::ref(Id id)
{
    assert(id >= 0);
    THROW_ON_ERROR(H5Iinc_ref(id));
    return IdRef(id);
}

IdRef IdRef::global_ref(Id id)
{
    assert(id >= 0);

    // Increment the index here - never decrementing it.
    THROW_ON_ERROR(H5Iinc_ref(id));
    return global(id);
}

IdRef::IdRef(IdRef const& other)
    : m_id(other.m_id)
    , m_is_global_constant(other.m_is_global_constant)
{
    if (!m_is_global_constant && m_id >= 0) {
        THROW_ON_ERROR(H5Iinc_ref(m_id));
    }
}

IdRef& IdRef::operator=(IdRef const& other)
{
    if (!other.m_is_global_constant && other.m_id >= 0) {
        THROW_ON_ERROR(H5Iinc_ref(other.m_id));
    }
    if (!m_is_global_constant && m_id >= 0) {
        auto result = H5Idec_ref(m_id);
        if (result < 0) {
            // this will be logged by the auto-logging code
            // (see install_error_function)
            assert(false);
        }
    }
    m_is_global_constant = other.m_is_global_constant;
    m_id = other.m_id;
    return *this;
}

IdRef::~IdRef()
{
    if (!m_is_global_constant && m_id >= 0) {
        auto result = H5Idec_ref(m_id);
        if (result < 0) {
            // this will be logged by the auto-logging code
            // (see install_error_function)
            assert(false);
        }
    }
}

int IdRef::ref_count() const
{
    if (m_id < 0) {
        return 0;
    }
    return H5Iget_ref(m_id);
}

}}