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

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

#include <src/cppvalue.h>

Public Types

enum class  Type { Int , Float }

Public Member Functions

constexpr CPPValue (char c) noexcept
constexpr CPPValue (long val=0) noexcept
constexpr CPPValue (double val) noexcept
constexpr operator double () const noexcept
constexpr operator long () const noexcept
constexpr Type type () const noexcept
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 m_type
double m_d
long m_l

Detailed Description

A class representing a C-preprocessor value.

Definition at line 23 of file cppvalue.h.

Member Enumeration Documentation

◆ Type

enum class CPPValue::Type
strong
Enumerator
Int 
Float 

Definition at line 26 of file cppvalue.h.

26{ Int, Float };

Constructor & Destructor Documentation

◆ CPPValue() [1/3]

CPPValue::CPPValue ( char c)
inlineexplicitconstexprnoexcept

Definition at line 28 of file cppvalue.h.

28: m_type(Type::Int), m_d(0.0), m_l(c) {}
Type m_type
Definition cppvalue.h:52
long m_l
Definition cppvalue.h:54
double m_d
Definition cppvalue.h:53

References Int.

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

◆ CPPValue() [2/3]

CPPValue::CPPValue ( long val = 0)
inlineexplicitconstexprnoexcept

Definition at line 29 of file cppvalue.h.

29: m_type(Type::Int), m_d(0.0), m_l(val) {}

References m_d, m_l, and m_type.

◆ CPPValue() [3/3]

CPPValue::CPPValue ( double val)
inlineexplicitconstexprnoexcept

Definition at line 30 of file cppvalue.h.

30: m_type(Type::Float), m_d(val), m_l(0) {}

References Float.

Member Function Documentation

◆ operator double()

CPPValue::operator double ( ) const
inlineconstexprnoexcept

Definition at line 32 of file cppvalue.h.

32{ return m_type==Type::Int ? static_cast<double>(m_l) : m_d; }

References Int, m_d, m_l, and m_type.

◆ operator long()

CPPValue::operator long ( ) const
inlineconstexprnoexcept

Definition at line 33 of file cppvalue.h.

33{ return m_type==Type::Int ? m_l : static_cast<long>(m_d); }

References Int, m_d, m_l, and m_type.

◆ parseBinary()

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

Definition at line 55 of file cppvalue.cpp.

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

References CPPValue().

◆ parseCharacter()

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

Definition at line 65 of file cppvalue.cpp.

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

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

◆ parseDecimal()

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

Definition at line 32 of file cppvalue.cpp.

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

References CPPValue().

◆ parseFloat()

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

Definition at line 102 of file cppvalue.cpp.

103{
104 return CPPValue(std::stod(token));
105}

References CPPValue().

◆ parseHexadecimal()

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

Definition at line 42 of file cppvalue.cpp.

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

References CPPValue().

Referenced by parseCharacter().

◆ parseOctal()

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

Definition at line 22 of file cppvalue.cpp.

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

References CPPValue().

Referenced by parseCharacter().

◆ print()

void CPPValue::print ( ) const
inline

Definition at line 37 of file cppvalue.h.

38 {
39 if (m_type==Type::Int)
40 printf("(%ld)\n",m_l);
41 else
42 printf("(%f)\n",m_d);
43 }

References Int, m_d, m_l, and m_type.

◆ type()

Type CPPValue::type ( ) const
inlineconstexprnoexcept

Definition at line 35 of file cppvalue.h.

35{ return m_type; }

References m_type.

Member Data Documentation

◆ m_d

double CPPValue::m_d
private

Definition at line 53 of file cppvalue.h.

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

◆ m_l

long CPPValue::m_l
private

Definition at line 54 of file cppvalue.h.

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

◆ m_type

Type CPPValue::m_type
private

Definition at line 52 of file cppvalue.h.

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


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