summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/Exceptions.h18
-rw-r--r--include/Input.h38
-rw-r--r--include/LockSmith.h45
-rw-r--r--include/generic.h8
4 files changed, 73 insertions, 36 deletions
diff --git a/include/Exceptions.h b/include/Exceptions.h
index ac46e28..428e1be 100644
--- a/include/Exceptions.h
+++ b/include/Exceptions.h
@@ -176,24 +176,6 @@ class printer_t : public Base {
extern printer_t * printer;
-
-/*!
- The locker class, if it exists, will be used to produce interlocking
- inside the nested classes when needed. Define it if you're going to build
- a real threaded software with Baltisot.
-*/
-
-class locker_t : public Base {
- public:
- virtual void lock() = 0;
- virtual void unlock() = 0;
-};
-
-extern locker_t * locker;
-
-#define LOCK if (locker) locker->lock()
-#define UNLOCK if (locker) locker->unlock();
-
#include <BString.h>
#endif
diff --git a/include/Input.h b/include/Input.h
index 208b185..66fa839 100644
--- a/include/Input.h
+++ b/include/Input.h
@@ -24,6 +24,8 @@
#include <time.h>
#include <BString.h>
#include <Handle.h>
+#include <Atomic.h>
+#include <LockSmith.h>
enum ArchiveType {
ARCHIVE_BUILTIN = 0,
@@ -33,17 +35,17 @@ enum ArchiveType {
class Input : public Handle {
public:
Input(const String & = "") throw (GeneralException);
- Input(const Input &);
- virtual ~Input() { nb_input--; }
- virtual bool CanWrite() const;
- virtual bool CanRead() const;
+ Input(const Input & i) : Handle(i), n(i.n), size(i.size), date_modif(i.date_modif) { Atomic::Increment(&nb_input); }
+ virtual ~Input() { Atomic::Decrement(&nb_input); }
+ virtual bool CanWrite() const { return false; }
+ virtual bool CanRead() const { return true; }
virtual bool CanSeek() const;
virtual off_t seek(off_t, int = SEEK_SET) throw (GeneralException);
- virtual String GetName() const;
- virtual ssize_t GetSize() const;
- virtual time_t GetModif() const;
+ virtual String GetName() const { return n; }
+ virtual ssize_t GetSize() const { return size; }
+ virtual time_t GetModif() const { return date_modif; }
virtual void SetZ(int = 9) throw (GeneralException);
- static int GetNbInput();
+ static int GetNbInput() { return nb_input; }
struct openresults_t {
String name;
@@ -66,10 +68,10 @@ class Input : public Handle {
class Stdin_t : public Input {
public:
- Stdin_t();
+ Stdin_t() {}
virtual ~Stdin_t() {}
- virtual bool CanSeek() const;
- virtual String GetName() const;
+ virtual bool CanSeek() const { return false; }
+ virtual String GetName() const { return "Stdin"; }
};
extern Stdin_t Stdin;
@@ -78,10 +80,10 @@ class Archive : public Base {
public:
Archive(const String &, int = ARCHIVE_BUILTIN);
Archive(Handle *, int = ARCHIVE_BUILTIN);
- virtual ~Archive();
+ virtual ~Archive() throw(GeneralException) { throw GeneralException("Archives ain't meant to be deleted."); }
protected:
static Archive * inarchive(const String &);
- Handle * GetHandle();
+ Handle * GetHandle() { return archive; }
int open(const String &, Input::openresults_t *);
private:
void create() throw (GeneralException);
@@ -90,12 +92,11 @@ class Archive : public Base {
class FileTree : public Base {
public:
FileTree(const String & = "", size_t = 0, int = 0, FileTree * = 0);
- virtual ~FileTree();
int compute_ptrs(size_t = 0);
- FileTree * Father();
- FileTree * Child();
- FileTree * Next();
- FileTree * Prev();
+ FileTree * Father() { return father; }
+ FileTree * Child() { return child; }
+ FileTree * Next() { return next; }
+ FileTree * Prev() { return prev; }
String name;
int type;
size_t size;
@@ -109,6 +110,7 @@ class Archive : public Base {
int type;
Archive * next, * prev;
static Archive * header;
+ static iLock lock;
friend class Input;
};
diff --git a/include/LockSmith.h b/include/LockSmith.h
new file mode 100644
index 0000000..fe2dc02
--- /dev/null
+++ b/include/LockSmith.h
@@ -0,0 +1,45 @@
+#ifndef __LOCKSMITH_H__
+#define __LOCKSMITH_H__
+
+#include <Exceptions.h>
+#include <Atomic.h>
+
+/*!
+ The locker class, if it exists, will be used to produce interlocking
+ inside the nested classes when needed. Define it if you're going to build
+ a real threaded software with Baltisot.
+*/
+
+class locker_t : public Base {
+ public:
+ virtual void lock() {}
+ virtual void unlock() {}
+};
+
+class LockSmith : public Base {
+ public:
+ LockSmith() { GlobalLockSmith = this; }
+ static locker_t * SpawnLock() { return GlobalLockSmith->SpawnSpecificLock(); }
+ protected:
+ virtual locker_t * SpawnSpecificLock() { return new locker_t(); }
+ static LockSmith * GlobalLockSmith;
+};
+
+class iLock : public Base {
+ public:
+ iLock() { }
+ ~iLock() { if (locker) delete(locker); }
+ void lock() {
+ if (LIKELY(locker)) {
+ locker->lock();
+ } else {
+ locker = LockSmith::SpawnLock();
+ locker->lock();
+ }
+ }
+ void unlock() { locker->unlock(); }
+ private:
+ locker_t * locker;
+};
+
+#endif
diff --git a/include/generic.h b/include/generic.h
index 44aa291..ace03e8 100644
--- a/include/generic.h
+++ b/include/generic.h
@@ -26,6 +26,14 @@
#pragma warning(disable:4996 4244 4267)
#endif
+#ifdef __GNUC__
+#define LIKELY(x) __builtin_expect(!!(x), 1)
+#define UNLIKELY(x) __builtin_expect(!!(x), 0)
+#else
+#define LIKELY(x) (x)
+#define UNLIKELY(x) (x)
+#endif
+
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>