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 
22 /** A class representing a C-preprocessor value. */
23 class CPPValue
24 {
25  public:
26  enum Type { Int, Float };
27 
28  CPPValue(long val=0) : type(Int) { v.l = val; }
29  CPPValue(double val) : type(Float) { v.d = val; }
30 
31  operator double () const { return type==Int ? (double)v.l : v.d; }
32  operator long () const { return type==Int ? v.l : (long)v.d; }
33 
34  bool isInt() const { return type == Int; }
35 
36  void print() const
37  {
38  if (type==Int)
39  printf("(%ld)\n",v.l);
40  else
41  printf("(%f)\n",v.d);
42  }
43 
44  private:
46  union {
47  double d;
48  long l;
49  } v;
50 };
51 
52 extern CPPValue parseOctal(const std::string& token);
53 extern CPPValue parseDecimal(const std::string& token);
54 extern CPPValue parseHexadecimal(const std::string& token);
55 extern CPPValue parseBinary(const std::string& token);
56 extern CPPValue parseCharacter(const std::string& token);
57 extern CPPValue parseFloat(const std::string& token);
58 
59 #endif
CPPValue::CPPValue
CPPValue(long val=0)
Definition: cppvalue.h:41
CPPValue::v
union CPPValue::@1 v
CPPValue::isInt
bool isInt() const
Definition: cppvalue.h:47
parseOctal
CPPValue parseOctal(const std::string &token)
Definition: cppvalue.cpp:21
CPPValue::type
Type type
Definition: cppvalue.h:58
CPPValue::d
double d
Definition: cppvalue.h:60
CPPValue::Int
@ Int
Definition: cppvalue.h:52
CPPValue
A class representing a C-preprocessor value.
Definition: cppvalue.h:23
parseFloat
CPPValue parseFloat(const std::string &token)
Definition: cppvalue.cpp:99
parseBinary
CPPValue parseBinary(const std::string &token)
Definition: cppvalue.cpp:54
parseDecimal
CPPValue parseDecimal(const std::string &token)
Definition: cppvalue.cpp:31
parseHexadecimal
CPPValue parseHexadecimal(const std::string &token)
Definition: cppvalue.cpp:41
CPPValue::Type
Type
Definition: cppvalue.h:39
CPPValue::Float
@ Float
Definition: cppvalue.h:52
CPPValue::print
void print() const
Definition: cppvalue.h:49
CPPValue::l
long l
Definition: cppvalue.h:61
parseCharacter
CPPValue parseCharacter(const std::string &token)
Definition: cppvalue.cpp:64