Skip to content

Commit 8092a8e

Browse files
author
Dilawar Singh
committed
If a Shell function retuns Id, wrap it in a MooseVec. The current API
has this behaviour.
1 parent 8ca430c commit 8092a8e

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

pybind11/MooseVec.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ MooseVec::MooseVec(const ObjId& oid) : oid_(oid), path_(oid.path())
3434
{
3535
}
3636

37+
MooseVec::MooseVec(const Id& id) : oid_(ObjId(id)), path_(id.path())
38+
{
39+
}
40+
3741
const string MooseVec::dtype() const
3842
{
3943
return oid_.element()->cinfo()->name();

pybind11/MooseVec.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
#ifndef MOOSE_VEC_H
1111
#define MOOSE_VEC_H
1212

13-
class MooseVec {
13+
class MooseVec
14+
{
1415

1516
public:
1617
MooseVec(const string& path, unsigned int n, const string& dtype);
1718

19+
MooseVec(const Id& id);
20+
1821
MooseVec(const ObjId& oid);
1922

2023
const ObjId& obj() const;

pybind11/helper.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "../external/pybind11/include/pybind11/pybind11.h"
2323
#include "../external/pybind11/include/pybind11/stl.h"
2424

25+
namespace py = pybind11;
26+
2527
// See
2628
// https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html#binding-stl-containers
2729
// #include "../external/pybind11/include/pybind11/stl_bind.h"
@@ -44,8 +46,6 @@
4446

4547
using namespace std;
4648

47-
namespace py = pybind11;
48-
4949
using namespace std;
5050

5151
Id initShell(void)
@@ -138,14 +138,12 @@ ObjId createIdFromPath(string path, string type, unsigned int numData)
138138
return nId;
139139
}
140140

141-
142141
bool mooseExists(const string& p)
143142
{
144143
string path = moose::normalizePath(p);
145144
return Id(path) != Id() || path == "/" || path == "/root";
146145
}
147146

148-
149147
ObjId loadModelInternal(const string& fname, const string& modelpath,
150148
const string& solverclass = "")
151149
{
@@ -215,7 +213,6 @@ void mooseMoveObjId(const ObjId& a, const ObjId& b)
215213
getShellPtr()->doMove(a.id, b);
216214
}
217215

218-
219216
void mooseSetClock(const unsigned int clockId, double dt)
220217
{
221218
getShellPtr()->doSetClock(clockId, dt);
@@ -243,10 +240,9 @@ py::object mooseGetCwe()
243240
return py::cast(mooseGetCweId());
244241
}
245242

246-
247243
void mooseSetCwe(const py::object& arg)
248244
{
249-
if(py::isinstance<py::str>(arg))
245+
if (py::isinstance<py::str>(arg))
250246
return getShellPtr()->setCwe(ObjId(arg.cast<string>()));
251247
return getShellPtr()->setCwe(arg.cast<ObjId>());
252248
}
@@ -319,13 +315,14 @@ void mooseStop()
319315
getShellPtr()->doStop();
320316
}
321317

322-
ObjId mooseCopy(const py::object& elem, ObjId newParent, string newName,
323-
unsigned int n = 1, bool toGlobal = false,
324-
bool copyExtMsgs = false)
318+
// Id is synonym with Id in previous binding.
319+
MooseVec mooseCopy(const py::object& elem, ObjId newParent, string newName,
320+
unsigned int n = 1, bool toGlobal = false,
321+
bool copyExtMsgs = false)
325322
{
326323
Id orig = py::cast<Id>(elem);
327-
return ObjId(getShellPtr()->doCopy(orig, newParent, newName, n, toGlobal,
328-
copyExtMsgs));
324+
return MooseVec(getShellPtr()->doCopy(orig, newParent, newName, n, toGlobal,
325+
copyExtMsgs));
329326
}
330327

331328
/**

pybind11/helper.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include "../shell/Shell.h"
2121

22+
#include "MooseVec.h"
23+
2224
namespace py = pybind11;
2325
using namespace std;
2426

@@ -134,7 +136,7 @@ inline bool mooseDeleteStr(const string& path)
134136
ObjId mooseCreate(const string type, const string& path,
135137
unsigned int numdata = 1);
136138

137-
ObjId mooseCopy(const py::object& orig, ObjId newParent, string newName,
139+
MooseVec mooseCopy(const py::object& orig, ObjId newParent, string newName,
138140
unsigned int n, bool toGlobal, bool copyExtMsgs);
139141

140142

pybind11/pymoose.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ PYBIND11_MODULE(_moose, m)
178178
.def("__getitem__", &__Finfo__::getItem)
179179
.def("__setitem__", &__Finfo__::setItem);
180180

181+
// A thin wrapper around Id from ../basecode/Id.h . Usually this is shows
182+
// at moose.vec.
181183
py::class_<Id>(m, "_Id")
182184
.def(py::init<>())
183185
.def(py::init<unsigned int>())

0 commit comments

Comments
 (0)