Skip to content

Commit 9b033d6

Browse files
authored
Made our SedInstance API more consistent.
2 parents 98eeb7c + 3247d4b commit 9b033d6

File tree

11 files changed

+102
-14
lines changed

11 files changed

+102
-14
lines changed

src/api/libopencor/file.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class LIBOPENCOR_EXPORT File: public Logger
189189
*
190190
* Return the child file names. This method is only relevant for COMBINE archives.
191191
*
192-
* @return The child file names, as a @c std::vector of @c std::string.
192+
* @return The child file names, as a @ref Strings.
193193
*/
194194

195195
Strings childFileNames() const;
@@ -199,7 +199,7 @@ class LIBOPENCOR_EXPORT File: public Logger
199199
*
200200
* Return the child files. This method is only relevant for COMBINE archives.
201201
*
202-
* @return The child files, as a @c std::vector of @ref FilePtr.
202+
* @return The child files, as a @ref FilePtrs.
203203
*/
204204

205205
FilePtrs childFiles() const;

src/api/libopencor/seddocument.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class LIBOPENCOR_EXPORT SedDocument: public Logger
115115
*
116116
* Return the models.
117117
*
118-
* @return The models, as a @c std::vector of @ref SedModelPtr.
118+
* @return The models, as a @ref SedModelPtrs.
119119
*/
120120

121121
SedModelPtrs models() const;
@@ -181,7 +181,7 @@ class LIBOPENCOR_EXPORT SedDocument: public Logger
181181
*
182182
* Return the simulations.
183183
*
184-
* @return The simulations, as a @c std::vector of @ref SedSimulationPtr.
184+
* @return The simulations, as a @ref SedSimulationPtrs.
185185
*/
186186

187187
SedSimulationPtrs simulations() const;
@@ -247,7 +247,7 @@ class LIBOPENCOR_EXPORT SedDocument: public Logger
247247
*
248248
* Return the tasks.
249249
*
250-
* @return The tasks, as a @c std::vector of @ref SedAbstractTaskPtr.
250+
* @return The tasks, as a @ref SedAbstractTaskPtrs.
251251
*/
252252

253253
SedAbstractTaskPtrs tasks() const;

src/api/libopencor/sedinstance.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ class LIBOPENCOR_EXPORT SedInstance: public Logger
5151

5252
void run();
5353

54+
/**
55+
* @brief Return whether there are some tasks.
56+
*
57+
* Return whether there are some tasks.
58+
*
59+
* @return @c true if there are some tasks, @c false otherwise.
60+
*/
61+
62+
bool hasTasks() const;
63+
64+
/**
65+
* @brief Return the number of tasks.
66+
*
67+
* Return the number of tasks.
68+
*
69+
* @return The number of tasks.
70+
*/
71+
72+
size_t taskCount() const;
73+
5474
/**
5575
* @brief Return the tasks.
5676
*
@@ -61,6 +81,18 @@ class LIBOPENCOR_EXPORT SedInstance: public Logger
6181

6282
SedInstanceTaskPtrs tasks() const;
6383

84+
/**
85+
* @brief Return the task at the given index.
86+
*
87+
* Return the task at the given index.
88+
*
89+
* @param pIndex The index of the task to return.
90+
*
91+
* @return The task as a @ref SedInstanceTaskPtr, if the index is valid, @c nullptr otherwise.
92+
*/
93+
94+
SedInstanceTaskPtr task(size_t pIndex) const;
95+
6496
private:
6597
class Impl; /**< Forward declaration of the implementation class, @private. */
6698

src/api/libopencor/sedinstancetask.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class LIBOPENCOR_EXPORT SedInstanceTask: public Logger
9595
*
9696
* @param pIndex The index of the state.
9797
*
98-
* @return The values of the state, as a @c std::vector of @c double, if the index is valid, an empty vector otherwise.
98+
* @return The values of the state, as a @ref Doubles, if the index is valid, an empty vector otherwise.
9999
*/
100100

101101
Doubles state(size_t pIndex) const;
@@ -145,7 +145,7 @@ class LIBOPENCOR_EXPORT SedInstanceTask: public Logger
145145
*
146146
* @param pIndex The index of the rate.
147147
*
148-
* @return The values of the rate, as a @c std::vector of @c double, if the index is valid, an empty vector otherwise.
148+
* @return The values of the rate, as a @ref Doubles, if the index is valid, an empty vector otherwise.
149149
*/
150150

151151
Doubles rate(size_t pIndex) const;
@@ -195,7 +195,7 @@ class LIBOPENCOR_EXPORT SedInstanceTask: public Logger
195195
*
196196
* @param pIndex The index of the constant.
197197
*
198-
* @return The values of the constant, as a @c std::vector of @c double, if the index is valid, an empty vector otherwise.
198+
* @return The values of the constant, as a @ref Doubles, if the index is valid, an empty vector otherwise.
199199
*/
200200

201201
Doubles constant(size_t pIndex) const;
@@ -245,8 +245,7 @@ class LIBOPENCOR_EXPORT SedInstanceTask: public Logger
245245
*
246246
* @param pIndex The index of the computed constant.
247247
*
248-
* @return The values of the computed constant, as a @c std::vector of @c double, if the index is valid, an empty
249-
* vector otherwise.
248+
* @return The values of the computed constant, as a @ref Doubles, if the index is valid, an empty vector otherwise.
250249
*/
251250

