Skip to content

Commit cb5903c

Browse files
committed
python callbacks for register_function, python_equation
1 parent e1f5726 commit cb5903c

16 files changed

Lines changed: 157 additions & 88 deletions

‎dist/package_macos.sh‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,17 @@ install_name_tool -change $j "@loader_path/../gcc/`basename $j`" $i
6666
done
6767
done
6868

69-
for i in ${DIST_BIN}/devsim_tcl
70-
do
71-
# get otool dependencies from the gcc compiler
72-
for j in `otool -L $i | egrep '\bgcc\b' | sed -e 's/(.*//'`
73-
do
74-
cp -vf $j ${DIST_LIB}/gcc
75-
echo install_name_tool -change $j "@executable_path/../lib/gcc/`basename $j`" $i
76-
install_name_tool -change $j "@executable_path/../lib/gcc/`basename $j`" $i
77-
done
78-
done
69+
#for i in ${DIST_BIN}/devsim_tcl
70+
#do
71+
## get otool dependencies from the gcc compiler
72+
#for j in `otool -L $i | egrep '\bgcc\b' | sed -e 's/(.*//'`
73+
#do
74+
#cp -vf $j ${DIST_LIB}/gcc
75+
#echo install_name_tool -change $j "@executable_path/../lib/gcc/`basename $j`" $i
76+
#install_name_tool -change $j "@executable_path/../lib/gcc/`basename $j`" $i
77+
#done
78+
#done
79+
7980
fi
8081

8182
# strip unneeded symbols

‎src/Data/GlobalData.cc‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,17 @@ void GlobalData::SignalCallbacksOnMaterialChange(const std::string &material_nam
397397
}
398398
}
399399

