variable.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) 2013-2017, Nucleic Development Team.
  3. |
  4. | Distributed under the terms of the Modified BSD License.
  5. |
  6. | The full license is in the file LICENSE, distributed with this software.
  7. |----------------------------------------------------------------------------*/
  8. #pragma once
  9. #include <memory>
  10. #include <string>
  11. #include "shareddata.h"
  12. namespace kiwi
  13. {
  14. class Variable
  15. {
  16. public:
  17. class Context
  18. {
  19. public:
  20. Context() = default;
  21. virtual ~Context() {} // LCOV_EXCL_LINE
  22. };
  23. Variable(Context *context = 0) : m_data(new VariableData("", context)) {}
  24. Variable(std::string name, Context *context = 0) : m_data(new VariableData(std::move(name), context)) {}
  25. Variable(const char *name, Context *context = 0) : m_data(new VariableData(name, context)) {}
  26. Variable(const Variable&) = default;
  27. Variable(Variable&&) noexcept = default;
  28. ~Variable() = default;
  29. const std::string &name() const
  30. {
  31. return m_data->m_name;
  32. }
  33. void setName(const char *name)
  34. {
  35. m_data->m_name = name;
  36. }
  37. void setName(const std::string &name)
  38. {
  39. m_data->m_name = name;
  40. }
  41. Context *context() const
  42. {
  43. return m_data->m_context.get();
  44. }
  45. void setContext(Context *context)
  46. {
  47. m_data->m_context.reset(context);
  48. }
  49. double value() const
  50. {
  51. return m_data->m_value;
  52. }
  53. void setValue(double value)
  54. {
  55. m_data->m_value = value;
  56. }
  57. // operator== is used for symbolics
  58. bool equals(const Variable &other)
  59. {
  60. return m_data == other.m_data;
  61. }
  62. Variable& operator=(const Variable&) = default;
  63. Variable& operator=(Variable&&) noexcept = default;
  64. private:
  65. class VariableData : public SharedData
  66. {
  67. public:
  68. VariableData(std::string name, Context *context) : SharedData(),
  69. m_name(std::move(name)),
  70. m_context(context),
  71. m_value(0.0) {}
  72. VariableData(const char *name, Context *context) : SharedData(),
  73. m_name(name),
  74. m_context(context),
  75. m_value(0.0) {}
  76. ~VariableData() = default;
  77. std::string m_name;
  78. std::unique_ptr<Context> m_context;
  79. double m_value;
  80. private:
  81. VariableData(const VariableData &other);
  82. VariableData &operator=(const VariableData &other);
  83. };
  84. SharedDataPtr<VariableData> m_data;
  85. friend bool operator<(const Variable &lhs, const Variable &rhs)
  86. {
  87. return lhs.m_data < rhs.m_data;
  88. }
  89. };
  90. } // namespace kiwi