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 
45  private:
46  Type type;
47  union {
48  double d;
49  long l;
50  } v;
51 };
52 
53 extern CPPValue parseOctal(const std::string& token);
54 extern CPPValue parseDecimal(const std::string& token);
55 extern CPPValue parseHexadecimal(const std::string& token);
56 extern CPPValue parseBinary(const std::string& token);
57 extern CPPValue parseCharacter(const std::string& token);
58 extern CPPValue parseFloat(const std::string& token);
59 
60 #endif
A class representing a C-preprocessor value.
Definition: cppvalue.h:23