400-
bool GlobalData::AddTclEquation(const std::string &name, const std::string &procedure, std::string &error)
400+
bool GlobalData::AddTclEquation(const std::string &name, ObjectHolder procedure, std::string &error)
401401
{
402402
bool ret = false;
403403
if (tclEquationList.count(name))
404404
{
405405
error += std::string("Tcl equation \"") + name + "\" already exists";
406406
}
407+
else if (!procedure.IsCallable())
408+
{
409+
error += std::string("Tcl equation \"") + name + "\" does not provide a callable procedure";
410+
}
407411
else
408412
{
409413
ret = true;

‎src/Data/GlobalData.hh‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,9 @@ class GlobalData
8585
void SetInterpreter(void *);
8686
void *GetInterpreter();
8787

88-
bool AddTclEquation(const std::string &/*name*/, const std::string &/*procedure*/, std::string &/*error*/);
88+
bool AddTclEquation(const std::string &/*name*/, ObjectHolder/*procedure*/, std::string &/*error*/);
8989

90-
void DeleteTclEquation(const std::string &/*name*/, const std::string &/*procedure*/);
91-
92-
typedef std::map<std::string, std::string> TclEquationList_t;
90+
typedef std::map<std::string, ObjectHolder> TclEquationList_t;
9391

9492
const TclEquationList_t &GetTclEquationList();
9593

‎src/MathEval/MathEval.cc‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -404,29 +404,37 @@ void MathEval<DoubleType>::EvaluateTclMathFunc(const std::string &func, std::vec
404404

405405
dsAssert(it != tclMathFuncMap_.end(), "UNEXPECTED");
406406

407-
const size_t tclcount = it->second;
407+
const size_t tclcount = (it->second).second;
408408

409409
const size_t cnt = vvals.size();
410410

411-
if (tclcount == cnt)
411+
std::vector<ObjectHolder> tclObjVector;
412+
413+
if (tclcount != cnt)
414+
{
415+
std::ostringstream os;
416+
os << "function registered with \"" << tclcount << "\" arguments and \"" << cnt << "\" were provided: \"" << func << "\"";
417+
error += os.str();
418+
}
419+
else
412420
{
421+
ObjectHolder procedure = (it->second).first;
422+
413423
Interpreter MyInterp;
414424
//// put the objects in here
415-
tclObjVector.resize(cnt + 1);
416-
417-
tclObjVector[0] = ObjectHolder(func);
425+
tclObjVector.resize(cnt);
418426

419427
size_t numvecs = 0;
420428
size_t numelems = 0;
421429
for (size_t i = 0; i < cnt; ++i)
422430
{
423431
if (vvals[i] == nullptr)
424432
{
425-
tclObjVector[i + 1] = ObjectHolder(static_cast<double>(dvals[i]));
433+
tclObjVector[i] = ObjectHolder(static_cast<double>(dvals[i]));
426434
}
427435
else
428436
{
429-
tclObjVector[i + 1].clear();
437+
tclObjVector[i].clear();
430438
++numvecs;
431439
if (numelems == 0)
432440
{
@@ -450,11 +458,11 @@ void MathEval<DoubleType>::EvaluateTclMathFunc(const std::string &func, std::vec
450458

451459
if (isvec)
452460
{
453-
tclObjVector[i+1] = ObjectHolder(static_cast<double>(vvals[i]->operator[](j)));
461+
tclObjVector[i] = ObjectHolder(static_cast<double>(vvals[i]->operator[](j)));
454462
}
455463
}
456464

457-
bool ok = MyInterp.RunCommand(tclObjVector);
465+
bool ok = MyInterp.RunCommand(procedure, tclObjVector);
458466

459467
if (ok)
460468
{
@@ -480,12 +488,6 @@ void MathEval<DoubleType>::EvaluateTclMathFunc(const std::string &func, std::vec
480488
}
481489
}
482490
}
483-
else
484-
{
485-
std::ostringstream os;
486-
os << "function registered with \"" << tclcount << "\" arguments and \"" << cnt << "\" were provided: \"" << func << "\"";
487-
error += os.str();
488-
}
489491
}
490492

491493
template <typename DoubleType>
@@ -523,12 +525,29 @@ void MathEval<DoubleType>::EvaluateMathFunc(const std::string &func, std::vector
523525
}
524526
}
525527

528+
template <typename DoubleType>
529+
bool MathEval<DoubleType>::AddTclMath(const std::string &funcname, ObjectHolder procedure, size_t numargs, std::string &error)
530+
{
531+
if (!procedure.IsCallable())
532+
{
533+
std::ostringstream os;
534+
os << "procedure given for \"" << funcname << "\" is not callable";
535+
error += os.str();
536+
}
537+
else
538+
{
539+
tclMathFuncMap_[funcname] = std::make_pair(procedure, numargs);
540+
}
541+
return error.empty();
542+
}
543+
#if 0
526544
template <typename DoubleType>
527545
void MathEval<DoubleType>::AddTclMath(const std::string &funcname, size_t numargs)
528546
{
529547
///Really need to make sure number of arguments make sense in tcl api
530548
tclMathFuncMap_[funcname] = numargs;
531549
}
550+
#endif
532551

533552
template <typename DoubleType>
534553
void MathEval<DoubleType>::RemoveTclMath(const std::string &funcname)

‎src/MathEval/MathEval.hh‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class MathEval {
4040
static MathEval &GetInstance();
4141
static void DestroyInstance();
4242

43-
void AddTclMath(const std::string &, size_t);
43+
bool AddTclMath(const std::string &, ObjectHolder, size_t, std::string & /*error_string*/);
4444
void RemoveTclMath(const std::string &);
4545

4646
private:
@@ -52,9 +52,8 @@ class MathEval {
5252
static MathEval *instance_;
5353
std::map<std::string, Eqomfp::MathWrapperPtr<DoubleType>> FuncPtrMap_;
5454

55-
typedef std::map<std::string, size_t> tclMathFuncMap_t;
55+
typedef std::map<std::string, std::pair<ObjectHolder, size_t> > tclMathFuncMap_t;
5656
tclMathFuncMap_t tclMathFuncMap_;
57-
mutable std::vector<ObjectHolder> tclObjVector;
5857

5958
void InitializeBuiltInMathFunc();
6059
};

‎src/commands/EquationCommands.cc‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ createCustomEquationCmd(CommandHandler &data)
712712
static dsGetArgs::Option option[] =
713713
{
714714
{"name", "", dsGetArgs::optionType::STRING, dsGetArgs::requiredType::REQUIRED, stringCannotBeEmpty},
715-
{"procedure", "", dsGetArgs::optionType::STRING, dsGetArgs::requiredType::REQUIRED, stringCannotBeEmpty},
715+
{"procedure", "", dsGetArgs::optionType::STRING, dsGetArgs::requiredType::REQUIRED},
716716
{nullptr, nullptr, dsGetArgs::optionType::STRING, dsGetArgs::requiredType::OPTIONAL}
717717
};
718718

@@ -727,10 +727,11 @@ createCustomEquationCmd(CommandHandler &data)
727727
return;
728728
}
729729

730-
const std::string &name = data.GetStringOption("name");
731-
const std::string &procedure = data.GetStringOption("procedure");
730+
const std::string &name = data.GetStringOption("name");
731+
auto procedure = data.GetObjectHolder("procedure");
732732

733733
gdata.AddTclEquation(name, procedure, errorString);
734+
734735
data.SetEmptyResult();
735736

736737
if (!errorString.empty())

‎src/commands/MaterialCommands.cc‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -228,21 +228,9 @@ getParameterCmd(CommandHandler &data)
228228
names = gdata.GetDBEntryListOnGlobal();
229229
}
230230

231-
// if (!names.empty())
232231
{
233232
data.SetStringListResult(names);
234233
}
235-
/*
236-
else
237-
{
238-
std::ostringstream os;
239-
os << "Cannot find any parameters\n";
240-
errorString = os.str();
241-
error = TCL_ERROR;
242-
data.SetErrorResult(errorString);
243-
return error;
244-
}
245-
*/
246234
}
247235
else if (commandName == "set_parameter")
248236
{

‎src/commands/MathCommands.cc‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,6 @@ solveCmdImpl(CommandHandler &data)
100100
return;
101101
}
102102

103-
#if 0
104-
const std::string &callback = data.GetStringOption("callback");
105-
if (!callback.empty())
106-
{
107-
errorString = "callback is not implemented yet\n";
108-
Tcl_SetResult(interp, const_cast<char *>(errorString.c_str()), TCL_VOLATILE);
109-
return TCL_ERROR;
110-
}
111-
#endif
112-
113103
const DoubleType charge_error = data.GetDoubleOption("charge_error");
114104
const DoubleType absolute_error = data.GetDoubleOption("absolute_error");
115105
const DoubleType relative_error = data.GetDoubleOption("relative_error");

‎src/commands/ModelCommands.cc‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,9 +2027,10 @@ registerFunctionCmd(CommandHandler &data)
20272027
using namespace dsGetArgs;
20282028
static dsGetArgs::Option option[] =
20292029
{
2030-
{"name", "", dsGetArgs::optionType::STRING, dsGetArgs::requiredType::REQUIRED},
2031-
{"nargs", "", dsGetArgs::optionType::INTEGER, dsGetArgs::requiredType::REQUIRED},
2032-
{nullptr, nullptr, dsGetArgs::optionType::STRING, dsGetArgs::requiredType::OPTIONAL}
2030+
{"name", "", dsGetArgs::optionType::STRING, dsGetArgs::requiredType::REQUIRED},
2031+
{"procedure", "", dsGetArgs::optionType::STRING, dsGetArgs::requiredType::REQUIRED},
2032+
{"nargs", "", dsGetArgs::optionType::INTEGER, dsGetArgs::requiredType::REQUIRED},
2033+
{nullptr, nullptr, dsGetArgs::optionType::STRING, dsGetArgs::requiredType::OPTIONAL}
20332034
};
20342035

20352036
dsGetArgs::switchList switches = nullptr;
@@ -2046,7 +2047,8 @@ registerFunctionCmd(CommandHandler &data)
20462047
// const std::string commandName = data.GetCommandName();
20472048

20482049
const std::string &name = data.GetStringOption("name");
2049-
const int nargs = data.GetIntegerOption("nargs");
2050+
const int nargs = data.GetIntegerOption("nargs");
2051+
ObjectHolder procedure = data.GetObjectHolder("procedure");
20502052

20512053
int num = nargs;
20522054

@@ -2056,7 +2058,15 @@ registerFunctionCmd(CommandHandler &data)
20562058
data.SetErrorResult(errorString);
20572059
return;
20582060
}
2059-
MathEval<double>::GetInstance().AddTclMath(name, static_cast<size_t>(nargs));
2061+
2062+
MathEval<double>::GetInstance().AddTclMath(name, procedure, static_cast<size_t>(nargs), errorString);
2063+
2064+
if (!errorString.empty())
2065+
{
2066+
data.SetErrorResult(errorString);
2067+
return;
2068+
}
2069+
20602070
data.SetEmptyResult();
20612071
}
20622072