252251
Doubles computedConstant(size_t pIndex) const;
@@ -296,8 +295,7 @@ class LIBOPENCOR_EXPORT SedInstanceTask: public Logger
296295
*
297296
* @param pIndex The index of the algebraic variable.
298297
*
299-
* @return The values of the algebraic variable, as a @c std::vector of @c double, if the index is valid, an empty
300-
* vector otherwise.
298+
* @return The values of the algebraic variable, as a @c Doubles, if the index is valid, an empty vector otherwise.
301299
*/
302300

303301
Doubles algebraic(size_t pIndex) const;

src/bindings/javascript/sed.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ void sedApi()
6868
emscripten::class_<libOpenCOR::SedInstance, emscripten::base<libOpenCOR::Logger>>("SedInstance")
6969
.smart_ptr<libOpenCOR::SedInstancePtr>("SedInstance")
7070
.function("run", &libOpenCOR::SedInstance::run)
71-
.property("tasks", &libOpenCOR::SedInstance::tasks);
71+
.property("hasTasks", &libOpenCOR::SedInstance::hasTasks)
72+
.property("taskCount", &libOpenCOR::SedInstance::taskCount)
73+
.property("tasks", &libOpenCOR::SedInstance::tasks)
74+
.function("task", &libOpenCOR::SedInstance::task);
7275

7376
// SedInstanceTask API.
7477

src/bindings/python/sed.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ void sedApi(nb::module_ &m)
7676
nb::class_<libOpenCOR::SedInstance, libOpenCOR::Logger> sedInstance(m, "SedInstance");
7777

7878
sedInstance.def("run", &libOpenCOR::SedInstance::run, "Run the tasks associated with this SedInstance object.")
79-
.def_prop_ro("tasks", &libOpenCOR::SedInstance::tasks, "Return the tasks.");
79+
.def_prop_ro("has_tasks", &libOpenCOR::SedInstance::hasTasks, "Return whether there are some tasks.")
80+
.def_prop_ro("task_count", &libOpenCOR::SedInstance::taskCount, "Return the number of tasks.")
81+
.def_prop_ro("tasks", &libOpenCOR::SedInstance::tasks, "Return the tasks.")
82+
.def("task", &libOpenCOR::SedInstance::task, "Return the task.");
8083

8184
// SedInstanceTask API.
8285

src/sed/sedinstance.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,30 @@ void SedInstance::Impl::run()
8787
}
8888
}
8989

90+
bool SedInstance::Impl::hasTasks() const
91+
{
92+
return !mTasks.empty();
93+
}
94+
95+
size_t SedInstance::Impl::taskCount() const
96+
{
97+
return mTasks.size();
98+
}
99+
90100
SedInstanceTaskPtrs SedInstance::Impl::tasks() const
91101
{
92102
return mTasks;
93103
}
94104

105+
SedInstanceTaskPtr SedInstance::Impl::task(size_t pIndex) const
106+
{
107+
if (pIndex >= mTasks.size()) {
108+
return {};
109+
}
110+
111+
return mTasks[pIndex];
112+
}
113+
95114
SedInstance::SedInstance(const SedDocumentPtr &pDocument, bool pCompiled)
96115
: Logger(new Impl(pDocument, pCompiled))
97116
{
@@ -117,9 +136,24 @@ void SedInstance::run()
117136
pimpl()->run();
118137
}
119138

139+
bool SedInstance::hasTasks() const
140+
{
141+
return pimpl()->hasTasks();
142+
}
143+
144+
size_t SedInstance::taskCount() const
145+
{
146+
return pimpl()->taskCount();
147+
}
148+
120149
SedInstanceTaskPtrs SedInstance::tasks() const
121150
{
122151
return pimpl()->tasks();
123152
}
124153

154+
SedInstanceTaskPtr SedInstance::task(size_t pIndex) const
155+
{
156+
return pimpl()->task(pIndex);
157+
}
158+
125159
} // namespace libOpenCOR

src/sed/sedinstance_p.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ class SedInstance::Impl: public Logger::Impl
3333

3434
void run();
3535

36+
bool hasTasks() const;
37+
size_t taskCount() const;
3638
SedInstanceTaskPtrs tasks() const;
39+
SedInstanceTaskPtr task(size_t pIndex) const;
3740
};
3841

3942
} // namespace libOpenCOR

tests/api/sed/coveragetests.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ TEST(CoverageSedTest, sedInstanceAndSedInstanceTask)
289289
auto instance = document->instantiate();
290290
auto instanceTask = instance->tasks()[0];
291291

292+
EXPECT_EQ(instance->hasTasks(), true);
293+
EXPECT_EQ(instance->taskCount(), 1);
294+
EXPECT_EQ(instance->task(0), instanceTask);
295+
EXPECT_EQ(instance->task(1), nullptr);
296+
292297
static const auto NoDoubles = std::vector<double> {};
293298

294299
EXPECT_EQ(instanceTask->voi(), NoDoubles);

tests/bindings/javascript/sed.coverage.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ describe("Sed coverage tests", () => {
303303
const instance = document.instantiate();
304304
const instanceTask = instance.tasks.get(0);
305305

306+
expect(instance.hasTasks).toBe(true);
307+
expect(instance.taskCount).toBe(1);
308+
expect(instance.task(0)).toStrictEqual(instanceTask);
309+
expect(instance.task(1)).toStrictEqual(null);
310+
306311
expect(instanceTask.voi.size()).toBe(0);
307312
expect(instanceTask.voiAsArray).toStrictEqual([]);
308313
expect(instanceTask.voiName).toBe("environment/time");

0 commit comments

Comments
 (0)