ape  0.5.0
Audio Programming Environment
TCCBindings.h
Go to the documentation of this file.
1 /*************************************************************************************
2 
3  Audio Programming Environment - Audio Plugin - v. 0.4.0.
4 
5  Copyright (C) 2017 Janus Lynggaard Thorborg [LightBridge Studios]
6 
7  This program is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20  See \licenses\ for additional details on licenses associated with this program.
21 
22  **************************************************************************************
23 
24  file:TCCBindings.h
25 
26  Automatic, threadsafe and reentrant bindings to TCC
27 
28 *************************************************************************************/
29 
30 #ifndef TCC_BINDINGS_H
31  #define TCC_BINDINGS_H
32 
33  #include "APE.h"
34  #include "libtcc.h"
35  #include <mutex>
36  #include <memory>
37  #ifndef TCC_STATIC_LINK
38  #include <cpl/CModule.h>
39  #endif
40  #include <cpl/Utility.h>
41 
42  namespace ape
43  {
44  class TCCBindings
45  {
46  public:
47 
48  class CompilerAccess : cpl::Utility::CNoncopyable
49  {
50  public:
51 
52  CompilerAccess() : bindings(instance()), compilerLock(bindings.compilerMutex) {}
53 
54  bool isLinked() const noexcept { return bindings.linkedCorrectly; }
55 
56  TCCState * createState() const { return bindings.newState(); }
57  void deleteState(TCCState * s) const { return bindings.deleteState(s); }
58  void setLibPath(TCCState * s, const char *path) const { return bindings.setLibPath(s, path); }
59  bool addLibPath(TCCState * s, const char *path) const { return bindings.addLibPath(s, path) != -1; }
60  void setErrorFunc(TCCState * s, void * errorOpaque, void (*errorFunction)(void*, const char*) ) const
61  {
62  return bindings.setErrorFunc(s, errorOpaque, errorFunction);
63  }
64  void addIncludePath(TCCState * s, const char * pathName) const { bindings.addIncludePath(s, pathName); }
65  void defineSymbol(TCCState * s, const char * symbol, const char * value) const { return bindings.defineSymbol(s, symbol, value); }
66  bool compileString(TCCState * s, const char * buffer) const { return bindings.compileString(s, buffer) != -1; }
67  int setOutputType(TCCState * s, int outputType) const { return bindings.setOutputType(s, outputType); }
68  int relocate(TCCState * s, void * options) const { return bindings.relocate(s, options); }
69  void * getSymbol(TCCState * s, const char * name) const { return bindings.getSymbol(s, name); }
70  void addSymbol(TCCState * s, const char * name, const void * value) const { bindings.addSymbol(s, name, value); }
71  void setOptions(TCCState * s, const char * commands) const { return bindings.setOptions(s, commands); }
72  bool addFile(TCCState * s, const char * file) const { return bindings.addFile(s, file) != -1; }
73  bool outputFile(TCCState * s, const char * ofile) const { return bindings.outputFile(s, ofile) != -1; }
74 
75  private:
76 
77  TCCBindings & bindings;
78  std::lock_guard<std::recursive_mutex> compilerLock;
79  };
80 
81  private:
82 
83  TCCBindings()
84  {
85 #ifdef TCC_STATIC_LINK
86  newState = tcc_new;
87  setLibPath = tcc_set_lib_path;
88  addLibPath = tcc_add_library_path;
89  addIncludePath = tcc_add_include_path;
90  setOutputType = tcc_set_output_type;
91  setErrorFunc = tcc_set_error_func;
92  compileString = tcc_compile_string;
93  deleteState = tcc_delete;
94  relocate = tcc_relocate;
95  addSymbol = tcc_add_symbol;
96  getSymbol = tcc_get_symbol;
97  defineSymbol = tcc_define_symbol;
98  setOptions = tcc_set_options;
99  addFile = tcc_add_file;
100  outputFile = tcc_output_file;
101 #else
102  if (!tccDLib.load(cpl::Misc::DirectoryPath() + "/libtcc.dll"))
103  {
104  newState = (decltype(newState))tccDLib.getFuncAddress("tcc_new");
105  setLibPath = (decltype(setLibPath))tccDLib.getFuncAddress("tcc_set_lib_path");
106  addLibPath = (decltype(addLibPath))tccDLib.getFuncAddress("tcc_add_library_path");
107  addIncludePath = (decltype(addIncludePath))tccDLib.getFuncAddress("tcc_add_include_path");
108  setOutputType = (decltype(setOutputType))tccDLib.getFuncAddress("tcc_set_output_type");
109  setErrorFunc = (decltype(setErrorFunc))tccDLib.getFuncAddress("tcc_set_error_func");
110  compileString = (decltype(compileString))tccDLib.getFuncAddress("tcc_compile_string");
111  deleteState = (decltype(deleteState))tccDLib.getFuncAddress("tcc_delete");
112  relocate = (decltype(relocate))tccDLib.getFuncAddress("tcc_relocate");
113  getSymbol = (decltype(getSymbol))tccDLib.getFuncAddress("tcc_get_symbol");
114  addSymbol = (decltype(addSymbol))tccDLib.getFuncAddress("tcc_add_symbol");
115  defineSymbol = (decltype(defineSymbol))tccDLib.getFuncAddress("tcc_define_symbol");
116  setOptions = (decltype(setOptions))tccDLib.getFuncAddress("tcc_set_options");
117  addFile = (decltype(addFile))tccDLib.getFuncAddress("tcc_add_file");
118  outputFile = (decltype(outputFile))tccDLib.getFuncAddress("tcc_output_file");
119  }
120 #endif
121 
122  // test whether ALL functions pointers are valid.
123  linkedCorrectly = newState && setLibPath && addSymbol && addLibPath && addIncludePath && setOutputType && setErrorFunc
124  && compileString && deleteState && relocate && getSymbol && defineSymbol && setOptions
125  && addFile && outputFile;
126  }
127 
128  static TCCBindings & instance()
129  {
130  static TCCBindings bindings;
131  return bindings;
132  }
133 
134  decltype(tcc_new) * newState;
135  decltype(tcc_delete) * deleteState;
136  decltype(tcc_set_lib_path) * setLibPath;
137  decltype(tcc_add_library_path) * addLibPath;
138  decltype(tcc_set_error_func) * setErrorFunc;
139  decltype(tcc_add_include_path) * addIncludePath;
140  decltype(tcc_define_symbol) * defineSymbol;
141  decltype(tcc_add_symbol) * addSymbol;
142  decltype(tcc_compile_string) * compileString;
143  decltype(tcc_set_output_type) * setOutputType;
144  decltype(tcc_relocate) * relocate;
145  decltype(tcc_get_symbol) * getSymbol;
146  decltype(tcc_set_options) * setOptions;
147  decltype(tcc_add_file) * addFile;
148  decltype(tcc_output_file) * outputFile;
149 
150 #ifndef TCC_STATIC_LINK
151  cpl::CModule tccDLib;
152 #endif
153 
154  private:
155 
156  bool linkedCorrectly;
157  std::recursive_mutex compilerMutex;
158  };
159 
160  class TCCDeleter
161  {
162  public: void operator()(TCCState * s) { TCCBindings::CompilerAccess().deleteState(s); }
163  };
164  typedef std::unique_ptr<TCCState, TCCDeleter> UniqueTCC;
165 
166  }
167 #endif
APE.h
ape::TCCBindings
Definition: TCCBindings.h:69
ape::TCCBindings::CompilerAccess::addSymbol
void addSymbol(TCCState *s, const char *name, const void *value) const
Definition: TCCBindings.h:147
ape::TCCBindings::CompilerAccess::addLibPath
bool addLibPath(TCCState *s, const char *path) const
Definition: TCCBindings.h:136
ape::TCCBindings::CompilerAccess::compileString
bool compileString(TCCState *s, const char *buffer) const
Definition: TCCBindings.h:143
ape::TCCBindings::CompilerAccess::CompilerAccess
CompilerAccess()
Definition: TCCBindings.h:129
ape::UniqueTCC
std::unique_ptr< TCCState, TCCDeleter > UniqueTCC
Definition: TCCBindings.h:189
ape::TCCBindings::CompilerAccess::addFile
bool addFile(TCCState *s, const char *file) const
Definition: TCCBindings.h:149
ape::TCCBindings::CompilerAccess
Definition: TCCBindings.h:99
ape::TCCBindings::CompilerAccess::setOptions
void setOptions(TCCState *s, const char *commands) const
Definition: TCCBindings.h:148
ape::TCCBindings::CompilerAccess::setErrorFunc
void setErrorFunc(TCCState *s, void *errorOpaque, void(*errorFunction)(void *, const char *)) const
Definition: TCCBindings.h:137
ape
Definition: audiofile.h:7
ape::TCCBindings::CompilerAccess::deleteState
void deleteState(TCCState *s) const
Definition: TCCBindings.h:134
ape::TCCBindings::CompilerAccess::defineSymbol
void defineSymbol(TCCState *s, const char *symbol, const char *value) const
Definition: TCCBindings.h:142
ape::TCCBindings::CompilerAccess::addIncludePath
void addIncludePath(TCCState *s, const char *pathName) const
Definition: TCCBindings.h:141
ape::TCCBindings::CompilerAccess::outputFile
bool outputFile(TCCState *s, const char *ofile) const
Definition: TCCBindings.h:150
ape::TCCDeleter
Definition: TCCBindings.h:185
ape::TCCBindings::CompilerAccess::getSymbol
void * getSymbol(TCCState *s, const char *name) const
Definition: TCCBindings.h:146
ape::TCCBindings::CompilerAccess::isLinked
bool isLinked() const noexcept
Definition: TCCBindings.h:131
ape::TCCBindings::CompilerAccess::relocate
int relocate(TCCState *s, void *options) const
Definition: TCCBindings.h:145
ape::TCCDeleter::operator()
void operator()(TCCState *s)
Definition: TCCBindings.h:187
ape::TCCBindings::CompilerAccess::setLibPath
void setLibPath(TCCState *s, const char *path) const
Definition: TCCBindings.h:135
ape::TCCBindings::CompilerAccess::createState
TCCState * createState() const
Definition: TCCBindings.h:133
ape::TCCBindings::CompilerAccess::setOutputType
int setOutputType(TCCState *s, int outputType) const
Definition: TCCBindings.h:144