‎src/common_api/Interpreter.hh‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,23 @@ class Interpreter
2727
Interpreter();
2828
~Interpreter();
2929

30+
public:
31+
#if 0
3032
bool RunCommand(std::vector<ObjectHolder> &/*command*/);
33+
#endif
34+
bool RunCommand(ObjectHolder & /*command*/, std::vector<ObjectHolder> &/*arguments*/);
35+
#if 0
3136
bool RunCommand(const std::string &/*command*/, const std::vector<std::string> &/*arguments*/);
32-
bool RunCommand(const std::string &/*command*/, const std::vector<std::pair<std::string, ObjectHolder> > &/*arguments*/);
37+
#endif
3338
bool RunInternalCommand(const std::string &/*command*/, const std::vector<std::pair<std::string, ObjectHolder> > &/*arguments*/);
39+
40+
private:
41+
bool RunCommand(const std::string &/*command*/, const std::vector<std::pair<std::string, ObjectHolder> > &/*arguments*/);
42+
#if 0
3443
bool RunCommand(const std::string &/*command*/);
44+
#endif
45+
46+
public:
3547
const std::string &GetErrorString() const
3648
{
3749
return error_string_;
@@ -44,8 +56,8 @@ class Interpreter
4456

4557
std::string GetVariable(const std::string &name);
4658

47-
4859
private:
60+
4961
Interpreter &operator=(const Interpreter &);
5062
Interpreter(const Interpreter &);
5163

0 commit comments

Comments
 (0)