aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Common
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/Common')
-rw-r--r--src/libjin/Common/Object.cpp32
-rw-r--r--src/libjin/Common/Object.h49
-rw-r--r--src/libjin/Common/Singleton.hpp1
-rw-r--r--src/libjin/Common/StringMap.hpp143
-rw-r--r--src/libjin/Common/utf8.cpp42
-rw-r--r--src/libjin/Common/utf8.h31
6 files changed, 298 insertions, 0 deletions
diff --git a/src/libjin/Common/Object.cpp b/src/libjin/Common/Object.cpp
new file mode 100644
index 0000000..6c3b667
--- /dev/null
+++ b/src/libjin/Common/Object.cpp
@@ -0,0 +1,32 @@
+// LOVE
+#include "Object.h"
+
+namespace jin
+{
+
+ Object::Object()
+ : count(1)
+ {
+ }
+
+ Object::~Object()
+ {
+ }
+
+ int Object::getReferenceCount() const
+ {
+ return count;
+ }
+
+ void Object::retain()
+ {
+ ++count;
+ }
+
+ void Object::release()
+ {
+ if (--count <= 0)
+ delete this;
+ }
+
+} // love
diff --git a/src/libjin/Common/Object.h b/src/libjin/Common/Object.h
new file mode 100644
index 0000000..9ac1b5a
--- /dev/null
+++ b/src/libjin/Common/Object.h
@@ -0,0 +1,49 @@
+#ifndef __JIN_COMMON_OBJECT_H
+#define __JIN_COMMON_OBJECT_H
+
+namespace jin
+{
+
+ class Object
+ {
+ private:
+
+ // The reference count.
+ int count;
+
+ public:
+
+ /**
+ * Constructor. Sets reference count to one.
+ **/
+ Object();
+
+ /**
+ * Destructor.
+ **/
+ virtual ~Object() = 0;
+
+ /**
+ * Gets the reference count of this Object.
+ * @returns The reference count.
+ **/
+ int getReferenceCount() const;
+
+ /**
+ * Retains the Object, i.e. increases the
+ * reference count by one.
+ **/
+ void retain();
+
+ /**
+ * Releases one reference to the Object, i.e. decrements the
+ * reference count by one, and potentially deletes the Object
+ * if there are no more references.
+ **/
+ void release();
+
+ }; // Object
+
+}
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Common/Singleton.hpp b/src/libjin/Common/Singleton.hpp
index c3ce467..48cd5bc 100644
--- a/src/libjin/Common/Singleton.hpp
+++ b/src/libjin/Common/Singleton.hpp
@@ -3,6 +3,7 @@
namespace jin
{
+
template<class T>
class Singleton
{
diff --git a/src/libjin/Common/StringMap.hpp b/src/libjin/Common/StringMap.hpp
new file mode 100644
index 0000000..bebd94d
--- /dev/null
+++ b/src/libjin/Common/StringMap.hpp
@@ -0,0 +1,143 @@
+#ifndef __JIN_COMMON_SREINGMAP_H
+#define __JIN_COMMON_SREINGMAP_H
+
+namespace jin
+{
+
+ template<typename T, unsigned SIZE>
+ class StringMap
+ {
+ private:
+
+ struct Record
+ {
+ const char * key;
+ T value;
+ bool set;
+ Record() : set(false) {}
+ };
+
+ const static unsigned MAX = SIZE * 2;
+
+ Record records[MAX];
+ const char * reverse[SIZE];
+
+ public:
+
+ struct Entry
+ {
+ const char * key;
+ T value;
+ };
+
+ StringMap(Entry * entries, unsigned num)
+ {
+
+ for (unsigned i = 0; i < SIZE; ++i)
+ reverse[i] = 0;
+
+ unsigned n = num / sizeof(Entry);
+
+ for (unsigned i = 0; i < n; ++i)
+ {
+ add(entries[i].key, entries[i].value);
+ }
+ }
+
+ bool streq(const char * a, const char * b)
+ {
+ while (*a != 0 && *b != 0)
+ {
+ if (*a != *b)
+ return false;
+ ++a;
+ ++b;
+ }
+
+ return (*a == 0 && *b == 0);
+ }
+
+ bool find(const char * key, T & t)
+ {
+ //unsigned str_hash = djb2(key);
+
+ for (unsigned i = 0; i < MAX; ++i)
+ {
+ //unsigned str_i = (str_hash + i) % MAX; //this isn't used, is this intentional?
+
+ if (records[i].set && streq(records[i].key, key))
+ {
+ t = records[i].value;
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ bool find(T key, const char *& str)
+ {
+ unsigned index = (unsigned)key;
+
+ if (index >= SIZE)
+ return false;
+
+ if (reverse[index] != 0)
+ {
+ str = reverse[index];
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ bool add(const char * key, T value)
+ {
+ unsigned str_hash = djb2(key);
+ bool inserted = false;
+
+ for (unsigned i = 0; i < MAX; ++i)
+ {
+ unsigned str_i = (str_hash + i) % MAX;
+
+ if (!records[str_i].set)
+ {
+ inserted = true;
+ records[str_i].set = true;
+ records[str_i].key = key;
+ records[str_i].value = value;
+ break;
+ }
+ }
+
+ unsigned index = (unsigned)value;
+
+ if (index >= SIZE)
+ {
+ printf("\nConstant %s out of bounds with %i!\n", key, index);
+ return false;
+ }
+
+ reverse[index] = key;
+
+ return inserted;
+ }
+
+ unsigned djb2(const char * key)
+ {
+ unsigned hash = 5381;
+ int c;
+
+ while ((c = *key++))
+ hash = ((hash << 5) + hash) + c;
+
+ return hash;
+ }
+
+ }; // StringMap
+
+}
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Common/utf8.cpp b/src/libjin/Common/utf8.cpp
new file mode 100644
index 0000000..f21a0d9
--- /dev/null
+++ b/src/libjin/Common/utf8.cpp
@@ -0,0 +1,42 @@
+#include "../modules.h"
+#if JIN_OS == JIN_WINDOWS
+
+#include "utf8.h"
+
+namespace jin
+{
+
+ std::string to_utf8(LPCWSTR wstr)
+ {
+ size_t wide_len = wcslen(wstr) + 1;
+
+ // Get size in UTF-8.
+ int utf8_size = WideCharToMultiByte(CP_UTF8, 0, wstr, wide_len, 0, 0, 0, 0);
+
+ char * utf8_str = new char[utf8_size];
+
+ // Convert to UTF-8.
+ int ok = WideCharToMultiByte(CP_UTF8, 0, wstr, wide_len, utf8_str, utf8_size, 0, 0);
+
+ if (!ok)
+ {
+ delete[] utf8_str;
+ }
+
+ return ok ? std::string(utf8_str) : std::string();
+ }
+
+ void replace_char(std::string & str, char find, char replace)
+ {
+ int length = str.length();
+
+ for (int i = 0; i<length; i++)
+ {
+ if (str[i] == find)
+ str[i] = replace;
+ }
+ }
+
+} // jins
+
+#endif // JIN_OS == JIN_WINDOWS \ No newline at end of file
diff --git a/src/libjin/Common/utf8.h b/src/libjin/Common/utf8.h
new file mode 100644
index 0000000..7f26841
--- /dev/null
+++ b/src/libjin/Common/utf8.h
@@ -0,0 +1,31 @@
+#ifndef __JIN_COMMON_UTF8_H
+#define __JIN_COMMON_UTF8_H
+
+#include "../modules.h"
+#if JIN_OS == JIN_WINDOWS
+
+#include <string>
+#include <windows.h>
+
+namespace jin
+{
+
+ /**
+ * Convert the wide string to a UTF-8 encoded string.
+ * @param wstr The wide-char string.
+ * @return A UTF-8 string.
+ **/
+ std::string to_utf8(LPCWSTR wstr);
+
+ /**
+ * Replace all occurences of 'find' with 'replace' in a string.
+ * @param str The string to modify.
+ * @param find The character to match.
+ * @param replace The character to replace matches.
+ **/
+ void replace_char(std::string & str, char find, char replace);
+
+}
+
+#endif // JIN_OS == JIN_WINDOWS
+#endif // __JIN_COMMON_UTF8_H \ No newline at end of file