File: resource.cc

package info (click to toggle)
atom4 4.1-6
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 656 kB
  • ctags: 947
  • sloc: cpp: 4,461; makefile: 54; sh: 21; perl: 6
file content (77 lines) | stat: -rw-r--r-- 1,483 bytes parent folder | download | duplicates (6)
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
/*
 * Simple resource management base classes
 * Implementation file
 *
 * $Id$
 */

#include <stdlib.h>			// for NULL
#include "resource.h"


/* FUNCTIONS */

resource::resource() : ref(0) {
}

resource::~resource() {}

void resource::alloc() {
  if (ref==0) load();
  ref++;
}

void resource::dealloc() {
  if (ref>0) {
    ref--;
    if (ref==0) unload();
  } else {
    throw resource_exception("Resource already unloaded\n");
  }
}

resourceman::resourceman(int static_ids) : num_static_ids(static_ids) {
  int i;

  staticres = new resource*[static_ids];
  if (!staticres) throw resource_exception("Out of memory");

  for (i=0; i<num_static_ids; i++) {
    staticres[i] = NULL;
  }
}

resourceman::~resourceman() {
  int i;

  for (i=0; i<num_static_ids; i++) {
    if (staticres[i]) delete staticres[i];
  }
  delete staticres;
}

void resourceman::init_resource(int id, resource *r) {
  if (id<0 || id>=num_static_ids)
    throw resource_exception("@Bad resource ID: %d\n", id);

  if (!staticres[id]) {
    staticres[id] = r;
  } else {
    throw resource_exception("@Resource ID %d already initialized\n", id);
  }
}

resource *resourceman::allocate(int id) {
  if (id<0 || id>=num_static_ids)
    throw resource_exception("@Bad resource ID: %d\n", id);

  staticres[id]->alloc();
  return staticres[id];
}

void resourceman::deallocate(int id) {
  if (id<0 || id>=num_static_ids)
    throw resource_exception("@Bad resource ID: %d\n", id);

  staticres[id]->dealloc();
}