summaryrefslogtreecommitdiff
path: root/includes/Exceptions.h
blob: 74ad938f82f4c5c631f40da103cf81a659d5045e (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef __EXCEPTIONS_H__
#define __EXCEPTIONS_H__
#ifdef __cplusplus

#include <stdio.h>
#include <unistd.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>

#define INLINE __inline__

class Base {
  public:
    static char * strdup(const char * s);
    static void * malloc(ssize_t s);
    static void * realloc(void * p, size_t s);
    static void * calloc(size_t n, size_t s);
    void * operator new(size_t s);
    void * operator new(size_t s, void * p);
    void operator delete(void * p);
    static void free(void *& p);
    static void free(char *& p);
    static void free(unsigned char *& p);
    static int pipe(int * p, int flag = 0);
    static pid_t fork();
    static void exit(int);
};

class String;

class GeneralException : public Base {
  public:
      GeneralException(String);
      GeneralException(const GeneralException &);
      ~GeneralException();
    const char * GetMsg() const;

  protected:
      GeneralException();
    char * msg;
    static char t[BUFSIZ];
};

char * xstrdup(const char *);
void * xmalloc(size_t) throw (GeneralException);
void xfree(void *&);
void xfree(char *&);
void xfree(unsigned char *&);
void * xrealloc(void *, size_t);
int xpipe(int *, int = 0) throw (GeneralException);
pid_t xfork() throw (GeneralException);
void xexit(int) throw (GeneralException);

INLINE char * Base::strdup(const char * s) {
    return xstrdup(s);
}

INLINE void * Base::malloc(ssize_t s) {
    return xmalloc(s);
}

INLINE void * Base::realloc(void * p, size_t s) {
    return xrealloc(p, s);
}

INLINE void * Base::calloc(size_t n, size_t s) {
    return xmalloc(n * s);
}

INLINE void * Base::operator new(size_t s) {
    return xmalloc(s);
}

INLINE void * Base::operator new(size_t s, void * p) {
    return memset(p, 0, s);
}

INLINE void Base::operator delete(void * p) {
    xfree(p);
}

INLINE void Base::free(void *& p) {
    xfree(p);
}

INLINE void Base::free(char *& p) {
    xfree(p);
}

INLINE void Base::free(unsigned char *& p) {
    xfree(p);
}

INLINE int Base::pipe(int * p, int flag) {
    return xpipe(p, flag);
}

INLINE pid_t Base::fork() {
    return xfork();
}

INLINE void Base::exit(int status) {
    xexit(status);
}

class MemoryException : public GeneralException {
  public:
      MemoryException(ssize_t);
};

class TaskNotFound : public GeneralException {
  public:
      TaskNotFound();
};

enum op_t {
  IO_WRITE = 1,
  IO_READ
};

class IOGeneral : public GeneralException {
  public:
      IOGeneral(String);
  protected:
      IOGeneral();
};

class IOException : public IOGeneral {
  public:
      IOException(String, op_t, ssize_t);
};

class IOAgain : public IOGeneral {
  public:
      IOAgain();
};

class TaskSwitch : public GeneralException {
  public:
      TaskSwitch();
};

class Exit : public GeneralException {
  public:
      Exit(int);
      int GetCode();
  private:
    int code;
};

#include <String.h>

#else
#error This only works with a C++ compiler
#endif
#endif