Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
make_templates.py
Go to the documentation of this file.
1 # :noTabs=true:
2 #
3 # (c) Copyright Rosetta Commons Member Institutions.
4 # (c) This file is part of the Rosetta software suite and is made available under license.
5 # (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
6 # (c) For more information, see http://www.rosettacommons.org. Questions about this can be
7 # (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
8 #
9 ## @file utility/tools/make_templates.py
10 ## @brief Program that generate make_vector.hh and make_map.hh files
11 ## @author Sergey Lyskov
12 
13 NFunctions = 25 # number of functional argument that will be genrated
14 
15 Header = """// -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
16 // vi: set ts=2 noet:
17 //
18 // (c) Copyright Rosetta Commons Member Institutions.
19 // (c) This file is part of the Rosetta software suite and is made available under license.
20 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
21 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
22 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
23 //
24 /// @file utility/tools/make_vector.hh
25 /// @brief Common function to build vector, vector0, vector1, map.
26 /// @author Sergey Lyskov
27 
28 #ifndef INCLUDED_utility_tools_%(iguard)s
29 #define INCLUDED_utility_tools_%(iguard)s
30 
31 %(include)s
32 
33 namespace utility {
34 namespace tools {
35 
36 """
37 
38 Footer = """
39 } // namespace utility
40 } // namespace tools
41 
42 #endif // INCLUDED_utility_tools_%(iguard)s
43 
44 """
45 
46 def getVectorFunction(n, className, functionName):
47  r = "template<typename T>\n%s<T> %s(const T & i0" % (className, functionName)
48  for i in range(1, n): r += ', const T & i%d' % i
49  r += ')\n{\n %s<T> v;\n ' % className
50  for i in range(n): r += ' v.push_back(i%d);' % i
51  r += '\n return(v);\n}\n\n'
52  return r
53 
54 def getMapFunction(n, className, functionName):
55  r = "template<typename T1, typename T2>\n%s<T1, T2> make_map(const T1 &f0, const T2 & s0" % className
56  for i in range(1, n): r += ', const T1 & f%d, const T2 & s%d' % (i,i)
57  r += ')\n{\n %s<T1, T2> m;\n '% className
58  for i in range(n): r += ' m[f%d]=s%d;' % (i, i)
59  r += '\n return(m);\n}\n\n'
60  return r
61 
62 
63 def makeFile(fileName, include, function, className, cppName):
64  iguard = fileName.replace('.', '_')
65  s = Header % dict(include=include, iguard=iguard)
66  for i in range(1, NFunctions):
67  s += function(i, className, cppName)
68  s += Footer % dict(iguard=iguard)
69  f = file(fileName, 'wb'); f.write(s); f.close()
70 
71 
72 tasks = [
73  dict(fileName='make_vector.hh', include='#include <vector>\n',
74  function=getVectorFunction, className='std::vector', cppName='make_vector'),
75 
76  dict(fileName='make_vector0.hh', include='#include <utility/vector0.fwd.hh>\n',
77  function=getVectorFunction, className='utility::vector0', cppName='make_vector0'),
78 
79  dict(fileName='make_vector1.hh', include='#include <utility/vector1.fwd.hh>\n',
80  function=getVectorFunction, className='utility::vector1', cppName='make_vector1'),
81 
82  dict(fileName='make_map.hh', include='#include <map>\n',
83  function=getMapFunction, className='std::map', cppName='make_map'),
84 ]
85 
86 
87 def main():
88  map(lambda x: makeFile(**x), tasks)
89 
90  """s = Header
91  for i in range(1, NFunctions):
92  s += getVectorFunction(i, 'std::vector', 'make_vector')
93  s += getVectorFunction(i, 'utility::vector0', 'make_vector0')
94  s += getVectorFunction(i, 'utility::vector1', 'make_vector1')
95  s += getMapFunction(i, 'std::map')
96  s += Footer
97 
98  f = file('make_vector.hh', 'wb'); f.write(s); f.close()
99  """
100 
101 if __name__ == "__main__": main()