/// Parent class for a member object of a bias, cv or cvc etc. containing dependencies
/// (features) and handling dependency resolution
// Some features like colvar::f_linear have no dependencies, require() doesn't enable anything but fails if unavailable
// Policy: those features are unavailable at all times
// Other features are under user control
// They are unavailable unless requested by the user, then they may be enabled subject to
// satisfied dependencies
// It seems important to have available default to false (for safety) and enabled to false (for efficiency)
classcvm::deps{
public:
deps(){}
virtual~deps();
// Subclasses should initialize the following members:
std::stringdescription;// reference to object name (cv, cvc etc.)
/// This contains the current state of each feature for each object
structfeature_state{
feature_state(boola,boole)
:available(a),enabled(e){}
/// Available means: supported, subject to dependencies as listed,
/// MAY BE ENABLED AS A RESULT OF DEPENDENCY SOLVING
/// Remains false for passive flags that are set based on other object properties,
/// eg. f_cv_linear
/// Is set to true upon user request for features that are implemented by the user
/// or under his/her direct control, e.g. f_cv_scripted or f_cv_extended_Lagrangian
boolavailable;
/// Currently enabled - this flag is subject to change dynamically
/// TODO consider implications for dependency solving: anyone who disables
/// it should trigger a refresh of parent objects
boolenabled;// see if this should be private depending on implementation
// bool enabledOnce; // this should trigger an update when object is evaluated
};
/// List of the state of all features
std::vector<feature_state*>feature_states;
/// Describes a feature and its dependecies
/// used in a static array within each subclass
classfeature{
public:
feature(){}
~feature(){}
std::stringdescription;// Set by derived object initializer
// features that this feature requires in the same object
// NOTE: we have no safety mechanism against circular dependencies, however, they would have to be internal to an object (ie. requires_self or requires_alt)
std::vector<int>requires_self;
// Features that are incompatible, ie. required disabled
// if enabled, they will cause a dependency failure (they will not be disabled)
// To enforce these dependencies regardless of the order in which they
// are enabled, they are always set in a symmetric way, so whichever is enabled
// second will cause the dependency to fail
std::vector<int>requires_exclude;
// sets of features that are required in an alternate way
// when parent feature is enabled, if none are enabled, the first one listed that is available will be enabled
std::vector<std::vector<int>>requires_alt;
// features that this feature requires in children
std::vector<int>requires_children;
};
// Accessor to array of all features with deps, static in most derived classes
// Subclasses with dynamic dependency trees may override this
// with a non-static array
// Intermediate classes (colvarbias and colvarcomp, which are also base classes)
// implement this as virtual to allow overriding
virtualstd::vector<feature*>&features()=0;
voidadd_child(deps*child);
voidremove_child(deps*child);
/// Used before deleting an object, if not handled by that object's destructor
/// (useful for cvcs because their children are member objects)
voidremove_all_children();
private:
// pointers to objects this object depends on
// list should be maintained by any code that modifies the object
// this could be secured by making lists of colvars / cvcs / atom groups private and modified through accessor functions
std::vector<deps*>children;
// pointers to objects that depend on this object
// the size of this array is in effect a reference counter
std::vector<deps*>parents;
public:
// disabling a feature f:
// if parents depend on f, tell them to refresh / check that they are ok?
// if children provide features to satisfy f ONLY, disable that
// When the state of this object has changed, recursively tell parents
// to enforce their dependencies
// void refresh_parents() {
//
// }
// std::vector<deps *> parents; // Needed to trigger a refresh if capabilities of this object change
// End of members to be initialized by subclasses
// Checks whether given feature is enabled
// Defaults to querying f_*_active
inlineboolis_enabled(intf=f_cv_active)const{
returnfeature_states[f]->enabled;
}
// Checks whether given feature is available
// Defaults to querying f_*_active
inlineboolis_available(intf=f_cv_active)const{
returnfeature_states[f]->available;
}
voidprovide(intfeature_id);// set the feature's flag to available in local object
intenable(intf,booldry_run=false,booltoplevel=true);// enable a feature and recursively solve its dependencies
// dry_run is set to true to recursively test if a feature is available, without enabling it
// int disable(int f);
/// This function is called whenever feature states are changed outside
/// of the object's control, that is, by parents
/// Eventually it may also be used when properties of children change
virtualintrefresh_deps(){returnCOLVARS_OK;}
// NOTE that all feature enums should start with f_*_active