CppADCodeGen  2.3.0
A C++ Algorithmic Differentiation Package with Source Code Generation
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) 2013 Ciengis
6  *
7  * CppADCodeGen is distributed under multiple licenses:
8  *
9  * - Eclipse Public License Version 1.0 (EPL1), and
10  * - GNU General Public License Version 3 (GPL3).
11  *
12  * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
13  * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
14  * ----------------------------------------------------------------------------
15  * Author: Joao Leal
16  */
17 
18 namespace CppAD {
19 namespace cg {
20 
27 template<class Base>
29 public:
30 
32  ModelLibraryProcessor<Base>(modelLibraryHelper) {
33  }
34 
35  inline virtual ~SaveFilesModelLibraryProcessor() {
36  }
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)