doxygen
cppvalue.h
1 /******************************************************************************
2  *
3  * Copyright (C) 1997-2021 by Dimitri van Heesch.
4  *
5  * Permission to use, copy, modify, and distribute this software and its
6  * documentation under the terms of the GNU General Public License is hereby
7  * granted. No representations are made about the suitability of this software
8  * for any purpose. It is provided "as is" without express or implied warranty.
9  * See the GNU General Public License for more details.
10  *
11  * Documents produced by Doxygen are derivative works derived from the
12  * input used in their production; they are not affected by this license.
13  *
14  */
15 
16 #ifndef CPPVALUE_H
17 #define CPPVALUE_H
18 
19 #include <cstdio>
20 #include <string>
21 
23 class CPPValue
24 {
25  public:
26  enum Type { Int, Float };
27 
28  explicit CPPValue(char c) : type(Int) { v.l = c; }
29  explicit CPPValue(long val=0) : type(Int) { v.l = val; }
30  explicit CPPValue(double val) : type(Float) { v.d = val; }
31 
32  operator double () const { return type==Int ? static_cast<double>(v.l) : v.d; }
33  operator long () const { return type==Int ? v.l : static_cast<long>(v.d); }
34 
35  bool isInt() const { return type == Int; }
36 
37  void print() const
38  {
39  if (type==Int)
40  printf("(%ld)\n",v.l);
41  else
42  printf("(%f)\n",v.d);
43  }
44  static CPPValue parseOctal(const std::string& token);
45  static CPPValue parseDecimal(const std::string& token);
46  static CPPValue parseHexadecimal(const std::string& token);
47  static CPPValue parseBinary(const std::string& token);
48  static CPPValue parseCharacter(const std::string& token);
49  static CPPValue parseFloat(const std::string& token);
50 
51  private:
52  Type type;
53  union {
54  double d;
55  long l;
56  } v;
57 };
58 
59 
60 #endif
A class representing a C-preprocessor value.
Definition: cppvalue.h:23