Minsky: 3.17.0
operationType.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2013
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef OPERATIONTYPE_H
20 #define OPERATIONTYPE_H
21 #include <factory.h>
22 #include <string>
23 #include <ostream>
24 
25 namespace minsky
26 {
28  {
29  enum Type {constant, // deprecated - left to support legacy schemas
30  time, // zero input port ops
32  differentiate, // with respect to time
33  data, // an interpolated data item
34  ravel, // Ravelâ„¢
35  euler, pi, zero, one, inf, percent,// fundamental constants
36  // multiwire binary ops
38  // underscores to avoid C++ keywords. Can be filtered at UI
40  // single wire binops
42  lt, le, eq,
44  // functions
48  // reductions
50  // scans
52  // other tensor ops
54  // statistics
56  numOps // last operation, for iteration purposes
57  };
59  static std::string typeName(int type);
61  static Group classify(Type t);
62  };
63 
64  namespace OperationTypeInfo
65  {
67  template <OperationType::Type type> int numArguments();
68  }
69 
70  inline std::ostream& operator<<(std::ostream& o, OperationType::Type t)
71  {return o<<OperationType::typeName(t);}
72 
74  template<class Base, template <minsky::OperationType::Type> class T,
77  {
78  struct CreatorBase
79  {
80  virtual ~CreatorBase() {}
81  virtual Base* create() const=0;
82  };
83 
84  std::vector<std::unique_ptr<CreatorBase>> creators;
85 
86  template <class U>
87  struct Creator: public CreatorBase
88  {
89  Base* create() const {return new U();}
90  };
91 
92  public:
93 
94  template <int I, int J>
95  struct is_equal {const static bool value=I==J;};
96 
97  // recursively enumerate the enum at compile time.
98  template <int I>
99  typename classdesc::enable_if<classdesc::Not<is_equal<I,N>>,void>::T
101  {
102  creators.emplace_back(new Creator<T<OperationType::Type(I)>>);
103  registerNext<I+1>();
104  }
105  template <int I>
106  typename classdesc::enable_if<is_equal<I,N>,void>::T
108  {creators.emplace_back(new Creator<T<OperationType::Type(I)>>);}
109 
111  Base* create(OperationType::Type o) const {
112  assert(o<creators.size());
113  return creators[o]->create();
114  }
115 
116  OperationFactory() {registerNext<OperationType::Type(0)>();}
117  };
118 
119 
120 }
121 
122 using minsky::operator<<;
123 
124 
125 #include "operationType.cd"
126 #include "operationType.xcd"
127 #endif
classdesc::enable_if< classdesc::Not< is_equal< I, N > >, void >::T registerNext()
virtual Base * create() const =0
int numArguments()
number of inputs this operator type has
Base * create(OperationType::Type o) const
create an object of type T<o> on the heap
classdesc::enable_if< is_equal< I, N >, void >::T registerNext()
std::ostream & operator<<(std::ostream &o, OperationType::Type t)
Definition: operationType.h:70
static std::string typeName(int type)
return the symbolic name of type
factory class template, for creating objects of type T<O>. N is the maximum value of O ...
Definition: operationType.h:76
std::vector< std::unique_ptr< CreatorBase > > creators
Definition: operationType.h:84
static Group classify(Type t)