-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathparameter.h
More file actions
642 lines (583 loc) · 24.9 KB
/
Copy pathparameter.h
File metadata and controls
642 lines (583 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2026 Team Dissolve and contributors
#pragma once
#include "base/context.h"
#include "base/enumOptions.h"
#include "base/serialiser.h"
#include "math/data1D.h"
#include "nodes/number.h"
#include "templates/algorithms.h"
#include "templates/flags.h"
#include <stdexcept>
#include <string>
#include <typeindex>
#include <vector>
// Forward Declarations
class Node;
class ParameterBase;
template <typename T> class Parameter;
template <typename T> class SerialisableParameter;
// Parameter Proxy
template <class T> class ParameterProxy
{
public:
T data;
};
// Parameter Link Data
struct ParameterLink
{
// Input parameter side
std::shared_ptr<ParameterBase> inputParameter;
// Output parameter side
std::shared_ptr<ParameterBase> outputParameter;
};
// Base type for all parameter templates to inherit from
class ParameterBase : public Serialisable
{
public:
ParameterBase(Node *parent, std::string_view name, std::string_view description, std::type_index storedDataType);
virtual ~ParameterBase() = default;
// Parameter Flags
enum ParameterFlags
{
Required, /* Indicates that this (input) has no default value and must have a link */
NoUpdate, /* Indicates that changing the parameters value does not warrant a node update */
ClearData, /* Indicates that any local data should be cleared if the parameter is changed */
Input, /* Indicates that the parameter is meant to be a sink for data and not a source */
Output, /* Indicates that the parameter is meant to be a source of data and not a sink */
};
// Allowed Edge Count
enum AllowedEdgeCount
{
Zero, /* No edges are allowed */
One, /* Exactly one edge is allowed */
AnyNumber /* Any number of edges is allowed */
};
/*
* Definition
*/
protected:
// The owner of the parameter
Node *parent_;
// Name of the parameter
std::string name_;
// Description of parameter (used as tooltip in the GUI)
std::string description_;
// Stored data type in the parameter
std::type_index storedDataType_;
// Flags for the parameter
Flags<ParameterBase::ParameterFlags> flags_;
public:
// Set node parent
void setParent(Node *parent);
// Return the parameter name
std::string_view name() const;
// Return the parameter description
std::string_view description() const;
// Return the stored data type
std::type_index storedDataType() const;
// Return the owner of the parameter
Node *parent() const;
// Set flag(s) for the parameter
void setFlags(const Flags<ParameterBase::ParameterFlags> &flags);
// Remove flag for the parameter
void removeFlag(ParameterBase::ParameterFlags flag);
// Return current flags
const Flags<ParameterBase::ParameterFlags> &flags() const;
// Return the number of allowed input edges
virtual AllowedEdgeCount nAllowedInputEdges() const = 0;
/*
* Data
*/
public:
// Return whether the contained data is an instance of std::vector
virtual bool isVector() const { return false; }
// Flag that an update is required in the parent node
void setParentUpdateRequired() const;
// Clear data in the parent node
void clearDataInParent() const;
// Mark edges for re-pull in parent node
void markIncomingEdgesForPull() const;
// Perform any updates after a successful setData()
void updateAfterSet() const;
// Return whether this datatype accepts the specified one
virtual bool acceptsDataFromSource(ParameterBase *source) = 0;
// Return whether this datatype provides the specified one
virtual bool providesDataType(std::type_index id) = 0;
// Assign the value of another parameter to this one
virtual bool assignDataFromSource(ParameterBase *source) = 0;
// Assign the value of this parameter to the target one
virtual bool assignDataTo(ParameterBase *destination) = 0;
// The type's representation as a raw int (only valid for int and enum)
virtual int getAsInt() const { return -1; }
// Set type's representation as a raw int (only valid for int and enum)
virtual void setFromInt(int value) { return; }
// Get the parameter's value
template <typename DataClass> DataClass get()
{
// Requested DataClass must always match the storedDataType_, regardless of the underlying parameter type
if (std::type_index(typeid(DataClass)) != storedDataType_)
throw(std::runtime_error(std::format("ParameterBase::get() called with wrong type ({} vs {}), name = {}\n",
std::type_index(typeid(DataClass)).name(), storedDataType_.name(), name_)));
// Upcast to Parameter<T> (common base of all parameter types)
auto cast = dynamic_cast<Parameter<DataClass> *>(this);
if (!cast)
throw(std::runtime_error(std::format("ParameterBase::get() failed to cast, name = {}.\n", name_)));
return cast->getData();
}
// Set the parameter's value
template <typename DataClass> void set(const DataClass &data)
{
// Requested DataClass must always match the storedDataType_, regardless of the underlying parameter type
if (std::type_index(typeid(DataClass)) != storedDataType_)
throw(std::runtime_error(std::format("ParameterBase::set() called with wrong type ({} vs {}), name = {}\n",
std::type_index(typeid(DataClass)).name(), storedDataType_.name(), name_)));
// Upcast to Parameter<T> (common base of all parameter types)
auto cast = dynamic_cast<Parameter<DataClass> *>(this);
if (!cast)
throw(std::runtime_error(std::format("ParameterBase::set() failed to cast, name = {}.\n", name_)));
cast->setData(data);
}
// Invalidate the vector data (instances of std::vector only)
virtual void invalidateVector() {}
// Create a parameter link (input - data proxy - output) for the derived class type
virtual ParameterLink createParameterLink(std::string_view newName, std::string_view newDescription = "") const = 0;
/*
* Serialisation
*/
public:
// Express as a serialised value
virtual void serialise(std::string tag, SerialisedValue &target) const override {}
// Read from a serialised value
virtual void deserialise(const SerialisedValue &node) override { return; }
};
namespace ParameterFactory
{
template <typename DataClass>
std::shared_ptr<ParameterBase> create(Node *parent, std::string_view name, std::string_view description, DataClass &value)
{
return std::make_shared<Parameter<DataClass>>(parent, name, description, value);
}
template <typename DataClass>
std::shared_ptr<ParameterBase> createPointer(Node *parent, std::string_view name, std::string_view description,
DataClass &fromObject)
{
return std::make_shared<Parameter<DataClass *>>(parent, name, description, fromObject);
}
template <typename DataClass>
std::shared_ptr<ParameterBase> createPointer(Node *parent, std::string_view name, std::string_view description,
std::optional<DataClass> &fromOptional)
{
return std::make_shared<Parameter<DataClass *>>(parent, name, description, fromOptional);
}
template <typename DataClass>
std::shared_ptr<ParameterBase> createSerialisable(Node *parent, std::string_view name, std::string_view description,
DataClass &value)
{
return std::make_shared<SerialisableParameter<DataClass>>(parent, name, description, value);
}
}; // namespace ParameterFactory
// Parameter Data based on std::variant
template <class... Ts> class VariantParameterData
{
public:
// Variant data
std::variant<std::monostate, Ts...> data;
// Return whether the type_index provided matches one of the supplied variant's allowed types
bool isAlternative(std::type_index id) const { return ((id == typeid(Ts)) || ...); };
// Emplace the data of the source parameter into the variant
bool emplaceFrom(ParameterBase *source)
{
return ((source->storedDataType() == typeid(Ts) ? (data = source->get<Ts>()), true : false) || ...);
}
// Emplace the variant contents into the destination parameter
bool emplaceInto(ParameterBase *destination) const
{
// Double-fold - check first for straight assignment of an alternative, then for std::optional of the same.
return ((destination->storedDataType() == typeid(Ts) ? destination->set<Ts>(std::get<Ts>(data)), true : false) ||
...) ||
((destination->storedDataType() == typeid(std::optional<Ts>)
? destination->set<std::optional<Ts>>(std::get<Ts>(data)),
true : false) ||
...);
}
// Return whether the variant is empty
bool isEmpty() const { return data.index() == 0; }
// Check for null pointer in variant, ignoring other types
bool hasNullPointer()
{
return std::visit(
[](auto &&arg) -> bool
{
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_pointer_v<T>)
{
return arg == nullptr;
}
return false;
},
data);
}
};
// Primary type for a Parameter to a specific DataClass
template <typename DataClass> class Parameter : public ParameterBase, public std::enable_shared_from_this<Parameter<DataClass>>
{
public:
Parameter(Node *parent, std::string_view name, std::string_view description, DataClass &value)
requires(is_instance_of_v<DataClass, std::vector>)
: ParameterBase(parent, name, description, std::type_index(typeid(DataClass))), data_(value), default_(value)
{
}
Parameter(Node *parent, std::string_view name, std::string_view description, std::remove_pointer_t<DataClass> &value)
requires(std::is_pointer_v<DataClass>)
: ParameterBase(parent, name, description, std::type_index(typeid(DataClass))), data_(localPointer_), default_(nullptr),
dataGetter_([&]() { return &value; }), dataSetter_([](const DataClass &value) { return false; })
{
}
Parameter(Node *parent, std::string_view name, std::string_view description,
std::optional<std::remove_pointer_t<DataClass>> &targetData)
requires(std::is_pointer_v<DataClass>)
: ParameterBase(parent, name, description, std::type_index(typeid(DataClass))), data_(localPointer_), default_(nullptr),
dataGetter_([&]() { return targetData.has_value() ? &targetData.value() : nullptr; }),
dataSetter_([](const DataClass &value) { return false; })
{
}
Parameter(Node *parent, std::string_view name, std::string_view description, DataClass &value)
: ParameterBase(parent, name, description, std::type_index(typeid(DataClass))), data_(value), default_(value)
{
}
Parameter(Node *parent, std::string_view name, std::string_view description,
std::shared_ptr<ParameterProxy<DataClass>> &proxy)
: ParameterBase(parent, name, description, std::type_index(typeid(DataClass))), data_(proxy->data),
default_(proxy->data)
{
// Store the proxy data smart pointer to preserve the lifetime of the data
proxyData_ = proxy;
}
virtual ~Parameter() = default;
/*
* Definition
*/
public:
// Return the number of allowed input edges
AllowedEdgeCount nAllowedInputEdges() const override
{
if (flags_.isSet(ParameterFlags::Output))
return AllowedEdgeCount::Zero;
else if constexpr (is_instance_of_v<DataClass, std::vector>)
return AllowedEdgeCount::AnyNumber;
else
return AllowedEdgeCount::One;
}
// The type's representation as a raw int (only valid for int and enum)
int getAsInt() const override
{
if constexpr (std::is_integral_v<DataClass>)
return data_;
if constexpr (std::is_enum_v<DataClass>)
return static_cast<int>(data_);
Messenger::exception("Tried to extract int value from non integral type {}", storedDataType_.name());
}
// Set type's representation as a raw int (only valid for int and enum)
void setFromInt(int value) override
{
if constexpr (std::is_integral_v<DataClass>)
data_ = value;
else if constexpr (std::is_enum_v<DataClass>)
data_ = static_cast<DataClass>(value);
Messenger::exception("Tried to extract int value from non integral type {}", storedDataType_.name());
}
/*
* Data
*/
protected:
// Reference to target data
DataClass &data_;
// Specialised container for local pointer referencing, if relevant
std::conditional_t<std::is_pointer_v<DataClass>, DataClass, bool> localPointer_;
// Getter for target data, defaulting so simple return of data_ reference member
using DataGetter = std::function<DataClass()>;
DataGetter dataGetter_{[&]() { return data_; }};
// Setter for target data, defaulting to simple 1-to-1 copy as long as equality fails
using DataSetter = std::function<void(const DataClass &value)>;
DataSetter dataSetter_{[&](const DataClass &value) { data_ = value; }};
// Initial value
const DataClass default_;
// Parameter proxy data (if a ParameterLink)
std::shared_ptr<ParameterProxy<DataClass>> proxyData_;
public:
// Return whether the contained data is an instance of std::vector
bool isVector() const override
{
if constexpr (is_instance_of_v<DataClass, std::vector>)
return true;
return false;
}
// Return whether this datatype accepts data from the specified source
bool acceptsDataFromSource(ParameterBase *source) override
{
if (source->providesDataType(storedDataType_))
return true;
// Optionals and vectors also accept single value_type
if constexpr (is_instance_of_v<DataClass, std::optional> || is_instance_of_v<DataClass, std::vector>)
return source->providesDataType(typeid(typename DataClass::value_type));
// Variants accept multiple data types
if constexpr (is_instance_of_v<DataClass, VariantParameterData>)
return data_.isAlternative(source->storedDataType());
return false;
}
// Return whether this datatype provides the specified one
bool providesDataType(std::type_index id) override
{
if (id == storedDataType_)
return true;
// Optionals can provide the plain value_type
if constexpr (is_instance_of_v<DataClass, std::optional>)
return id == typeid(typename DataClass::value_type);
// A Variant might contain the right data as one of its alternatives
if constexpr (is_instance_of_v<DataClass, VariantParameterData>)
return data_.isAlternative(id);
return false;
}
// Assign the value of another parameter to this one
bool assignDataFromSource(ParameterBase *source) override
{
if constexpr (is_instance_of_v<DataClass, std::vector>)
{
// If we represent a std::vector container we can conditionally check for a single data item being passed
// Vector to vector
if (storedDataType_ == source->storedDataType())
{
setData(source->get<DataClass>());
return true;
}
// Single value to vector
if (std::type_index(typeid(typename DataClass::value_type)) == source->storedDataType())
{
data_.push_back(source->get<typename DataClass::value_type>());
updateAfterSet();
return true;
}
}
else if constexpr (is_instance_of_v<DataClass, std::optional>)
{
// Optional arguments can be set from the base class (i.e. with no std::optional container) as well as std::optional
// or a variant containing the correct data type
// Optional to optional
if (storedDataType_ == source->storedDataType())
{
setData(source->get<DataClass>());
return true;
}
// Base type into std::optional
if (std::type_index(typeid(typename DataClass::value_type)) == source->storedDataType())
{
setData(source->get<typename DataClass::value_type>());
return true;
}
// Variant
if (source->providesDataType(typeid(typename DataClass::value_type)))
return source->assignDataTo(this);
}
else if (typeid(std::optional<DataClass>) == source->storedDataType())
{
// Data types can be set from a std::optional containing the same type
auto otherData = source->get<std::optional<DataClass>>();
if (otherData.has_value())
{
setData(*otherData);
return true;
}
}
else if constexpr (is_instance_of_v<DataClass, VariantParameterData>)
{
// Variant to variant
if (storedDataType_ == source->storedDataType())
{
setData(source->get<DataClass>());
// If the variant now contains a pointer type we need to check for nullptr
if (data_.hasNullPointer())
return false;
return true;
}
// Variants can be set from any matching type
if (data_.emplaceFrom(source))
{
// If the variant now contains a pointer type we need to check for nullptr
if (data_.hasNullPointer())
return false;
updateAfterSet();
return true;
}
}
else if (storedDataType_ == source->storedDataType())
{
// General case - if the stored data types are the same then we can just do a straight assignment
setData(source->get<DataClass>());
// If we are a pointer type, getting a nullptr is disallowed
if constexpr (std::is_pointer<DataClass>())
{
return data_ != nullptr;
}
return true;
}
else
{
// Try setting from the source parameter so we have it's full DataClass information - this is necessary for types
// like std::variant where we need to know the stored alternative.
if (source->assignDataTo(this))
{
updateAfterSet();
return true;
}
}
return false;
}
// Assign the value of this parameter to the target one
bool assignDataTo(ParameterBase *destination) override
{
if constexpr (is_instance_of_v<DataClass, VariantParameterData>)
{
// Check that we actually contain a valid value
if (data_.isEmpty())
return false;
// The possibility for a match between the parameters has already been checkwd by the Edge, so here we must just
// try to emplace the variant's data into the destination parameter.
if (data_.emplaceInto(destination))
{
destination->updateAfterSet();
return true;
}
}
else if (storedDataType_ == destination->storedDataType())
{
// General case - if the stored data types are the same then we can just do a straight assignment
destination->set<DataClass>(data_);
return true;
}
return false;
}
// Invalidate the vector data (instances of std::vector only)
void invalidateVector() override
{
if constexpr (is_instance_of_v<DataClass, std::vector>)
{
// Mark all incoming edges to us as needing a re-pull
markIncomingEdgesForPull();
// Empty the vector
data_.clear();
}
else
throw(std::runtime_error(std::format("Parameter<{}>::invalidateVector() - Tried to invalidate a non-vector type.",
storedDataType_.name())));
}
// Set the parameter value
void setData(const DataClass &value)
{
dataSetter_(value);
updateAfterSet();
}
// Return the parameter value
virtual DataClass getData() { return dataGetter_(); }
// Create a parameter link (input - data proxy - output) for this parameter type
ParameterLink createParameterLink(std::string_view newName, std::string_view newDescription) const override
{
// Create a parameter holder object with the same type as ours and add it to the proxies_ storage
auto proxy = std::make_shared<ParameterProxy<DataClass>>();
// Create an input and an output Parameter linked to the proxy data
auto inputParameter = std::make_shared<Parameter<DataClass>>(nullptr, newName, newDescription, proxy);
inputParameter->setFlags(ParameterBase::ParameterFlags::Input);
// Create a companion input on our Outputs node, again linked to the proxy data
auto outputParameter = std::make_shared<Parameter<DataClass>>(nullptr, newName, newDescription, proxy);
outputParameter->setFlags(ParameterBase::ParameterFlags::Output);
return {inputParameter, outputParameter};
}
};
// Primary type for a Parameter to a specific DataClass
template <typename DataClass> class SerialisableParameter : public Parameter<DataClass>
{
public:
SerialisableParameter(Node *parent, std::string_view name, std::string_view description, DataClass &value,
typename Context<DataClass>::type context = {})
: Parameter<DataClass>(parent, name, description, value)
{
}
// Helper templates for handling serialisation
template <typename V> struct is_ptr_vector : std::false_type
{
};
template <serialisablePointer E> struct is_ptr_vector<std::vector<E>> : std::true_type
{
};
/*
* Serialisation
*/
public:
// Express as a serialised value
void serialise(std::string tag, SerialisedValue &target) const override
{
SerialisedValue result = {};
// Serialise non-pointer values
if constexpr (HasEnumOptions<DataClass>)
result["data"] = getEnumOptions(Parameter<DataClass>::data_).serialise(Parameter<DataClass>::data_);
else if constexpr (std::is_convertible<DataClass, Number>::value)
result["data"] = Parameter<DataClass>::data_;
else if constexpr (std::is_convertible<DataClass, std::string>::value)
result["data"] = Parameter<DataClass>::data_;
else if constexpr (std::is_convertible<DataClass, std::optional<Number>>::value)
{
if (Parameter<DataClass>::data_)
result["data"] = *Parameter<DataClass>::data_;
}
else if constexpr (serialisablePointer<DataClass>)
Parameter<DataClass>::data_->serialise("data", result);
else
result["data"] = Parameter<DataClass>::data_;
target[tag] = result;
};
// Read from a serialised value
void deserialise(const SerialisedValue &node) override
{
if constexpr (std::is_pointer<DataClass>::value)
{
Parameter<DataClass>::data_ = nullptr;
}
else if constexpr (is_ptr_vector<DataClass>::value)
Parameter<DataClass>::data_.clear();
else if constexpr (HasEnumOptions<DataClass>)
{
DataClass proxy; // Fake T value to get the correct overload
Parameter<DataClass>::data_ = getEnumOptions(proxy).enumeration(toml::find<std::string>(node, "data"));
}
else if constexpr (std::is_same_v<DataClass, std::optional<double>>)
{
if (node.contains("data"))
Parameter<DataClass>::data_ = toml::find<double>(node, "data");
else
Parameter<DataClass>::data_ = {};
}
else if constexpr (std::is_same_v<DataClass, std::optional<Number>>)
{
if (node.contains("data"))
Parameter<DataClass>::data_ = toml::find<Number>(node, "data");
else
Parameter<DataClass>::data_ = {};
}
else if constexpr (std::is_same_v<DataClass, std::optional<Data1D>>)
{
if (node.contains("data"))
Parameter<DataClass>::data_ = toml::find<Data1D>(node, "data");
else
Parameter<DataClass>::data_ = {};
}
else if constexpr (serialisablePointer<DataClass>)
{
CoreData coreData; // Temporary patch until we fix up the deserialisation
Parameter<DataClass>::data_->deserialise(node.at("data"), coreData);
}
else
{
Parameter<DataClass>::data_ = toml::find<DataClass>(node, "data");
}
}
};