1 /*!
2  * \file   mfront/src/CMaterialPropertyInterface.cxx
3  * \brief
4  * \author Thomas Helfer
5  * \date   06 mai 2008
6  * \copyright Copyright (C) 2006-2018 CEA/DEN, EDF R&D. All rights
7  * reserved.
8  * This project is publicly released under either the GNU GPL Licence
9  * or the CECILL-A licence. A copy of thoses licences are delivered
10  * with the sources of TFEL. CEA or EDF may also distribute this
11  * project under specific licensing conditions.
12  */
13 
14 #include<sstream>
15 #include<stdexcept>
16 #include<algorithm>
17 #include"TFEL/Raise.hxx"
18 #include"TFEL/Config/GetInstallPath.hxx"
19 #include"MFront/DSLUtilities.hxx"
20 #include"MFront/MFrontUtilities.hxx"
21 #include"MFront/MFrontHeader.hxx"
22 #include"MFront/TargetsDescription.hxx"
23 #include"MFront/MaterialPropertyDescription.hxx"
24 #include"MFront/CMaterialPropertyInterface.hxx"
25 
26 namespace mfront
27 {
28 
getName()29   std::string CMaterialPropertyInterface::getName()
30   {
31     return "c";
32   }
33 
34   CMaterialPropertyInterface::CMaterialPropertyInterface() = default;
35 
36   std::string
getGeneratedLibraryName(const MaterialPropertyDescription & m) const37   CMaterialPropertyInterface::getGeneratedLibraryName(const MaterialPropertyDescription& m) const
38   {
39     return getMaterialLawLibraryNameBase(m);
40   } // end of CMaterialPropertyInterface::getGeneratedLibraryName
41 
42   std::pair<bool,tfel::utilities::CxxTokenizer::TokensContainer::const_iterator>
treatKeyword(const std::string & k,const std::vector<std::string> & i,tokens_iterator current,const tokens_iterator)43   CMaterialPropertyInterface::treatKeyword(const std::string& k,
44 					   const std::vector<std::string>& i,
45 					   tokens_iterator current,
46 					   const tokens_iterator)
47   {
48     tfel::raise_if(std::find(i.begin(),i.end(),"c")!=i.end(),
49 		   "CMaterialPropertyInterface::treatKeyword: "
50 		   "unsupported key '"+k+"'");
51     return {false,current};
52   } // end of CMaterialPropertyInterface::treatKeyword
53 
54   void
getTargetsDescription(TargetsDescription & d,const MaterialPropertyDescription & mpd) const55   CMaterialPropertyInterface::getTargetsDescription(TargetsDescription& d,
56 						    const MaterialPropertyDescription& mpd) const
57   {
58     const auto lib  = this->getGeneratedLibraryName(mpd);
59     const auto name = this->getSrcFileName(mpd.material,mpd.className);
60     const auto f    = mpd.material.empty() ? mpd.className : mpd.material+"_"+mpd.className;
61     const auto header = this->getHeaderFileName(mpd.material,mpd.className);
62     const auto tfel_config = tfel::getTFELConfigExecutableName();
63     insert_if(d[lib].cppflags,
64 	      "$(shell "+tfel_config+" --cppflags --compiler-flags)");
65     insert_if(d[lib].include_directories,
66 	      "$(shell "+tfel_config+" --include-path)");
67 #if  !((defined _WIN32) && (defined _MSC_VER))
68     insert_if(d[lib].link_libraries,"m");
69 #endif /* !((defined _WIN32) && (defined _MSC_VER)) */
70     insert_if(d[lib].sources,name+".cxx");
71     insert_if(d[lib].epts,{f,f+"_checkBounds"});
72     if(!header.empty()){
73       insert_if(d.headers,header+".hxx");
74     }
75   } // end of CMaterialPropertyInterface::getTargetsDescription
76 
writeInterfaceSymbol(std::ostream & out,const MaterialPropertyDescription & mpd) const77   void CMaterialPropertyInterface::writeInterfaceSymbol(std::ostream& out,
78 							      const MaterialPropertyDescription& mpd) const{
79     mfront::writeInterfaceSymbol(out,this->getFunctionName(mpd),"C");
80   } // end of CMaterialPropertyInterface
81 
82   std::string
getHeaderFileName(const std::string & material,const std::string & className) const83   CMaterialPropertyInterface::getHeaderFileName(const std::string& material,
84 						const std::string& className) const
85   {
86     return material.empty() ? className : material+"_"+className;
87   } // end of CMaterialPropertyInterface::getHeaderFileName
88 
89   std::string
getSrcFileName(const std::string & material,const std::string & className) const90   CMaterialPropertyInterface::getSrcFileName(const std::string& material,
91 					     const std::string& className) const
92   {
93     return material.empty() ? className : material+"_"+className;
94   } // end of CMaterialPropertyInterface::getSrcFileName
95 
writeBeginHeaderNamespace(std::ostream & os) const96   void CMaterialPropertyInterface::writeBeginHeaderNamespace(std::ostream& os) const
97   {
98     os << "#ifdef __cplusplus\n"
99        << "extern \"C\"{\n"
100        << "#endif /* __cplusplus */\n\n";
101   } // end of CMaterialPropertyInterface::writeBeginHeaderNamespace
102 
writeEndHeaderNamespace(std::ostream & os) const103   void CMaterialPropertyInterface::writeEndHeaderNamespace(std::ostream& os) const
104   {
105     os << "#ifdef __cplusplus\n"
106        << "} /* end of extern \"C\" */\n"
107        << "#endif /* __cplusplus */\n\n";
108   } // end of CMaterialPropertyInterface::writeEndHeaderNamespace()
109 
writeBeginSrcNamespace(std::ostream & os) const110   void CMaterialPropertyInterface::writeBeginSrcNamespace(std::ostream& os) const
111   {
112     os << "#ifdef __cplusplus\n"
113        << "extern \"C\"{\n"
114        << "#endif /* __cplusplus */\n\n";
115   } // end of CMaterialPropertyInterface::writeBeginSrcNamespace
116 
writeEndSrcNamespace(std::ostream & os) const117   void CMaterialPropertyInterface::writeEndSrcNamespace(std::ostream& os) const
118   {
119     os << "#ifdef __cplusplus\n"
120        << "} // end of extern \"C\"\n"
121        << "#endif /* __cplusplus */\n\n";
122   } // end of CMaterialPropertyInterface::writeEndSrcNamespace()
123 
124   std::string
getFunctionName(const MaterialPropertyDescription & mpd) const125   CMaterialPropertyInterface::getFunctionName(const MaterialPropertyDescription& mpd) const
126   {
127     const auto material  = mpd.material;
128     const auto className = mpd.className;
129     return material.empty() ? className : material+"_"+className;
130   } // end of CMaterialPropertyInterface::getFunctionName
131 
requiresCheckBoundsFunction() const132   bool CMaterialPropertyInterface::requiresCheckBoundsFunction() const
133   {
134     return false;
135   }
136 
137   std::string
getCheckBoundsFunctionName(const MaterialPropertyDescription & mpd) const138   CMaterialPropertyInterface::getCheckBoundsFunctionName(const MaterialPropertyDescription& mpd) const
139   {
140     const auto material  = mpd.material;
141     const auto className = mpd.className;
142     if(material.empty()){
143       return className+"_checkBounds";
144     }
145     return material+"_"+className+"_checkBounds";
146   } // end of CMaterialPropertyInterface::getCheckBoundsFunctionName
147 
148   CMaterialPropertyInterface::~CMaterialPropertyInterface() = default;
149 
150 } // end of namespace mfront
151