source: branches/stable/CONVERTALN/refs.h

Last change on this file was 16763, checked in by westram, 6 years ago
File size: 1.7 KB
Line 
1#ifndef REFS_H
2#define REFS_H
3
4#ifndef INPUT_FORMAT_H
5#include "input_format.h"
6#endif
7#ifndef RDP_INFO_H
8#include "rdp_info.h"
9#endif
10
11template <typename REF>
12class Refs : virtual Noncopyable {
13    REF   *ref;
14    int  size;
15public:
16    Refs() {
17        ref  = NULp;
18        size = 0;
19    }
20    ~Refs() {
21        delete [] ref;
22    }
23
24    void resize(int new_size) {
25        ca_assert(new_size >= size);
26        if (new_size>size) {
27            REF *new_ref = new REF[new_size];
28            for (int i = 0; i<size; ++i) {
29                new_ref[i] = ref[i];
30            }
31            delete [] ref;
32            ref  = new_ref;
33            size = new_size;
34        }
35    }
36
37    int get_count() const { return size; }
38
39    const REF& get_ref(int num) const {
40        ca_assert(num >= 0);
41        ca_assert(num < get_count());
42        return ref[num];
43    }
44    REF& get_ref(int num) {
45        ca_assert(num >= 0);
46        ca_assert(num < get_count());
47        return ref[num];
48    }
49};
50
51template<typename REF>
52class RefContainer {
53    Refs<REF> refs;
54
55public:
56    void reinit_refs() { INPLACE_RECONSTRUCT(Refs<REF>, &refs); }
57    void resize_refs(int new_size) { refs.resize(new_size); }
58    int get_refcount() const  { return refs.get_count(); }
59    bool has_refs() const  { return get_refcount() > 0; }
60    const REF& get_ref(int num) const { return refs.get_ref(num); }
61    const REF& get_latest_ref() const { return get_ref(get_refcount()-1); }
62    REF& get_ref(int num) { return refs.get_ref(num); }
63    REF& get_latest_ref() { return get_ref(get_refcount()-1); }
64    REF& get_new_ref() { resize_refs(get_refcount()+1); return get_latest_ref(); }
65};
66
67#else
68#error refs.h included twice
69#endif // REFS_H
Note: See TracBrowser for help on using the repository browser.