Plot Script
atom.hpp
Go to the documentation of this file.
1 
4 #ifndef ATOM_HPP
5 #define ATOM_HPP
6 
7 #include "token.hpp"
8 
14 class Atom {
15 public:
16 
18  Atom();
19 
21  Atom(double value);
22 
24  Atom(const std::string & value);
25 
27  Atom(const Token & token);
28 
30  Atom(const Atom & x);
31 
33  Atom & operator=(const Atom & x);
34 
36  ~Atom();
37 
39  bool isNone() const noexcept;
40 
42  bool isNumber() const noexcept;
43 
45  bool isSymbol() const noexcept;
46 
48  double asNumber() const noexcept;
49 
51  std::string asSymbol() const noexcept;
52 
54  bool operator==(const Atom & right) const noexcept;
55 
56 private:
57 
58  // internal enum of known types
59  enum Type {NoneKind, NumberKind, SymbolKind};
60 
61  // track the type
62  Type m_type;
63 
64  // values for the known types. Note the use of a union requires care
65  // when setting non POD values (see setSymbol)
66  union {
67  double numberValue;
68  std::string stringValue;
69  };
70 
71  // helper to set type and value of Number
72  void setNumber(double value);
73 
74  // helper to set type and value of Symbol
75  void setSymbol(const std::string & value);
76 };
77 
79 bool operator!=(const Atom &left, const Atom & right) noexcept;
80 
82 std::ostream & operator<<(std::ostream & out, const Atom & a);
83 
84 #endif
~Atom()
Atom destructor.
Definition: atom.cpp:64
double asNumber() const noexcept
value of Atom as a number, return 0 if not a Number
Definition: atom.cpp:104
bool isSymbol() const noexcept
predicate to determine if an Atom is of type Symbol
Definition: atom.cpp:80
bool operator==(const Atom &right) const noexcept
equality comparison based on type and value
Definition: atom.cpp:121
Atom & operator=(const Atom &x)
Assign an Atom.
Definition: atom.cpp:48
Value class representing a token.
Definition: token.hpp:15
Atom()
Construct a default Atom of type None.
Definition: atom.cpp:8
double numberValue
Definition: atom.hpp:67
std::ostream & operator<<(std::ostream &out, const Atom &a)
output stream rendering
Definition: atom.cpp:159
bool isNumber() const noexcept
predicate to determine if an Atom is of type Number
Definition: atom.cpp:76
A variant type that may be a Number or Symbol or the default type None.
Definition: atom.hpp:14
std::string stringValue
Definition: atom.hpp:68
bool operator!=(const Atom &left, const Atom &right) noexcept
inequality comparison for Atom
Definition: atom.cpp:153
bool isNone() const noexcept
predicate to determine if an Atom is of type None
Definition: atom.cpp:72
std::string asSymbol() const noexcept
value of Atom as a number, returns empty-string if not a Symbol
Definition: atom.cpp:110