CppADCodeGen 2.4.3
A C++ Algorithmic Differentiation Package with Source Code Generation
Loading...
Searching...
No Matches
save_files_model_library_processor.hpp
1#ifndef CPPAD_CG_SAVE_FILES_MODEL_LIBRARY_PROCESSOR_INCLUDED
2#define CPPAD_CG_SAVE_FILES_MODEL_LIBRARY_PROCESSOR_INCLUDED
3/* --------------------------------------------------------------------------
4 * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5 * Copyright (C) 2019 Joao Leal
6 * Copyright (C) 2013 Ciengis
7 *
8 * CppADCodeGen is distributed under multiple licenses:
9 *
10 * - Eclipse Public License Version 1.0 (EPL1), and
11 * - GNU General Public License Version 3 (GPL3).
12 *
13 * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
14 * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
15 * ----------------------------------------------------------------------------
16 * Author: Joao Leal
17 */
18
19namespace CppAD {
20namespace cg {
21
28template<class Base>
30public:
31
32 inline explicit SaveFilesModelLibraryProcessor(ModelLibraryCSourceGen<Base>& modelLibraryHelper) :
33 ModelLibraryProcessor<Base>(modelLibraryHelper) {
34 }
35
36 inline virtual ~SaveFilesModelLibraryProcessor() = default;
37
38 inline void saveSources() {
39 saveSourcesTo("cppadcg_sources");
40 }
41
42 inline void saveSourcesTo(const std::string& sourcesFolder) {
43
44 auto saveFile = [&](const std::string& filename, const std::string& source){
45 std::ofstream sourceFile;
46 std::string file = system::createPath(sourcesFolder, filename);
47 sourceFile.open(file.c_str());
48 sourceFile << source;
49 sourceFile.close();
50 };
51
52 system::createFolder(sourcesFolder);
53
54 const std::map<std::string, ModelCSourceGen<Base>*>& models = this->modelLibraryHelper_->getModels();
55
56 for (const auto& itm : models) {
57 const std::map<std::string, std::string>& sources = this->getSources(*itm.second);
58
59 for (const auto& it : sources) {
60 saveFile(it.first, it.second);
61 }
62 }
63
64 for (const auto& it : this->modelLibraryHelper_->getLibrarySources()) {
65 saveFile(it.first, it.second);
66 }
67
68 for (const auto& it : this->modelLibraryHelper_->getCustomSources()) {
69 saveFile(it.first, it.second);
70 }
71 }
72
73 inline static void saveLibrarySourcesTo(ModelLibraryCSourceGen<Base>& modelLibraryHelper,
74 const std::string& sourcesFolder) {
75 SaveFilesModelLibraryProcessor s(modelLibraryHelper);
76 s.saveSourcesTo(sourcesFolder);
77 }
78
79};
80
81} // END cg namespace
82} // END CppAD namespace
83
84#endif
std::string createPath(const std::string &baseFolder, const std::string &file)
void createFolder(const std::string &folder)