Doxygen
Loading...
Searching...
No Matches
CPPValue Class Reference

A class representing a C-preprocessor value. More...

#include <src/cppvalue.h>

Public Types

enum  Type { Int , Float }
 

Public Member Functions

 CPPValue (char c)
 
 CPPValue (long val=0)
 
 CPPValue (double val)
 
 operator double () const
 
 operator long () const
 
bool isInt () const
 
void print () const
 

Static Public Member Functions

static CPPValue parseOctal (const std::string &token)
 
static CPPValue parseDecimal (const std::string &token)
 
static CPPValue parseHexadecimal (const std::string &token)
 
static CPPValue parseBinary (const std::string &token)
 
static CPPValue parseCharacter (const std::string &token)
 
static CPPValue parseFloat (const std::string &token)
 

Private Attributes

Type type
 
union { 
 
   double   d 
 
   long   l 
 
v 
 

Detailed Description

A class representing a C-preprocessor value.

Definition at line 23 of file cppvalue.h.

Member Enumeration Documentation

◆ Type

Enumerator
Int 
Float 

Definition at line 26 of file cppvalue.h.

26{ Int, Float };

Constructor & Destructor Documentation

◆ CPPValue() [1/3]

CPPValue::CPPValue ( char c)
inlineexplicit

Definition at line 28 of file cppvalue.h.

28: type(Int) { v.l = c; }
union CPPValue::@026105325157306004343225305026272177106174241033 v
Type type
Definition cppvalue.h:52

References Int, type, and v.

Referenced by parseBinary(), parseCharacter(), parseDecimal(), parseFloat(), parseHexadecimal(), and parseOctal().

◆ CPPValue() [2/3]

CPPValue::CPPValue ( long val = 0)
inlineexplicit

Definition at line 29 of file cppvalue.h.

29: type(Int) { v.l = val; }

References Int, type, and v.

◆ CPPValue() [3/3]

CPPValue::CPPValue ( double val)
inlineexplicit

Definition at line 30 of file cppvalue.h.

30: type(Float) { v.d = val; }

References Float, type, and v.

Member Function Documentation

◆ isInt()

bool CPPValue::isInt ( ) const
inline

Definition at line 35 of file cppvalue.h.

35{ return type == Int; }

References Int, and type.

◆ operator double()

CPPValue::operator double ( ) const
inline

Definition at line 32 of file cppvalue.h.

32{ return type==Int ? static_cast<double>(v.l) : v.d; }
double d
Definition cppvalue.h:54

References Int, type, and v.

◆ operator long()

CPPValue::operator long ( ) const
inline

Definition at line 33 of file cppvalue.h.

33{ return type==Int ? v.l : static_cast<long>(v.d); }

References Int, type, and v.

◆ parseBinary()

CPPValue CPPValue::parseBinary ( const std::string & token)
static

Definition at line 54 of file cppvalue.cpp.

55{
56 long val = 0;
57 for (const char *p = token.c_str(); *p != 0; p++)
58 {
59 if (*p >= '0' && *p <= '1') val = val * 2 + *p - '0';
60 }
61 return CPPValue(val);
62}
CPPValue(char c)
Definition cppvalue.h:28

References CPPValue().

◆ parseCharacter()

CPPValue CPPValue::parseCharacter ( const std::string & token)
static

Definition at line 64 of file cppvalue.cpp.

65{
66 if (token[1]=='\\')
67 {
68 switch(token[2])
69 {
70 case 'n': return CPPValue('\n');
71 case 't': return CPPValue('\t');
72 case 'v': return CPPValue('\v');
73 case 'b': return CPPValue('\b');
74 case 'r': return CPPValue('\r');
75 case 'f': return CPPValue('\f');
76 case 'a': return CPPValue('\a');
77 case '\\': return CPPValue('\\');
78 case '?': return CPPValue('\?');
79 case '\'': return CPPValue('\'');
80 case '"': return CPPValue('"');
81 case '0': // fall through
82 case '1': // fall through
83 case '2': // fall through
84 case '3': // fall through
85 case '4': // fall through
86 case '5': // fall through
87 case '6': // fall through
88 case '7': // fall through
89 return parseOctal(token);
90 case 'x':
91 case 'X': return parseHexadecimal(token);
92 default: printf("Invalid escape sequence %s found!\n",token.c_str());
93 return CPPValue(0L);
94 }
95 }
96 return CPPValue(token[1]);
97}
static CPPValue parseOctal(const std::string &token)
Definition cppvalue.cpp:21
static CPPValue parseHexadecimal(const std::string &token)
Definition cppvalue.cpp:41

References CPPValue(), parseHexadecimal(), and parseOctal().

◆ parseDecimal()

CPPValue CPPValue::parseDecimal ( const std::string & token)
static

Definition at line 31 of file cppvalue.cpp.

32{
33 long val = 0;
34 for (const char *p = token.c_str(); *p != 0; p++)
35 {
36 if (*p >= '0' && *p <= '9') val = val * 10 + *p - '0';
37 }
38 return CPPValue(val);
39}

References CPPValue().

◆ parseFloat()

CPPValue CPPValue::parseFloat ( const std::string & token)
static

Definition at line 99 of file cppvalue.cpp.

100{
101 return CPPValue(std::stod(token));
102}

References CPPValue().

◆ parseHexadecimal()

CPPValue CPPValue::parseHexadecimal ( const std::string & token)
static

Definition at line 41 of file cppvalue.cpp.

42{
43 long val = 0;
44 for (const char *p = token.c_str(); *p != 0; p++)
45 {
46 if (*p >= '0' && *p <= '9') val = val * 16 + *p - '0';
47 else if (*p >= 'a' && *p <= 'f') val = val * 16 + *p - 'a' + 10;
48 else if (*p >= 'A' && *p <= 'F') val = val * 16 + *p - 'A' + 10;
49 }
50 //printf("parseHexadecimal %s->%x\n",qPrint(token),val);
51 return CPPValue(val);
52}

References CPPValue().

Referenced by parseCharacter().

◆ parseOctal()

CPPValue CPPValue::parseOctal ( const std::string & token)
static

Definition at line 21 of file cppvalue.cpp.

22{
23 long val = 0;
24 for (const char *p = token.c_str(); *p != 0; p++)
25 {
26 if (*p >= '0' && *p <= '7') val = val * 8 + *p - '0';
27 }
28 return CPPValue(val);
29}

References CPPValue().

Referenced by parseCharacter().

◆ print()

void CPPValue::print ( ) const
inline

Definition at line 37 of file cppvalue.h.

38 {
39 if (type==Int)
40 printf("(%ld)\n",v.l);
41 else
42 printf("(%f)\n",v.d);
43 }

References Int, type, and v.

Member Data Documentation

◆ d

double CPPValue::d

Definition at line 54 of file cppvalue.h.

◆ l

long CPPValue::l

Definition at line 55 of file cppvalue.h.

◆ type

Type CPPValue::type
private

Definition at line 52 of file cppvalue.h.

Referenced by CPPValue(), CPPValue(), CPPValue(), isInt(), operator double(), operator long(), and print().

◆ [union]

union { ... } CPPValue::v

The documentation for this class was generated from the following files: