Ilwis-Objects  1.0
GIS and Remote Sensing framework for data access and processing
 All Classes Functions Enumerations Pages
operation.h
1 #ifndef OPERATION_H
2 #define OPERATION_H
3 
4 namespace Ilwis {
5 
6 class OperationExpression;
7 class SymbolTable;
8 
9 class KERNELSHARED_EXPORT OperationImplementation : public Identity
10 {
11 public:
12  enum State{sNOTPREPARED,sPREPARED, sPREPAREFAILED};
13  OperationImplementation() : _prepState(sNOTPREPARED) {}
14  OperationImplementation(quint64 metaid, const Ilwis::OperationExpression &e);
15  virtual ~OperationImplementation() {}
16  const IOperationMetaData& metadata() const;
17  virtual bool execute(ExecutionContext *ctx, SymbolTable& symTable)=0;
18  virtual bool isValid() const;
19  OperationExpression expression() const;
20 
21 protected:
22  IOperationMetaData _metadata;
23  OperationExpression _expression;
24  State _prepState;
25 
26  virtual State prepare(ExecutionContext *ctx, const SymbolTable& symTable) =0;
27 };
28 
29 typedef QScopedPointer<OperationImplementation> SPOperationImplementation;
30 typedef std::function<Ilwis::OperationImplementation *(quint64 metaid, const OperationExpression&)> CreateOperation;
31 
32 class KERNELSHARED_EXPORT Operation
33 {
34 public:
35  Operation() {}
36  virtual ~Operation();
37 
39 
40  Operation& operator=(const Operation& op);
41  SPOperationImplementation& operator->();
42  const SPOperationImplementation& operator->() const;
43  bool isValid() const;
44  template<typename T> static T calculate(const QString& name, const QString& txt){
45  SymbolTable symtbl;
46  bool ok = false;
47  ExecutionContext ctx;
48  OperationExpression o("script " + txt);
49  Operation op(o);
50  try{
51  ok = op->execute(&ctx,symtbl);
52  if(!ok)
53  return T();
54  QVariant var = symtbl.getValue(name);
55  if ( var.isValid()) {
56  return var.value<T>();
57  }
58  } catch (const ErrorObject& err) {
59  kernel()->issues()->log(err.message());
60  return T();
61  }
62  return T();
63  }
64  // helper function
65  static std::nullptr_t registerOperation(quint64 id, Ilwis::CreateOperation op);
66 
67 private:
68  SPOperationImplementation _operation;
69 
70 
71 };
72 
73 }
74 
75 #define NEW_OPERATION(name) \
76  private: \
77 static name *dummy_operation;
78 
79 #define REGISTER_OPERATION(name) \
80  name *name::dummy_operation = Operation::registerOperation(name::createMetadata(),name::create);
81 
82 
83 
84 
85 #endif // OPERATION_H