Skip to content
This repository has been archived by the owner on Jan 14, 2023. It is now read-only.

Commit

Permalink
Use types like uint32_t instead of unsigned int/long in generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jul 29, 2017
1 parent 18d02bb commit 4b3a711
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
7 changes: 3 additions & 4 deletions Source2Gen/SchemaClassGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 13,7 @@

using namespace schema;

std::map<std::string, std::string> SchemaClassGenerator::s_typedefs =
std::unordered_map<std::string, std::string> SchemaClassGenerator::s_typedefs =
{
{ "float32", "float" },
{ "float64", "double" },
Expand All @@ -26,7 26,7 @@ std::map<std::string, std::string> SchemaClassGenerator::s_typedefs =
{ "uint8", "uint8_t" },
{ "uint16", "uint16_t" },
{ "uint32", "uint32_t" },
{ "uint64", "uint64_t" },
{ "uint64", "uint64_t" }
};

std::vector<std::string> SchemaClassGenerator::s_knownTypes =
Expand Down Expand Up @@ -110,8 110,7 @@ std::string& SchemaClassGenerator::Generate(const std::string& genFolder)
}

m_headerDependencies = "#pragma once\n";
m_headerDependencies = "#include <vadefs.h>\n";
m_headerDependencies = "#include <stdint.h>\n";
m_headerDependencies = "#include <cstdint>\n";
m_headerDependencies = "#include \"SchemaBase.hpp\"\n";
m_headerDependencies = "#include \"SchemaSystem.hpp\"\n";
m_headerDependencies = "#include \"UnknownType.hpp\"\n";
Expand Down
4 changes: 2 additions & 2 deletions Source2Gen/SchemaClassGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 2,7 @@

#include <set>
#include <vector>
#include <map>
#include <unordered_map>

#include "Schema.hpp"
#include "SchemaSystem.hpp"
Expand Down Expand Up @@ -68,6 68,6 @@ class SchemaClassGenerator

std::vector<schema::CSchemaClassBinding*> m_classes;

static std::map<std::string, std::string> s_typedefs;
static std::unordered_map<std::string, std::string> s_typedefs;
static std::vector<std::string> s_knownTypes;
};
15 changes: 6 additions & 9 deletions Source2Gen/SchemaEnumGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 33,7 @@ std::string& SchemaEnumGenerator::Generate(const std::string& genFolder)

m_generatedHeader.clear();
m_generatedHeader = "#pragma once\n";
m_generatedHeader = "#include <cstdint>\n";

std::sort(m_enums.begin(), m_enums.end(),
[](CSchemaEnumInfo* a, CSchemaEnumInfo* b)
Expand Down Expand Up @@ -94,26 95,22 @@ std::string SchemaEnumGenerator::Single::GenerateTypeStorage()
if (!enumType)
return typeStorage;

// because we can't accurately check if something is actually negative or just going over the signed point of an integer.
// it will be the same value in the end either way (at a bit level)
std::string typePrefix = "unsigned ";

switch (enumType->GetSize())
{
case 1:
typeStorage = typePrefix "char";
typeStorage = "uint8_t";
break;
case 2:
typeStorage = typePrefix "short";
typeStorage = "uint16_t";
break;
case 4:
typeStorage = typePrefix "long";
typeStorage = "uint32_t";
break;
case 8:
typeStorage = typePrefix "long long";
typeStorage = "uint64_t";
break;
default:
typeStorage = typePrefix "INVALID_TYPE";
typeStorage = "INVALID_TYPE";
}

return typeStorage;
Expand Down
6 changes: 3 additions & 3 deletions Source2Gen/shared/Schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 289,11 @@ namespace schema
if (m_Amount == 1)
memberDeclaration << "bool ";
else if (m_Amount <= 8)
memberDeclaration << "char ";
memberDeclaration << "uint8_t ";
else if (m_Amount <= 16)
memberDeclaration << "short ";
memberDeclaration << "uint16_t ";
else if (m_Amount <= 32)
memberDeclaration << "int ";
memberDeclaration << "uint32_t ";

memberDeclaration << memberName << " : " << m_Amount;

Expand Down

0 comments on commit 4b3a711

Please sign in to comment.