From 1b43f4bb636e2e314d7ae72cf7158d9648ffceb0 Mon Sep 17 00:00:00 2001
From: Carl Philipp Klemm <philipp@uvos.xyz>
Date: Tue, 30 May 2023 14:24:35 +0200
Subject: [PATCH] add missing compcache files

---
 compcache.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++
 compcache.h   | 39 +++++++++++++++++++++++++++
 2 files changed, 113 insertions(+)
 create mode 100644 compcache.cpp
 create mode 100644 compcache.h

diff --git a/compcache.cpp b/compcache.cpp
new file mode 100644
index 0000000..3b64b40
--- /dev/null
+++ b/compcache.cpp
@@ -0,0 +1,74 @@
+#include "compcache.h"
+
+#include <string>
+#include <vector>
+#include <map>
+#include <complex>
+#include <dlfcn.h>
+
+using namespace eis;
+
+std::string eis::getTempdir()
+{
+	char* tmpEnv = getenv("TMP");
+	char* tempEnv = getenv("TEMP");
+	char* tempDirEnv = getenv("TEMPDIR");
+
+	std::filesystem::path path;
+	if(tmpEnv && std::string(tmpEnv).length() > 1)
+		path = tmpEnv;
+	else if(tempEnv && std::string(tempEnv).length() > 1)
+		path = tempEnv;
+	else if(tempDirEnv && std::string(tempDirEnv).length() > 1)
+		path = tempDirEnv;
+	else
+		path = "/tmp";
+	path = path/"eis_models";
+
+	if(!std::filesystem::is_directory(path))
+	{
+		if(!std::filesystem::create_directory(path))
+			throw std::runtime_error(path.string() +
+				"is not a directory and a directory can not be created at this locaion");
+	}
+
+	return path.string();
+}
+
+
+CompCache* CompCache::getInstance()
+{
+	if(!instance)
+		instance = new CompCache();
+	return instance;
+}
+
+bool CompCache::addObject(size_t uuid, const CompiledObject& object)
+{
+	CompiledObject* foundobject = getObject(uuid);
+	if(foundobject)
+		return false;
+
+	objects.insert({uuid, new CompiledObject(object)});
+	return true;
+}
+
+CompiledObject* CompCache::getObject(size_t uuid)
+{
+	auto search = objects.find(uuid);
+	if(search == objects.end())
+		return nullptr;
+	else
+		return search->second;
+}
+
+void CompCache::dropAllObjects()
+{
+	for(std::pair<size_t, CompiledObject*> object : objects)
+	{
+		dlclose(object.second->objectCode);
+		delete object.second;
+	}
+
+	objects.clear();
+}
diff --git a/compcache.h b/compcache.h
new file mode 100644
index 0000000..469385a
--- /dev/null
+++ b/compcache.h
@@ -0,0 +1,39 @@
+#include <string>
+#include <vector>
+#include <map>
+#include <complex>
+
+#include "eistype.h"
+
+namespace eis
+{
+
+std::string getTempdir();
+
+struct CompiledObject
+{
+	void* objectCode;
+	std::vector<std::complex<fvalue>>(*symbol)(const std::vector<fvalue>&, const std::vector<fvalue>&);
+};
+
+class CompCache
+{
+public:
+
+private:
+
+	inline static CompCache* instance = nullptr;
+	std::map<size_t, CompiledObject*> objects;
+	CompCache() {};
+
+public:
+
+	static CompCache* getInstance();
+	CompCache(const CompCache&) = delete;
+	CompCache& operator=(const CompCache&) = delete;
+	bool addObject(size_t uuid, const CompiledObject& object);
+	CompiledObject* getObject(size_t uuid);
+	void dropAllObjects();
+};
+
+}
-- 
GitLab