Initial structure
This commit is contained in:
Executable
+18
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// Created by imon on 2015-03-31.
|
||||
//
|
||||
|
||||
#ifndef DAYDREAM_ENUMCLASSHASH_H
|
||||
#define DAYDREAM_ENUMCLASSHASH_H
|
||||
|
||||
// http://stackoverflow.com/a/24847480/417018
|
||||
struct EnumClassHash
|
||||
{
|
||||
template <typename T>
|
||||
std::size_t operator()(T t) const
|
||||
{
|
||||
return static_cast<std::size_t>(t);
|
||||
}
|
||||
};
|
||||
|
||||
#endif //DAYDREAM_ENUMCLASSHASH_H
|
||||
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
This file is part of Daydream Engine.
|
||||
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
|
||||
|
||||
Daydream Engine is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Daydream Engine is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef ComponentFactory_h__
|
||||
#define ComponentFactory_h__
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
|
||||
namespace dd
|
||||
{
|
||||
|
||||
/** Generic object factory.
|
||||
|
||||
Creates dynamically allocated objects of base type T.
|
||||
@tparam T Object type.
|
||||
*/
|
||||
template <typename T>
|
||||
class Factory
|
||||
{
|
||||
public:
|
||||
/*void Register(std::string name, std::function<T(void)> factoryFunction)
|
||||
{
|
||||
m_FactoryFunctions[name] = factoryFunction;
|
||||
}*/
|
||||
|
||||
/** Register a custom factory function for a type with the factory.
|
||||
|
||||
@tparam T2 Object type of base type T.
|
||||
@param factoryFunction The lambda factory function.
|
||||
*/
|
||||
template <typename T2>
|
||||
void Register(std::function<T*(void)> factoryFunction)
|
||||
{
|
||||
m_FactoryFunctions[typeid(T2).name()] = factoryFunction;
|
||||
}
|
||||
|
||||
/** Register a type with the factory.
|
||||
|
||||
@tparam T2 Object type of base type T.
|
||||
*/
|
||||
template <typename T2>
|
||||
void Register()
|
||||
{
|
||||
m_FactoryFunctions[typeid(T2).name()] = []() { return new T2(); };
|
||||
m_CopyFunctions[typeid(T2).name()] = [](const T* t) { return new T2(*static_cast<const T2*>(t)); };
|
||||
}
|
||||
|
||||
/** Create an instance of a specific typename.
|
||||
|
||||
@param name Typename of type of base type T, as a string.
|
||||
@returns Pointer to the new object of type T.
|
||||
*/
|
||||
T* Create(std::string name)
|
||||
{
|
||||
auto it = m_FactoryFunctions.find(name);
|
||||
if (it != m_FactoryFunctions.end())
|
||||
{
|
||||
return it->second();
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
/** Create an instance of a specific type.
|
||||
|
||||
@tparam T2 Type of base type T.
|
||||
@returns Pointer to the new object of type T.
|
||||
*/
|
||||
template <typename T2>
|
||||
T* Create()
|
||||
{
|
||||
auto it = m_FactoryFunctions.find(typeid(T2).name());
|
||||
if (it != m_FactoryFunctions.end())
|
||||
{
|
||||
return it->second();
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
/** Create a copy of an existing object of a specific _type_ registered with the factory.
|
||||
|
||||
@param name Typename of type of base type T, as a string.
|
||||
@param toCopy The existing object to copy.
|
||||
@returns Pointer to the new object of type T2.
|
||||
*/
|
||||
T* Copy(std::string name, const T* toCopy)
|
||||
{
|
||||
auto it = m_CopyFunctions.find(name);
|
||||
if (it != m_CopyFunctions.end())
|
||||
{
|
||||
return it->second(toCopy);
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
/** Create a copy of an existing object of a specific _typename_ registered with the factory.
|
||||
|
||||
@tparam T2 Type of base type T.
|
||||
@param toCopy The existing object to copy.
|
||||
@returns Pointer to the new object of type T2.
|
||||
*/
|
||||
template <typename T2>
|
||||
T* Copy(const T* toCopy)
|
||||
{
|
||||
auto it = m_CopyFunctions.find(typeid(T2).name());
|
||||
if (it != m_CopyFunctions.end())
|
||||
{
|
||||
return it->second(toCopy);
|
||||
} else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<std::string, std::function<T*(void)>> m_FactoryFunctions;
|
||||
std::map<std::string, std::function<T*(const T*)>> m_CopyFunctions;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // ComponentFactory_h__
|
||||
Executable
+81
@@ -0,0 +1,81 @@
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/thread.hpp>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "Core/Util/Logging.h"
|
||||
|
||||
namespace std
|
||||
{
|
||||
template<> struct hash < boost::filesystem::path >
|
||||
{
|
||||
size_t operator()(const boost::filesystem::path& p) const
|
||||
{
|
||||
return boost::filesystem::hash_value(p);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
namespace dd
|
||||
{
|
||||
|
||||
class FileWatcher
|
||||
{
|
||||
public:
|
||||
enum class FileEventFlags;
|
||||
typedef std::function<void(std::string, FileEventFlags)> FileEventCallback_t;
|
||||
|
||||
FileWatcher();
|
||||
FileWatcher(std::string rootPath);
|
||||
~FileWatcher();
|
||||
|
||||
std::string RootPath() const { return m_RootPath.string(); }
|
||||
void AddWatch(std::string path, FileEventCallback_t callback);
|
||||
void Start();
|
||||
bool IsRunning() const { return m_IsRunning; }
|
||||
void Stop();
|
||||
void Check();
|
||||
|
||||
private:
|
||||
class Worker;
|
||||
|
||||
boost::filesystem::path m_RootPath;
|
||||
boost::thread m_Thread;
|
||||
Worker* m_Worker = nullptr;
|
||||
bool m_IsRunning = false;
|
||||
};
|
||||
|
||||
enum class FileWatcher::FileEventFlags
|
||||
{
|
||||
None = 0,
|
||||
Created = 1 << 0,
|
||||
SizeChanged = 1 << 1,
|
||||
TimestampChanged = 1 << 2,
|
||||
Deleted = 1 << 3,
|
||||
};
|
||||
inline FileWatcher::FileEventFlags operator|(FileWatcher::FileEventFlags a, FileWatcher::FileEventFlags b) { return static_cast<FileWatcher::FileEventFlags>(static_cast<int>(a) | static_cast<int>(b)); }
|
||||
inline bool operator&(FileWatcher::FileEventFlags a, FileWatcher::FileEventFlags b) { return static_cast<int>(a)& static_cast<int>(b); }
|
||||
|
||||
class FileWatcher::Worker
|
||||
{
|
||||
public:
|
||||
void operator()();
|
||||
void AddWatch(std::string path, FileEventCallback_t callback);
|
||||
void Check();
|
||||
|
||||
private:
|
||||
struct FileInfo
|
||||
{
|
||||
int Size;
|
||||
std::time_t Timestamp;
|
||||
};
|
||||
|
||||
std::unordered_map<boost::filesystem::path, FileEventCallback_t> m_FileCallbacks;
|
||||
std::unordered_map<boost::filesystem::path, FileInfo> m_FileInfo;
|
||||
boost::mutex m_Mutex;
|
||||
|
||||
FileInfo GetFileInfo(boost::filesystem::path path);
|
||||
FileEventFlags UpdateFileInfo(boost::filesystem::path path);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
This file is part of Daydream Engine.
|
||||
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
|
||||
|
||||
Daydream Engine is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Daydream Engine is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef GLError_h__
|
||||
#define GLError_h__
|
||||
|
||||
#include "PrecompiledHeader.h"
|
||||
#include <iostream>
|
||||
|
||||
inline bool _GLERROR(const char* info, const char* file, const char* func, unsigned int line)
|
||||
{
|
||||
GLenum error = glGetError();
|
||||
if (error != GL_NO_ERROR)
|
||||
{
|
||||
_LOG(LOG_LEVEL_ERROR, file, func, line, "GL Error: %s %i %s", info, error, gluErrorString(error));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#define GLERROR(function) \
|
||||
_GLERROR(function, __BASE_FILE__, __func__, __LINE__)
|
||||
|
||||
#endif // GLError_h__
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
This file is part of Daydream Engine.
|
||||
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
|
||||
|
||||
Daydream Engine is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Daydream Engine is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef Logging_h__
|
||||
#define Logging_h__
|
||||
|
||||
#ifdef _WIN32
|
||||
// http://stackoverflow.com/a/2282433
|
||||
#define __func__ __FUNCTION__
|
||||
// http://stackoverflow.com/a/8488201
|
||||
#define __BASE_FILE__ \
|
||||
(strrchr(__FILE__, '/') \
|
||||
? strrchr(__FILE__, '/') + 1 \
|
||||
: (strrchr(__FILE__, '\\') \
|
||||
? strrchr(__FILE__, '\\') + 1 \
|
||||
: __FILE__))
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <stdarg.h>
|
||||
|
||||
enum _LOG_LEVEL
|
||||
{
|
||||
LOG_LEVEL_ERROR,
|
||||
LOG_LEVEL_WARNING,
|
||||
LOG_LEVEL_INFO,
|
||||
LOG_LEVEL_DEBUG
|
||||
};
|
||||
|
||||
#ifdef DEBUG
|
||||
static _LOG_LEVEL LOG_LEVEL = LOG_LEVEL_DEBUG;
|
||||
#else
|
||||
static _LOG_LEVEL LOG_LEVEL = LOG_LEVEL_DEBUG;
|
||||
#endif
|
||||
|
||||
const static char* _LOG_LEVEL_PREFIX[] =
|
||||
{
|
||||
"E: ",
|
||||
"W: ",
|
||||
"",
|
||||
"D: "
|
||||
};
|
||||
|
||||
static void _LOG(_LOG_LEVEL logLevel, const char* file, const char* func, unsigned int line, const char* format, ...)
|
||||
{
|
||||
if (logLevel > LOG_LEVEL)
|
||||
return;
|
||||
|
||||
char* message = nullptr;
|
||||
va_list args;
|
||||
|
||||
va_start(args, format);
|
||||
size_t size = vsnprintf(message, 0, format, args) + 1;
|
||||
va_end(args);
|
||||
|
||||
va_start(args, format);
|
||||
message = new char[size];
|
||||
vsnprintf(message, size, format, args);
|
||||
va_end(args);
|
||||
|
||||
if (logLevel == LOG_LEVEL_ERROR)
|
||||
{
|
||||
std::cerr << file << ":" << line << " " << func << std::endl;
|
||||
std::cerr << _LOG_LEVEL_PREFIX[logLevel] << message << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << _LOG_LEVEL_PREFIX[logLevel] << message << std::endl;
|
||||
}
|
||||
|
||||
delete[] message;
|
||||
}
|
||||
|
||||
#define LOG(logLevel, format, ...) \
|
||||
_LOG(logLevel, __BASE_FILE__, __func__, __LINE__, format, ##__VA_ARGS__)
|
||||
|
||||
#define LOG_ERROR(format, ...) \
|
||||
LOG(LOG_LEVEL_ERROR, format, ##__VA_ARGS__)
|
||||
|
||||
#define LOG_WARNING(format, ...) \
|
||||
LOG(LOG_LEVEL_WARNING, format, ##__VA_ARGS__)
|
||||
|
||||
#define LOG_INFO(format, ...) \
|
||||
LOG(LOG_LEVEL_INFO, format, ##__VA_ARGS__)
|
||||
|
||||
#define LOG_DEBUG(format, ...) \
|
||||
LOG(LOG_LEVEL_DEBUG, format, ##__VA_ARGS__)
|
||||
|
||||
#endif // Logging_h__
|
||||
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
This file is part of Daydream Engine.
|
||||
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
|
||||
|
||||
Daydream Engine is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Daydream Engine is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef Util_Rectangle_h__
|
||||
#define Util_Rectangle_h__
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace dd
|
||||
{
|
||||
|
||||
struct Rectangle
|
||||
{
|
||||
Rectangle()
|
||||
: X(0), Y(0), Width(0), Height(0) { }
|
||||
|
||||
Rectangle(int width, int height)
|
||||
: X(0), Y(0), Width(width), Height(height) { }
|
||||
|
||||
Rectangle(int x, int y, int width, int height)
|
||||
: X(x), Y(y), Width(width), Height(height) { }
|
||||
|
||||
/*Rectangle(const Rectangle &rect)
|
||||
: X(rect.X), Y(rect.Y), Width(rect.Width), Height(rect.Height) { }*/
|
||||
|
||||
int X;
|
||||
int Y;
|
||||
int Width;
|
||||
int Height;
|
||||
|
||||
virtual int Left() const { return X; }
|
||||
void SetLeft(int left)
|
||||
{
|
||||
Width += X - left;
|
||||
X = left;
|
||||
}
|
||||
virtual int Right() const { return X + Width; }
|
||||
void SetRight(int right)
|
||||
{
|
||||
Width = right - X;
|
||||
}
|
||||
virtual int Top() const { return Y; }
|
||||
void SetTop(int top)
|
||||
{
|
||||
Height += Y - top;
|
||||
Y = top;
|
||||
}
|
||||
virtual int Bottom() const { return Y + Height; }
|
||||
int SetBottom(int bottom)
|
||||
{
|
||||
Height = bottom - Y;
|
||||
}
|
||||
|
||||
Rectangle& operator+=(const Rectangle &rhs) {
|
||||
SetLeft(std::min(Left(), rhs.Left()));
|
||||
SetRight(std::max(Right(), rhs.Right()));
|
||||
SetTop(std::min(Top(), rhs.Top()));
|
||||
SetBottom(std::max(Bottom(), rhs.Bottom()));
|
||||
}
|
||||
|
||||
static bool Intersects(const Rectangle &r1, const Rectangle &r2)
|
||||
{
|
||||
return !(r2.Left() > r1.Right() || r2.Right() < r1.Left() || r2.Top() > r1.Bottom() || r2.Bottom() < r1.Top());
|
||||
}
|
||||
};
|
||||
|
||||
inline bool operator==(const Rectangle &r1, const Rectangle &r2)
|
||||
{
|
||||
return (r1.X == r2.X) && (r1.Y == r2.Y) && (r1.Width == r2.Width) && (r1.Height == r2.Height);
|
||||
}
|
||||
|
||||
inline bool operator!=(const Rectangle &lhs, const Rectangle &rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
inline bool operator<(const Rectangle &lhs, const Rectangle &rhs)
|
||||
{
|
||||
return (lhs.Width < rhs.Width) && (lhs.Height < rhs.Height);
|
||||
}
|
||||
|
||||
inline bool operator>(const Rectangle &lhs, const Rectangle &rhs)
|
||||
{
|
||||
return rhs < lhs;
|
||||
}
|
||||
|
||||
inline bool operator<=(const Rectangle &lhs, const Rectangle &rhs)
|
||||
{
|
||||
return !(lhs > rhs);
|
||||
}
|
||||
|
||||
inline bool operator>=(const Rectangle &lhs, const Rectangle &rhs)
|
||||
{
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
|
||||
inline Rectangle operator+(Rectangle lhs, const Rectangle &rhs)
|
||||
{
|
||||
lhs += rhs;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // Util_Rectangle_h__
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
This file is part of Daydream Engine.
|
||||
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
|
||||
|
||||
Daydream Engine is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Daydream Engine is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef Util_UnorderedMapPair_h__
|
||||
#define Util_UnorderedMapPair_h__
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
namespace std
|
||||
{
|
||||
|
||||
template<typename S, typename T> struct hash < pair<S, T> >
|
||||
{
|
||||
inline size_t operator()(const pair<S, T> & v) const
|
||||
{
|
||||
size_t seed = 0;
|
||||
boost::hash_combine(seed, v.first);
|
||||
boost::hash_combine(seed, v.second);
|
||||
return seed;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // Util_UnorderedMapPair_h__
|
||||
Reference in New Issue
Block a user