-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAddToLuaPaths.cpp
More file actions
135 lines (114 loc) · 3.73 KB
/
AddToLuaPaths.cpp
File metadata and controls
135 lines (114 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
@file
@brief Implementation
@date 2013
@author
Ryan Pavlik
<rpavlik@iastate.edu> and <abiryan@ryand.net>
http://academic.cleardefinition.com/
Iowa State University Virtual Reality Applications Center
Human-Computer Interaction Graduate Program
*/
// Copyright Iowa State University 2013.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Internal Includes
#include "AddToLuaPaths.h"
// Library/third-party includes
#include <boost/foreach.hpp>
// Standard includes
// - none
namespace vrjLua {
#define LUA_VER "5.1"
namespace {
template<typename T>
class PushBackFunctor {
public:
PushBackFunctor(T & container) : _container(container) {}
void operator()(typename T::value_type a) {
_container.push_back(a);
}
private:
T & _container;
};
} // end of namespace
namespace detail {
template<typename SearchTag>
struct GetDirsFromRoot {
static const char * dirs[];
};
template<>
const char * GetDirsFromRoot<LuaPathTags::LuaSearch>::dirs[] = {
"share/vrjugglua/lua/",
"share/lua/" LUA_VER "/",
"lib/lua/" LUA_VER "/"
};
template<>
const char * GetDirsFromRoot<LuaPathTags::LuaCSearch>::dirs[] = {
"lib/lua/" LUA_VER "/",
"lib/"
#ifdef _WIN32
,
"bin/"
#endif
};
template<typename SearchTag>
struct CallWithPatterns;
template<>
struct CallWithPatterns<LuaPathTags::LuaSearch> {
template<typename F>
static void apply(std::string const& dir, F func) {
static const char * luaPatterns[] = {
"?.lua",
"?/init.lua"
};
BOOST_FOREACH(const char * patt, luaPatterns) {
func(dir + patt);
}
}
};
template<>
struct CallWithPatterns<LuaPathTags::LuaCSearch> {
template<typename F>
static void apply(std::string const& dir, F func) {
#ifdef _WIN32
static const char luaCPattern[] = "?.dll";
#else
static const char luaCPattern[] = "?.so";
#endif
func(dir + luaCPattern);
}
};
template<typename DirectoryTag, typename SearchTag>
struct CallWithDirectory;
template<typename SearchTag>
struct CallWithDirectory<LuaPathTags::RootDirectory, SearchTag> {
template<typename F>
static void apply(std::string const& rootdir, F func) {
BOOST_FOREACH(const char * patt, GetDirsFromRoot<SearchTag>::dirs) {
CallWithPatterns<SearchTag>::apply(rootdir + patt, func);
}
}
};
template<typename SearchTag>
struct CallWithDirectory<LuaPathTags::SearchDirectory, SearchTag> {
template<typename F>
static void apply(std::string const& rootdir, F func) {
CallWithPatterns<SearchTag>::apply(rootdir, func);
}
};
template<typename DirectoryTag, typename SearchTag>
void extendLuaSearchPath(DirectoryBase<DirectoryTag> const& d, SearchPathContainerBase<SearchTag> & s) {
typedef std::vector<std::string> StringList;
StringList newpaths;
CallWithDirectory<DirectoryTag, SearchTag>::apply(d.get(), PushBackFunctor<StringList>(newpaths));
s.insert(newpaths);
}
// Explicit instantiation of function template above, so we can hide this all in the CPP file.
template void extendLuaSearchPath(DirectoryBase<LuaPathTags::RootDirectory> const&, SearchPathContainerBase<LuaPathTags::LuaSearch> &);
template void extendLuaSearchPath(DirectoryBase<LuaPathTags::SearchDirectory> const&, SearchPathContainerBase<LuaPathTags::LuaSearch> &);
template void extendLuaSearchPath(DirectoryBase<LuaPathTags::RootDirectory> const&, SearchPathContainerBase<LuaPathTags::LuaCSearch> &);
template void extendLuaSearchPath(DirectoryBase<LuaPathTags::SearchDirectory> const&, SearchPathContainerBase<LuaPathTags::LuaCSearch> &);
} // end of namespace detail
} // end of namespace vrjLua