You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.8 KiB

  1. //==============================================================================
  2. //
  3. // Copyright (c) 2015-
  4. // Authors:
  5. // * Joachim Klein <klein@tcs.inf.tu-dresden.de>
  6. // * David Mueller <david.mueller@tcs.inf.tu-dresden.de>
  7. //
  8. //------------------------------------------------------------------------------
  9. //
  10. // This file is part of the cpphoafparser library,
  11. // http://automata.tools/hoa/cpphoafparser/
  12. //
  13. // The cpphoafparser library is free software; you can redistribute it and/or
  14. // modify it under the terms of the GNU Lesser General Public
  15. // License as published by the Free Software Foundation; either
  16. // version 2.1 of the License, or (at your option) any later version.
  17. //
  18. // The cpphoafparser library is distributed in the hope that it will be useful,
  19. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. // Lesser General Public License for more details.
  22. //
  23. // You should have received a copy of the GNU Lesser General Public
  24. // License along with this library; if not, write to the Free Software
  25. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. //
  27. //==============================================================================
  28. #ifndef CPPHOAFPARSER_ATOMLABEL_HH
  29. #define CPPHOAFPARSER_ATOMLABEL_HH
  30. #include <memory>
  31. #include <string>
  32. #include <iostream>
  33. namespace cpphoafparser {
  34. /**
  35. * Atom of a label expression (either an atomic proposition index or an alias reference)
  36. */
  37. class AtomLabel {
  38. public:
  39. /** A shared_ptr wrapping an AtomLabel */
  40. typedef std::shared_ptr<AtomLabel> ptr;
  41. /** Static constructor for an atomic proposition atom */
  42. static AtomLabel::ptr createAPIndex(unsigned int apIndex) {
  43. return AtomLabel::ptr(new AtomLabel(apIndex));
  44. }
  45. /** Static constructor for an alias reference (name has to be without leading @) */
  46. static AtomLabel::ptr createAlias(const std::string name) {
  47. return AtomLabel::ptr(new AtomLabel(name));
  48. }
  49. /** Copy constructor. */
  50. AtomLabel(const AtomLabel& other) : apIndex(0), aliasName(nullptr) {
  51. if (other.isAlias()) {
  52. aliasName = std::shared_ptr<std::string>(new std::string(*other.aliasName));
  53. } else {
  54. apIndex = other.apIndex;
  55. }
  56. }
  57. /** Returns true if this atom is an alias reference */
  58. bool isAlias() const {return (bool)aliasName;}
  59. /**
  60. * Returns the alias name (for an alias reference atom).
  61. * May only be called if `isAlias() == true`.
  62. */
  63. const std::string& getAliasName() const {
  64. if (!isAlias()) {throw std::logic_error("Illegal access");}
  65. return *aliasName;
  66. }
  67. /**
  68. * Returns the atomic proposition index (for an AP atom).
  69. * May only be called if `isAlias() == false`.
  70. */
  71. unsigned int getAPIndex() const {
  72. if (isAlias()) {throw std::logic_error("Illegal access");}
  73. return apIndex;
  74. }
  75. /** Output operator, renders in HOA syntax */
  76. friend std::ostream& operator<<(std::ostream& out, const AtomLabel& atom) {
  77. if (atom.isAlias()) {
  78. out << "@" << *atom.aliasName;
  79. } else {
  80. out << atom.apIndex;
  81. }
  82. return out;
  83. }
  84. /** Equality operator. */
  85. bool operator==(const AtomLabel& other) const {
  86. if (isAlias()) {
  87. return other.isAlias() && getAliasName() == other.getAliasName();
  88. } else {
  89. return !other.isAlias() && getAPIndex() == other.getAPIndex();
  90. }
  91. }
  92. private:
  93. /** The AP index (if applicable) */
  94. unsigned int apIndex;
  95. /** The aliasName (empty pointer if AP atom) */
  96. std::shared_ptr<std::string> aliasName;
  97. /** Private constructor (AP atom) */
  98. AtomLabel(unsigned int apIndex) : apIndex(apIndex), aliasName(nullptr) {}
  99. /** Private constructor (alias reference atom) */
  100. AtomLabel(const std::string& name) : apIndex(0), aliasName(new std::string(name)) {}
  101. };
  102. }
  103. #endif