GeNN  4.9.0
GPU enhanced Neuronal Networks (GeNN)
supportCodeMerged.h
Go to the documentation of this file.
1 #pragma once
2 
3 // Standard C++ includes
4 #include <unordered_map>
5 
6 // GeNN code generator includes
9 
10 //--------------------------------------------------------------------------
11 // CodeGenerator::SupportCodeMerged
12 //--------------------------------------------------------------------------
13 namespace CodeGenerator
14 {
16 {
17 public:
18  SupportCodeMerged(const std::string &namespacePrefix) : m_NamespacePrefix(namespacePrefix)
19  {}
20 
21  //------------------------------------------------------------------------
22  // Public API
23  //------------------------------------------------------------------------
25  void addSupportCode(const std::string &code)
26  {
27  // If there is any support code
28  if(!code.empty()) {
29  // Try and add code to set, assuming that a new namespace will be required
30  // **NOTE** namespace name is NOT hashed or compared
31  const size_t numStrings = m_SupportCode.size();
32  m_SupportCode.emplace(code, m_NamespacePrefix + std::to_string(numStrings));
33  }
34  }
35 
37  const std::string &getSupportCodeNamespace(const std::string &code) const
38  {
39  const auto s = m_SupportCode.find(code);
40  assert(s != m_SupportCode.cend());
41 
42  // Return the name of the namespace which should be included to use it
43  return s->second;
44  }
45 
47  void gen(CodeStream &os, const std::string &ftype, const bool supportsNamespace = true) const
48  {
49  // Loop through support code
50  for(const auto &s : m_SupportCode) {
51  if (supportsNamespace) {
52  // Write namespace containing support code with fixed up floating point type
53  os << "namespace " << s.second;
54  {
55  CodeStream::Scope b(os);
56  os << ensureFtype(s.first, ftype) << std::endl;
57  }
58  }
59  else {
60  // Regex for function definition - looks for words with succeeding parentheses with or without any data inside the parentheses (arguments) followed by braces on the same or new line
61  std::regex r("\\w+(?=\\(.*\\)\\s*\\{)");
62  os << ensureFtype(std::regex_replace(s.first, r, s.second + "_$&"), ftype) << std::endl;
63  }
64  os << std::endl;
65  }
66  }
67 
69  size_t getNumSupportCodeString() const{ return m_SupportCode.size(); }
70 
71 private:
72  //------------------------------------------------------------------------
73  // Members
74  //------------------------------------------------------------------------
75  // Map of support code strings to namespace names
76  std::unordered_map<std::string, std::string> m_SupportCode;
77 
78  // Prefix
79  const std::string m_NamespacePrefix;
80 };
81 } // namespace CodeGenerator
SupportCodeMerged(const std::string &namespacePrefix)
Definition: supportCodeMerged.h:18
size_t getNumSupportCodeString() const
Gets the number of support code strings hence namespaces which will be generated. ...
Definition: supportCodeMerged.h:69
Helper class for generating code - automatically inserts brackets, indents etc.
Definition: backendBase.h:30
Definition: codeStream.h:21
void addSupportCode(const std::string &code)
Add support code string.
Definition: supportCodeMerged.h:25
const std::string & getSupportCodeNamespace(const std::string &code) const
Gets the name of the support code namespace which should be &#39;used&#39; to provide this support code...
Definition: supportCodeMerged.h:37
void gen(CodeStream &os, const std::string &ftype, const bool supportsNamespace=true) const
Generate support code.
Definition: supportCodeMerged.h:47
GENN_EXPORT std::string ensureFtype(const std::string &oldcode, const std::string &type)
This function implements a parser that converts any floating point constant in a code snippet to a fl...
Definition: codeGenUtils.cc:373
Definition: supportCodeMerged.h:15
Definition: codeStream.h:94