Saner handling if config.mk doesn't exist: use a default config.defaults.mk.
[wvstreams.git] / include / wverror.h
blobef38b13d06598e070822378f1145319a3cfee84f
1 /* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * A class for managing error numbers and strings.
6 */
7 #ifndef __WVERROR_H
8 #define __WVERROR_H
10 #include "wvstring.h"
12 /**
13 * A class for managing error numbers and strings.
15 * It can have either a system error value, like those defined
16 * in errno.h, or an arbitrary error string. In either case, it
17 * can return a string representation of the error message.
19 * This object is most useful for using as a base class of your own class,
20 * for historical/backwards compatibility reasons. Consider using a WvError
21 * instead, and making it a member of your class instead of a parent.
23 class WvErrorBase
25 protected:
26 int errnum;
27 WvString errstring;
29 public:
30 WvErrorBase()
31 { noerr(); }
32 virtual ~WvErrorBase();
34 /**
35 * By default, returns true if geterr() == 0.
36 * Might be overridden so that isok() == false even though no
37 * error code has been specified.
39 virtual bool isok() const
40 { return errnum == 0; }
42 /**
43 * If isok() is false, return the system error number corresponding to
44 * the error, -1 for a special error string (which you can obtain with
45 * errstr()) or 0 on end of file. If isok() is true, returns an
46 * undefined number.
47 */
48 virtual int geterr() const
49 { return errnum; }
50 virtual WvString errstr() const;
52 /**
53 * A replacement for the operating system ::strerror() function that
54 * can map more kinds of error strings (especially in win32).
56 static WvString strerror(int errnum);
58 /**
59 * Set the errnum variable -- we have an error. If called more than
60 * once, seterr() doesn't change the error code away from the previous
61 * one. That way, we remember the _original_ cause of our problems.
63 * Subclasses may want to override seterr(int) to shut themselves down
64 * (eg. WvStream::close()) when an error condition is set.
66 * Note that seterr(WvString) will call seterr(-1).
68 virtual void seterr(int _errnum);
69 void seterr(WvStringParm specialerr);
70 void seterr(WVSTRING_FORMAT_DECL)
71 { seterr(WvString(WVSTRING_FORMAT_CALL)); }
72 void seterr_both(int _errnum, WvStringParm specialerr);
73 void seterr_both(int _errnum, WVSTRING_FORMAT_DECL)
74 { seterr_both(_errnum, WvString(WVSTRING_FORMAT_CALL)); }
75 void seterr(const WvErrorBase &err);
77 /** Reset our error state - there's no error condition anymore. */
78 void noerr()
79 { errnum = 0; errstring = WvString::null; }
83 /**
84 * A variant of WvErrorBase suitable for embedding as a member of your own
85 * object, preferably called 'err'. It adds some extra convenience functions
86 * to remove function name redundancy, so you can say "obj.err.get()" instead
87 * of "obj.err.geterr()", for example.
89 class WvError : public WvErrorBase
91 public:
92 int get() const
93 { return geterr(); }
94 WvString str() const
95 { return errstr(); }
97 void set(int _errnum)
98 { seterr(_errnum); }
99 void set(WvStringParm specialerr)
100 { seterr(specialerr); }
101 void set(WVSTRING_FORMAT_DECL)
102 { seterr(WvString(WVSTRING_FORMAT_CALL)); }
103 void set_both(int _errnum, WvStringParm specialerr)
104 { seterr_both(_errnum, specialerr); }
105 void set(const WvErrorBase &err)
106 { seterr(err); }
108 void reset()
109 { noerr(); }
113 #endif // __WVERROR_H