/usr/include/gromacs/options/basicoptions.h is in libgromacs-dev 2018.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 | /*
* This file is part of the GROMACS molecular simulation package.
*
* Copyright (c) 2010,2011,2012,2013,2014,2015,2016,2017, by the GROMACS development team, led by
* Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
* and including many others, as listed in the AUTHORS file in the
* top-level source directory and at http://www.gromacs.org.
*
* GROMACS is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* GROMACS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with GROMACS; if not, see
* http://www.gnu.org/licenses, or write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* If you want to redistribute modifications to GROMACS, please
* consider that scientific software is very special. Version
* control is crucial - bugs must be traceable. We will be happy to
* consider code for inclusion in the official distribution, but
* derived work must not be called official GROMACS. Details are found
* in the README & COPYING files - if they are missing, get the
* official version at http://www.gromacs.org.
*
* To help us fund GROMACS development, we humbly ask that you cite
* the research papers on the package. Check out http://www.gromacs.org.
*/
/*! \file
* \brief
* Declares option objects for basic option types.
*
* Together with options.h, this header forms the part of the public API
* that most classes will use to provide options.
*
* \author Teemu Murtola <teemu.murtola@gmail.com>
* \inpublicapi
* \ingroup module_options
*/
#ifndef GMX_OPTIONS_BASICOPTIONS_H
#define GMX_OPTIONS_BASICOPTIONS_H
#include <string>
#include <vector>
#include "gromacs/options/abstractoption.h"
#include "gromacs/options/ivaluestore.h"
#include "gromacs/utility/arrayref.h"
#include "gromacs/utility/basedefinitions.h"
#include "gromacs/utility/gmxassert.h"
namespace gmx
{
class BooleanOptionInfo;
class BooleanOptionStorage;
class IntegerOptionInfo;
class IntegerOptionStorage;
class Int64OptionInfo;
class Int64OptionStorage;
class DoubleOptionInfo;
class DoubleOptionStorage;
class FloatOptionInfo;
class FloatOptionStorage;
class StringOptionInfo;
class StringOptionStorage;
class EnumOptionInfo;
class EnumOptionStorage;
//! \addtogroup module_options
//! \{
/*! \brief
* Specifies an option that provides boolean values.
*
* Example:
* \code
bool bPBC;
using gmx::BooleanOption;
options.addOption(BooleanOption("pbc").store(&bPBC));
* \endcode
*
* Public methods in this class do not throw.
*
* \inpublicapi
*/
class BooleanOption : public OptionTemplate<bool, BooleanOption>
{
public:
//! OptionInfo subclass corresponding to this option type.
typedef BooleanOptionInfo InfoType;
//! Initializes an option with the given name.
explicit BooleanOption(const char *name) : MyBase(name) {}
private:
//! Creates a BooleanOptionStorage object.
virtual AbstractOptionStorage *createStorage(
const OptionManagerContainer &managers) const;
};
/*! \brief
* Specifies an option that provides integer values.
*
* Examples:
* \code
using gmx::IntegerOption;
// Simple option
int rcut = 0;
options.addOption(IntegerOption("rcut").store(&rcut));
// Vector-valued option
int box[3] = {1, 1, 1}; // Default value
options.addOption(IntegerOption("box").store(box).vector());
* \endcode
*
* Public methods in this class do not throw.
*
* \inpublicapi
*/
class IntegerOption : public OptionTemplate<int, IntegerOption>
{
public:
//! OptionInfo subclass corresponding to this option type.
typedef IntegerOptionInfo InfoType;
//! Initializes an option with the given name.
explicit IntegerOption(const char *name) : MyBase(name) {}
/*! \brief
* Sets the option to return a vector value.
*
* A vector value returns a fixed number of values, the default being
* three (can be changed with valueCount()). However, it also accepts
* a single value, in which case the value is used to fill the whole
* vector.
*/
MyClass &vector() { setVector(); return me(); }
private:
//! Creates an IntegerOptionStorage object.
virtual AbstractOptionStorage *createStorage(
const OptionManagerContainer &managers) const;
/*! \brief
* Needed to initialize IntegerOptionStorage from this class without
* otherwise unnecessary accessors.
*/
friend class IntegerOptionStorage;
};
/*! \brief
* Specifies an option that provides 64-bit integer values.
*
* Public methods in this class do not throw.
*
* \see IntegerOption
*
* \inpublicapi
*/
class Int64Option : public OptionTemplate<gmx_int64_t, Int64Option>
{
public:
//! OptionInfo subclass corresponding to this option type.
typedef Int64OptionInfo InfoType;
//! Initializes an option with the given name.
explicit Int64Option(const char *name) : MyBase(name) {}
private:
//! Creates an Int64OptionStorage object.
virtual AbstractOptionStorage *createStorage(
const OptionManagerContainer &managers) const;
/*! \brief
* Needed to initialize Int64OptionStorage from this class without
* otherwise unnecessary accessors.
*/
friend class Int64OptionStorage;
};
/*! \brief
* Specifies an option that provides floating-point (double) values.
*
* Public methods in this class do not throw.
*
* \inpublicapi
*/
class DoubleOption : public OptionTemplate<double, DoubleOption>
{
public:
//! OptionInfo subclass corresponding to this option type.
typedef DoubleOptionInfo InfoType;
//! Initializes an option with the given name.
explicit DoubleOption(const char *name) : MyBase(name), bTime_(false)
{
}
//! \copydoc IntegerOption::vector()
MyClass &vector() { setVector(); return me(); }
/*! \brief
* Marks this option as providing a time value whose unit can be changed.
*
* By itself, this option does nothing. It marks the option as a time
* value such that TimeUnitManager::scaleTimeOptions() can process it.
* In typical cases, \Gromacs scales the time options just before
* Options::finish() has been called, so the option value is only
* available after all option values have been processed.
* All values in the program are in ps (including any default value);
* user-provided values are scaled according to the time unit set in
* TimeUnitManager.
*/
MyClass &timeValue() { bTime_ = true; return me(); }
private:
//! Creates a DoubleOptionStorage object.
virtual AbstractOptionStorage *createStorage(
const OptionManagerContainer &managers) const;
bool bTime_;
/*! \brief
* Needed to initialize DoubleOptionStorage from this class without
* otherwise unnecessary accessors.
*/
friend class DoubleOptionStorage;
};
/*! \brief
* Specifies an option that provides floating-point (float) values.
*
* Public methods in this class do not throw.
*
* \see DoubleOption
*
* \inpublicapi
*/
class FloatOption : public OptionTemplate<float, FloatOption>
{
public:
//! OptionInfo subclass corresponding to this option type.
typedef FloatOptionInfo InfoType;
//! Initializes an option with the given name.
explicit FloatOption(const char *name) : MyBase(name), bTime_(false)
{
}
//! \copydoc IntegerOption::vector()
MyClass &vector() { setVector(); return me(); }
//! \copydoc DoubleOption::timeValue()
MyClass &timeValue() { bTime_ = true; return me(); }
private:
//! Creates a FloatOptionStorage object.
virtual AbstractOptionStorage *createStorage(
const OptionManagerContainer &managers) const;
bool bTime_;
/*! \brief
* Needed to initialize FloatOptionStorage from this class without
* otherwise unnecessary accessors.
*/
friend class FloatOptionStorage;
};
/*! \brief
* Specifies an option that provides string values.
*
* Examples:
* \code
using gmx::StringOption;
// Simple option
std::string str;
options.addOption(StringOption("str").store(&str));
// Option that only accepts predefined values
const char * const allowed[] = { "atom", "residue", "molecule" };
std::string str;
options.addOption(StringOption("type").enumValue(allowed).store(&str));
* \endcode
*
* Public methods in this class do not throw.
*
* \inpublicapi
*/
class StringOption : public OptionTemplate<std::string, StringOption>
{
public:
//! OptionInfo subclass corresponding to this option type.
typedef StringOptionInfo InfoType;
//! Initializes an option with the given name.
explicit StringOption(const char *name)
: MyBase(name), enumValues_(nullptr), enumValuesCount_(0),
defaultEnumIndex_(-1)
{
}
/*! \brief
* Sets the option to only accept one of a fixed set of strings.
*
* \param[in] values Array of strings to accept.
*
* Also accepts prefixes of the strings; if a prefix matches more than
* one of the possible strings, the shortest one is used (in a tie, the
* first one is).
*
* The strings are copied once the option is created.
*/
template <size_t count>
MyClass &enumValue(const char *const (&values)[count])
{
GMX_ASSERT(enumValues_ == NULL,
"Multiple sets of enumerated values specified");
enumValues_ = values;
enumValuesCount_ = count;
return me();
}
/*! \brief
* Sets the option to only accept one of a fixed set of strings.
*
* \param[in] values Array of strings to accept, with a NULL pointer
* following the last string.
*
* Works otherwise as the array version, but accepts a pointer to
* an array of undetermined length. The end of the array is indicated
* by a NULL pointer in the array.
*
* \see enumValue()
*/
MyClass &enumValueFromNullTerminatedArray(const char *const *values)
{
GMX_ASSERT(enumValues_ == NULL,
"Multiple sets of enumerated values specified");
enumValues_ = values;
enumValuesCount_ = -1;
return me();
}
/*! \brief
* Sets the default value using an index into the enumeration table.
*
* Cannot be specified without enumValue().
*/
MyClass &defaultEnumIndex(int index)
{
GMX_ASSERT(index >= 0, "Invalid enumeration index");
defaultEnumIndex_ = index;
return me();
}
private:
//! Creates a StringOptionStorage object.
virtual AbstractOptionStorage *createStorage(
const OptionManagerContainer &managers) const;
const char *const *enumValues_;
int enumValuesCount_;
int defaultEnumIndex_;
/*! \brief
* Needed to initialize StringOptionStorage from this class without
* otherwise unnecessary accessors.
*/
friend class StringOptionStorage;
};
//! \}
namespace internal
{
/*! \internal
* \brief
* Type-specific implementation for IOptionValueStore for an enum option.
*
* This class is instantiated for each enum type for which EnumOption is used,
* and takes care of managing `int`-to-`enum` conversions. Having this part in
* the header allows the actual storage implementation to not be in the header,
* which would require exposing all the internals through this one header...
*
* \ingroup module_options
*/
template <typename EnumType>
class EnumIndexStore : public IOptionValueStore<int>
{
public:
//! Initializes the storage for the given actual enum variables.
EnumIndexStore(EnumType *store, std::vector<EnumType> *storeVector)
: store_(store), storeVector_(storeVector)
{
if (storeVector_ != nullptr)
{
for (EnumType value : *storeVector_)
{
intStore_.push_back(static_cast<int>(value));
}
}
else if (store_ != nullptr)
{
// TODO: Copy more than one value if that would make sense.
intStore_.push_back(static_cast<int>(store_[0]));
}
}
virtual int valueCount() { return static_cast<int>(intStore_.size()); }
virtual ArrayRef<int> values() { return intStore_; }
virtual void clear()
{
intStore_.clear();
if (storeVector_ != nullptr)
{
storeVector_->clear();
}
}
virtual void reserve(size_t count)
{
intStore_.reserve(intStore_.size() + count);
if (storeVector_ != nullptr)
{
storeVector_->reserve(storeVector_->size() + count);
}
}
virtual void append(const int &value)
{
const size_t count = intStore_.size();
intStore_.push_back(value);
if (store_ != nullptr)
{
store_[count] = static_cast<EnumType>(value);
}
if (storeVector_ != nullptr)
{
storeVector_->push_back(static_cast<EnumType>(value));
}
}
private:
//! Stores the integer values for values().
std::vector<int> intStore_;
EnumType *store_;
std::vector<EnumType> *storeVector_;
};
//! \cond internal
/*! \internal
* \brief
* Helper to create EnumOptionStorage instances.
*
* This function works as a proxy between EnumOption::createStorage() and the
* EnumOptionStorage constructor, such that the latter does not need to be
* exposed in the header.
*
* \ingroup module_options
*/
AbstractOptionStorage *
createEnumOptionStorage(const AbstractOption &option,
const char *const *enumValues, int count,
int defaultValue, int defaultValueIfSet,
IOptionValueStore<int> *store);
//! \endcond
} // namespace internal
//! \addtogroup module_options
//! \{
/*! \brief
* Specifies an option that accepts enumerated string values and writes the
* selected index into an `enum` variable.
*
* \tparam EnumType Type of the variable that receives the values
* (can also be `int`).
*
* Examples:
* \code
enum MyEnum { eAtom, eRes, eMol };
using gmx::EnumOption;
const char * const allowed[] = { "atom", "residue", "molecule" };
MyEnum value = eAtom; // default value
options.addOption(EnumOption<MyEnum>("type").enumValue(allowed).store(&value));
* \endcode
*
* storeCount() is not currently implemented for this option type, and
* providing multiple default values through an array passed to store() does
* not work consistently in all cases.
* In the current implementation, the values of the enum type should correspond
* to indices in the array passed to enumValue(), i.e., be consencutive
* starting from zero. Only values corresponding to valid indices are accepted
* as parameters to, e.g., defaultValue(). However, other values can be used
* as the initial value of the variable (`value` in the above example), and
* those will be preserved if the option is not set.
*
* Public methods in this class do not throw.
*
* \inpublicapi
*/
template <typename EnumType>
class EnumOption : public OptionTemplate<EnumType, EnumOption<EnumType> >
{
public:
//! OptionInfo subclass corresponding to this option type.
typedef EnumOptionInfo InfoType;
// This needs to be duplicated from OptionTemplate because this class
// is a template.
//! Short-hand for the base class.
typedef OptionTemplate<EnumType, EnumOption<EnumType> > MyBase;
//! Initializes an option with the given name.
explicit EnumOption(const char *name)
: MyBase(name), enumValues_(nullptr), enumValuesCount_(0)
{
}
/*! \brief
* Sets the option to only accept one of a fixed set of strings.
*
* \param[in] values Array of strings to accept.
*
* Also accepts prefixes of the strings; if a prefix matches more than
* one of the possible strings, the shortest one is used (in a tie, the
* first one is).
*
* The strings are copied once the option is created.
*/
template <size_t count>
EnumOption &enumValue(const char *const (&values)[count])
{
GMX_ASSERT(enumValues_ == NULL,
"Multiple sets of enumerated values specified");
enumValues_ = values;
enumValuesCount_ = count;
return MyBase::me();
}
/*! \brief
* Sets the option to only accept one of a fixed set of strings.
*
* \param[in] values Array of strings to accept, with a NULL pointer
* following the last string.
*
* Works otherwise as the array version, but accepts a pointer to
* an array of undetermined length. The end of the array is indicated
* by a NULL pointer in the array.
*
* \see enumValue()
*/
EnumOption &enumValueFromNullTerminatedArray(const char *const *values)
{
GMX_ASSERT(enumValues_ == NULL,
"Multiple sets of enumerated values specified");
enumValues_ = values;
enumValuesCount_ = -1;
return MyBase::me();
}
private:
//! Helper function to convert default values for storate initialization.
static int convertToInt(const EnumType *defaultValue)
{
return defaultValue != nullptr ? static_cast<int>(*defaultValue) : -1;
}
//! Creates a EnumOptionStorage object.
virtual AbstractOptionStorage *createStorage(
const OptionManagerContainer & /*managers*/) const
{
// TODO: Implement storeCount() if necessary.
return internal::createEnumOptionStorage(
*this, enumValues_, enumValuesCount_,
convertToInt(MyBase::defaultValue()),
convertToInt(MyBase::defaultValueIfSet()),
new internal::EnumIndexStore<EnumType>(
MyBase::store(), MyBase::storeVector()));
}
const char *const *enumValues_;
int enumValuesCount_;
/*! \brief
* Needed to initialize EnumOptionStorage from this class without
* otherwise unnecessary accessors.
*/
friend class EnumOptionStorage;
};
//! Shorthand for an enumerated option that stores into an `int` variable.
typedef EnumOption<int> EnumIntOption;
/*! \brief
* Wrapper class for accessing boolean option information.
*
* \inpublicapi
*/
class BooleanOptionInfo : public OptionInfo
{
public:
//! Creates an option info object for the given option.
explicit BooleanOptionInfo(BooleanOptionStorage *option);
//! Returns the default value for this option.
bool defaultValue() const;
private:
const BooleanOptionStorage &option() const;
};
/*! \brief
* Wrapper class for accessing integer option information.
*
* \inpublicapi
*/
class IntegerOptionInfo : public OptionInfo
{
public:
//! Creates an option info object for the given option.
explicit IntegerOptionInfo(IntegerOptionStorage *option);
};
/*! \brief
* Wrapper class for accessing 64-bit integer option information.
*
* \inpublicapi
*/
class Int64OptionInfo : public OptionInfo
{
public:
//! Creates an option info object for the given option.
explicit Int64OptionInfo(Int64OptionStorage *option);
};
/*! \brief
* Wrapper class for accessing floating-point option information.
*
* \inpublicapi
*/
class DoubleOptionInfo : public OptionInfo
{
public:
//! Creates an option info object for the given option.
explicit DoubleOptionInfo(DoubleOptionStorage *option);
//! Whether the option specifies a time value.
bool isTime() const;
/*! \brief
* Sets a scale factor for user-provided values.
*
* Any user-provided value is scaled by the provided factor.
* Programmatically set default values are not scaled.
* If called multiple times, later calls override the previously set
* value. In other words, the scaling is not cumulative.
*/
void setScaleFactor(double factor);
private:
DoubleOptionStorage &option();
const DoubleOptionStorage &option() const;
};
/*! \brief
* Wrapper class for accessing floating-point option information.
*
* \inpublicapi
*/
class FloatOptionInfo : public OptionInfo
{
public:
//! Creates an option info object for the given option.
explicit FloatOptionInfo(FloatOptionStorage *option);
//! Whether the option specifies a time value.
bool isTime() const;
//! \copydoc DoubleOptionInfo::setScaleFactor()
void setScaleFactor(double factor);
private:
FloatOptionStorage &option();
const FloatOptionStorage &option() const;
};
/*! \brief
* Wrapper class for accessing string option information.
*
* \inpublicapi
*/
class StringOptionInfo : public OptionInfo
{
public:
//! Creates an option info object for the given option.
explicit StringOptionInfo(StringOptionStorage *option);
/*! \brief
* Whether this option accepts an enumerated set of values.
*
* Returns true if StringOption::enumValues() was used when creating
* this option.
*/
bool isEnumerated() const;
/*! \brief
* Returns the set of allowed values for this option.
*
* Returns an empty vector if isEnumerated() returns false.
*/
const std::vector<std::string> &allowedValues() const;
private:
const StringOptionStorage &option() const;
};
/*! \brief
* Wrapper class for accessing enum option information.
*
* \inpublicapi
*/
class EnumOptionInfo : public OptionInfo
{
public:
//! Creates an option info object for the given option.
explicit EnumOptionInfo(EnumOptionStorage *option);
/*! \brief
* Returns the set of allowed values for this option.
*/
const std::vector<std::string> &allowedValues() const;
private:
const EnumOptionStorage &option() const;
};
/*! \typedef RealOption
* \brief
* Typedef for either DoubleOption or FloatOption, depending on precision.
*
* Generally, new would be better using DoubleOption, but this is provided for
* cases where the output value needs to be of type `real` for some reason.
*/
/*! \typedef RealOptionInfo
* \brief
* Typedef for either DoubleOptionInfo or FloatOptionInfo, depending on precision.
*
* Generally, new would be better using DoubleOption, but this is provided for
* cases where the output value needs to be of type `real` for some reason.
*/
#if GMX_DOUBLE
typedef DoubleOption RealOption;
typedef DoubleOptionInfo RealOptionInfo;
#else
typedef FloatOption RealOption;
typedef FloatOptionInfo RealOptionInfo;
#endif
//! \}
} // namespace gmx
#endif
|