| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2015 Eivind Kvedalen <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library 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 Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#ifndef EXPRESSION_H |
| 24 |
#define EXPRESSION_H |
| 25 |
|
| 26 |
#include <deque> |
| 27 |
#include <set> |
| 28 |
#include <string> |
| 29 |
|
| 30 |
#include <App/PropertyLinks.h> |
| 31 |
#include <App/ObjectIdentifier.h> |
| 32 |
#include <App/Range.h> |
| 33 |
#include <Base/Exception.h> |
| 34 |
#include <Base/BaseClass.h> |
| 35 |
|
| 36 |
|
| 37 |
namespace Base |
| 38 |
{ |
| 39 |
class Quantity; |
| 40 |
} |
| 41 |
|
| 42 |
namespace App { |
| 43 |
|
| 44 |
class DocumentObject; |
| 45 |
class Expression; |
| 46 |
class Document; |
| 47 |
|
| 48 |
using ExpressionPtr = std::unique_ptr<Expression>; |
| 49 |
|
| 50 |
AppExport bool isAnyEqual(const App::any &v1, const App::any &v2); |
| 51 |
AppExport Base::Quantity anyToQuantity(const App::any &value, const char *errmsg = nullptr); |
| 52 |
|
| 53 |
// clang-format off |
| 54 |
// Map of depending objects to a map of depending property name to the full referencing object identifier |
| 55 |
using ExpressionDeps = std::map<App::DocumentObject*, std::map<std::string, std::vector<ObjectIdentifier> > >; |
| 56 |
// clang-format on |
| 57 |
|
| 58 |
class AppExport ExpressionVisitor { |
| 59 |
public: |
| 60 |
virtual ~ExpressionVisitor() = default; |
| 61 |
virtual void visit(Expression &e) = 0; |
| 62 |
virtual void aboutToChange() {} |
| 63 |
virtual int changed() const { return 0;} |
| 64 |
virtual void reset() {} |
| 65 |
virtual App::PropertyLinkBase* getPropertyLink() {return nullptr;} |
| 66 |
|
| 67 |
protected: |
| 68 |
void getIdentifiers(Expression &e, std::map<App::ObjectIdentifier, bool> &); |
| 69 |
bool adjustLinks(Expression &e, const std::set<App::DocumentObject*> &inList); |
| 70 |
bool relabeledDocument(Expression &e, const std::string &oldName, const std::string &newName); |
| 71 |
bool renameObjectIdentifier(Expression &e, |
| 72 |
const std::map<ObjectIdentifier,ObjectIdentifier> &, const ObjectIdentifier &); |
| 73 |
void collectReplacement(Expression &e, std::map<ObjectIdentifier,ObjectIdentifier> &, |
| 74 |
const App::DocumentObject *parent, App::DocumentObject *oldObj, App::DocumentObject *newObj) const; |
| 75 |
bool updateElementReference(Expression &e, App::DocumentObject *feature,bool reverse); |
| 76 |
void importSubNames(Expression &e, const ObjectIdentifier::SubNameMap &subNameMap); |
| 77 |
void updateLabelReference(Expression &e, App::DocumentObject *obj, |
| 78 |
const std::string &ref, const char *newLabel); |
| 79 |
void moveCells(Expression &e, const CellAddress &address, int rowCount, int colCount); |
| 80 |
void offsetCells(Expression &e, int rowOffset, int colOffset); |
| 81 |
}; |
| 82 |
|
| 83 |
template<class P> class ExpressionModifier : public ExpressionVisitor { |
| 84 |
public: |
| 85 |
explicit ExpressionModifier(P & _prop) |
| 86 |
: prop(_prop) |
| 87 |
, propLink(freecad_cast<App::PropertyLinkBase*>(&prop)) |
| 88 |
, signaller(_prop,false) |
| 89 |
{} |
| 90 |
|
| 91 |
~ExpressionModifier() override = default; |
| 92 |
|
| 93 |
void aboutToChange() override{ |
| 94 |
++_changed; |
| 95 |
signaller.aboutToChange(); |
| 96 |
} |
| 97 |
|
| 98 |
int changed() const override { return _changed; } |
| 99 |
|
| 100 |
void reset() override {_changed = 0;} |
| 101 |
|
| 102 |
App::PropertyLinkBase* getPropertyLink() override {return propLink;} |
| 103 |
|
| 104 |
protected: |
| 105 |
P & prop; |
| 106 |
App::PropertyLinkBase *propLink; |
| 107 |
typename AtomicPropertyChangeInterface<P>::AtomicPropertyChange signaller; |
| 108 |
int _changed{0}; |
| 109 |
}; |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* @brief %Base class for expressions. |
| 114 |
* @ingroup ExpressionFramework |
| 115 |
* |
| 116 |
* @details For a high-level overview of the %Expression framework see topic |
| 117 |
* @ref ExpressionFramework "Expression Framework". |
| 118 |
*/ |
| 119 |
class AppExport Expression : public Base::BaseClass { |
| 120 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); |
| 121 |
|
| 122 |
public: |
| 123 |
|
| 124 |
explicit Expression(const App::DocumentObject * _owner); |
| 125 |
|
| 126 |
~Expression() override; |
| 127 |
|
| 128 |
virtual bool isTouched() const { return false; } |
| 129 |
|
| 130 |
Expression * eval() const; |
| 131 |
|
| 132 |
std::string toString(bool persistent=false, bool checkPriority=false, int indent=0) const; |
| 133 |
void toString(std::ostream &os, bool persistent=false, bool checkPriority=false, int indent=0) const; |
| 134 |
|
| 135 |
static Expression * parse(const App::DocumentObject * owner, const std::string& buffer); |
| 136 |
|
| 137 |
Expression * copy() const; |
| 138 |
|
| 139 |
virtual int priority() const; |
| 140 |
|
| 141 |
void getIdentifiers(std::map<App::ObjectIdentifier,bool> &) const; |
| 142 |
std::map<App::ObjectIdentifier,bool> getIdentifiers() const; |
| 143 |
|
| 144 |
enum DepOption { |
| 145 |
DepNormal, |
| 146 |
DepHidden, |
| 147 |
DepAll, |
| 148 |
}; |
| 149 |
void getDeps(ExpressionDeps &deps, int option=DepNormal) const; |
| 150 |
ExpressionDeps getDeps(int option=DepNormal) const; |
| 151 |
|
| 152 |
std::map<App::DocumentObject*,bool> getDepObjects(std::vector<std::string> *labels=nullptr) const; |
| 153 |
void getDepObjects(std::map<App::DocumentObject*,bool> &, std::vector<std::string> *labels=nullptr) const; |
| 154 |
|
| 155 |
ExpressionPtr importSubNames(const std::map<std::string,std::string> &nameMap) const; |
| 156 |
|
| 157 |
ExpressionPtr updateLabelReference(App::DocumentObject *obj, |
| 158 |
const std::string &ref, const char *newLabel) const; |
| 159 |
|
| 160 |
ExpressionPtr replaceObject(const App::DocumentObject *parent, |
| 161 |
App::DocumentObject *oldObj, App::DocumentObject *newObj) const; |
| 162 |
|
| 163 |
bool adjustLinks(const std::set<App::DocumentObject*> &inList); |
| 164 |
|
| 165 |
virtual Expression * simplify() const = 0; |
| 166 |
|
| 167 |
void visit(ExpressionVisitor & v); |
| 168 |
|
| 169 |
class Exception : public Base::Exception { |
| 170 |
public: |
| 171 |
explicit Exception(const char *sMessage) : Base::Exception(sMessage) { } |
| 172 |
}; |
| 173 |
|
| 174 |
App::DocumentObject * getOwner() const { return owner; } |
| 175 |
struct Component; |
| 176 |
|
| 177 |
virtual void addComponent(Component* component); |
| 178 |
|
| 179 |
using ComponentList = std::vector<Component*>; |
| 180 |
|
| 181 |
static Component *createComponent(const std::string &n); |
| 182 |
static Component *createComponent(Expression *e1, Expression *e2=nullptr, |
| 183 |
Expression *e3=nullptr, bool isRange=false); |
| 184 |
|
| 185 |
bool hasComponent() const {return !components.empty();} |
| 186 |
|
| 187 |
boost::any getValueAsAny() const; |
| 188 |
|
| 189 |
Py::Object getPyValue() const; |
| 190 |
|
| 191 |
bool isSame(const Expression &other, bool checkComment=true) const; |
| 192 |
|
| 193 |
friend class ExpressionVisitor; |
| 194 |
|
| 195 |
protected: |
| 196 |
virtual bool _isIndexable() const {return false;} |
| 197 |
virtual Expression *_copy() const = 0; |
| 198 |
virtual void _toString(std::ostream &ss, bool persistent, int indent=0) const = 0; |
| 199 |
virtual void _getIdentifiers(std::map<App::ObjectIdentifier,bool> &) const {} |
| 200 |
virtual bool _adjustLinks(const std::set<App::DocumentObject*> &, ExpressionVisitor &) {return false;} |
| 201 |
virtual bool _updateElementReference(App::DocumentObject *,bool,ExpressionVisitor &) {return false;} |
| 202 |
virtual bool _relabeledDocument(const std::string &, const std::string &, ExpressionVisitor &) {return false;} |
| 203 |
virtual void _importSubNames(const ObjectIdentifier::SubNameMap &) {} |
| 204 |
virtual void _updateLabelReference(App::DocumentObject *, const std::string &, const char *) {} |
| 205 |
virtual bool _renameObjectIdentifier(const std::map<ObjectIdentifier,ObjectIdentifier> &, |
| 206 |
const ObjectIdentifier &, ExpressionVisitor &) {return false;} |
| 207 |
virtual void _collectReplacement(std::map<ObjectIdentifier,ObjectIdentifier> &, |
| 208 |
const App::DocumentObject *parent, App::DocumentObject *oldObj, App::DocumentObject *newObj) const |
| 209 |
{ |
| 210 |
(void)parent; |
| 211 |
(void)oldObj; |
| 212 |
(void)newObj; |
| 213 |
} |
| 214 |
virtual void _moveCells(const CellAddress &, int, int, ExpressionVisitor &) {} |
| 215 |
virtual void _offsetCells(int, int, ExpressionVisitor &) {} |
| 216 |
virtual Py::Object _getPyValue() const = 0; |
| 217 |
virtual void _visit(ExpressionVisitor &) {} |
| 218 |
|
| 219 |
protected: |
| 220 |
// clang-format off |
| 221 |
App::DocumentObject * owner; /**< The document object used to access unqualified variables (i.e local scope) */ |
| 222 |
|
| 223 |
ComponentList components; |
| 224 |
|
| 225 |
public: |
| 226 |
std::string comment; |
| 227 |
// clang-format on |
| 228 |
}; |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
#endif // EXPRESSION_H |