Skip to content

Commit c9db974

Browse files
authored
C++ example
2 parents 657b57e + 076b010 commit c9db974

8 files changed

Lines changed: 463 additions & 4 deletions

File tree

.github/workflows/Pipeline.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,22 @@ jobs:
116116
run: |
117117
cd example/C
118118
pyVersioning fillout ../../templates/C/versioning.c.template versioning.c
119+
cd ../CXX
120+
pyVersioning fillout ../../templates/CXX/versioning.cpp.template versioning.cpp
119121
120122
- name: Compile example
121123
run: |
122124
cd example/C
123125
gcc -o example example.c versioning.c
126+
cd ../CXX
127+
g++ -o example example.cpp versioning.cpp
124128
125129
- name: Execute example
126130
run: |
127131
cd example/C
128132
./example
133+
cd ../CXX
134+
./example
129135
130136
PublishCoverageResults:
131137
uses: pyTooling/Actions/.github/workflows/PublishCoverageResults.yml@r4

example/C/example.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
/* SPDX-License-Identifier: Apache-2.0 *
2828
/**********************************************************************************************************************/
2929
#include <stdio.h>
30+
#include <stdlib.h>
3031

3132
#include "versioning.h"
3233

@@ -67,14 +68,14 @@ void printVersion(void) {
6768
);
6869
}
6970

70-
int main(char** argv, int argc) {
71+
int main(void) {
7172
printf(
7273
"========================================\n"
73-
"pyVersioning Example\n"
74+
"pyVersioning Example C\n"
7475
"========================================\n"
7576
);
7677

7778
printVersion();
7879

79-
return 0;
80+
return EXIT_SUCCESS;
8081
}

example/CXX/.pyVersioning.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: 1
2+
3+
project:
4+
name: Test Project
5+
variant: A2
6+
version: v2.1.6
7+
8+
build:
9+
compiler:
10+
name: g++
11+
version: 10.2.0
12+
configuration: Release
13+
options: -g -O3

example/CXX/example.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/***********************************************************************************************************************
2+
/* __ __ _ _ *
3+
/* _ __ _ \ \ / /__ _ __ ___(_) ___ _ __ (_)_ __ __ _ *
4+
/* | '_ \| | | \ \ / / _ \ '__/ __| |/ _ \| '_ \| | '_ \ / _` | *
5+
/* | |_) | |_| |\ V / __/ | \__ \ | (_) | | | | | | | | (_| | *
6+
/* | .__/ \__, | \_/ \___|_| |___/_|\___/|_| |_|_|_| |_|\__, | *
7+
/* |_| |___/ |___/ *
8+
/***********************************************************************************************************************
9+
/* @author Patrick Lehmann *
10+
/* *
11+
/* @brief Code example in C++ *
12+
/* *
13+
/* @copyright Copyright 2020-2024 Patrick Lehmann - Boetzingen, Germany *
14+
/* *
15+
/* Licensed under the Apache License, Version 2.0 (the "License"); *
16+
/* you may not use this file except in compliance with the License. *
17+
/* You may obtain a copy of the License at *
18+
/* *
19+
/* http://www.apache.org/licenses/LICENSE-2.0 *
20+
/* *
21+
/* Unless required by applicable law or agreed to in writing, software *
22+
/* distributed under the License is distributed on an "AS IS" BASIS, *
23+
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
24+
/* See the License for the specific language governing permissions and *
25+
/* limitations under the License. *
26+
/* *
27+
/* SPDX-License-Identifier: Apache-2.0 *
28+
/**********************************************************************************************************************/
29+
#include <iostream>
30+
#include <iomanip>
31+
#include <cstdlib>
32+
33+
#include "versioning.hpp"
34+
35+
void printVersion()
36+
{
37+
using namespace pyVersioning;
38+
39+
std::cout << "Project: "
40+
<< versioningInformation.project.name
41+
<< " - "
42+
<< versioningInformation.project.variant
43+
<< "\n";
44+
45+
std::cout << "Version: v"
46+
<< std::to_string(versioningInformation.version.major)
47+
<< "."
48+
<< std::to_string(versioningInformation.version.minor)
49+
<< "."
50+
<< std::to_string(versioningInformation.version.patch)
51+
<< "\n";
52+
53+
std::cout << "Git: "
54+
<< versioningInformation.git.reference
55+
<< " - "
56+
<< std::setw(2) << std::setfill('0') << std::to_string(versioningInformation.git.commit.datetime.date.day)
57+
<< "."
58+
<< std::setw(2) << std::setfill('0') << std::to_string(versioningInformation.git.commit.datetime.date.month)
59+
<< "."
60+
<< std::setw(2) << std::setfill('0') << std::to_string(versioningInformation.git.commit.datetime.date.year)
61+
<< "-"
62+
<< std::setw(2) << std::setfill('0') << std::to_string(versioningInformation.git.commit.datetime.time.hour)
63+
<< ":"
64+
<< std::setw(2) << std::setfill('0') << std::to_string(versioningInformation.git.commit.datetime.time.minute)
65+
<< ":"
66+
<< std::setw(2) << std::setfill('0') << std::to_string(versioningInformation.git.commit.datetime.time.second)
67+
<< "\n";
68+
69+
std::cout << " " << versioningInformation.git.commit.hash << "\n";
70+
std::cout << " " << versioningInformation.git.repository << "\n";
71+
std::cout << "Build on: "
72+
<< versioningInformation.git.reference
73+
<< " - "
74+
<< std::setw(2) << std::setfill('0') << std::to_string(versioningInformation.build.datetime.date.day)
75+
<< "."
76+
<< std::setw(2) << std::setfill('0') << std::to_string(versioningInformation.build.datetime.date.month)
77+
<< "."
78+
<< std::setw(2) << std::setfill('0') << std::to_string(versioningInformation.build.datetime.date.year)
79+
<< "-"
80+
<< std::setw(2) << std::setfill('0') << std::to_string(versioningInformation.build.datetime.time.hour)
81+
<< ":"
82+
<< std::setw(2) << std::setfill('0') << std::to_string(versioningInformation.build.datetime.time.minute)
83+
<< ":"
84+
<< std::setw(2) << std::setfill('0') << std::to_string(versioningInformation.build.datetime.time.second)
85+
<< "\n";
86+
87+
std::cout << "Compiler: "
88+
<< versioningInformation.build.compiler.name
89+
<< " ("
90+
<< std::to_string(versioningInformation.build.compiler.version.major)
91+
<< "."
92+
<< std::to_string(versioningInformation.build.compiler.version.minor)
93+
<< "."
94+
<< std::to_string(versioningInformation.build.compiler.version.patch)
95+
<< ")" << std::endl;
96+
}
97+
98+
int main()
99+
{
100+
std::cout << "========================================\n"
101+
<< "pyVersioning Example C++\n"
102+
<< "========================================"
103+
<< std::endl;
104+
105+
printVersion();
106+
107+
return EXIT_SUCCESS;
108+
}

example/CXX/versioning.hpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/***********************************************************************************************************************
2+
/* __ __ _ _ *
3+
/* _ __ _ \ \ / /__ _ __ ___(_) ___ _ __ (_)_ __ __ _ *
4+
/* | '_ \| | | \ \ / / _ \ '__/ __| |/ _ \| '_ \| | '_ \ / _` | *
5+
/* | |_) | |_| |\ V / __/ | \__ \ | (_) | | | | | | | | (_| | *
6+
/* | .__/ \__, | \_/ \___|_| |___/_|\___/|_| |_|_|_| |_|\__, | *
7+
/* |_| |___/ |___/ *
8+
/***********************************************************************************************************************
9+
/* @author Patrick Lehmann *
10+
/* *
11+
/* @brief C++ Structure definitions for pyVersioning *
12+
/* *
13+
/* @copyright Copyright 2020-2024 Patrick Lehmann - Boetzingen, Germany *
14+
/* *
15+
/* Licensed under the Apache License, Version 2.0 (the "License"); *
16+
/* you may not use this file except in compliance with the License. *
17+
/* You may obtain a copy of the License at *
18+
/* *
19+
/* http://www.apache.org/licenses/LICENSE-2.0 *
20+
/* *
21+
/* Unless required by applicable law or agreed to in writing, software *
22+
/* distributed under the License is distributed on an "AS IS" BASIS, *
23+
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
24+
/* See the License for the specific language governing permissions and *
25+
/* limitations under the License. *
26+
/* *
27+
/* SPDX-License-Identifier: Apache-2.0 *
28+
/**********************************************************************************************************************/
29+
30+
#ifndef VERSIONING_H
31+
#define VERSIONING_H
32+
33+
#include <cstdint>
34+
35+
// Check if <string_view> is available using __has_include.
36+
#if defined(__has_include)
37+
#if __has_include(<string_view>)
38+
#include <string_view>
39+
#define PYVERSIONING_HAS_STRING_VIEW
40+
#endif // #if __has_include(<string_view>)
41+
#endif // #if defined(__has_include)
42+
43+
namespace pyVersioning
44+
{
45+
46+
#ifdef PYVERSIONING_HAS_STRING_VIEW
47+
using FixedString_t = std::string_view;
48+
#elif __cplusplus >= 201103L
49+
using FixedString_t = char const *;
50+
#else
51+
typedef char const * FixedString_t;
52+
#endif
53+
54+
struct Date {
55+
uint8_t day;
56+
uint8_t month;
57+
uint16_t year;
58+
};
59+
60+
struct Time {
61+
uint8_t hour;
62+
uint8_t minute;
63+
uint8_t second;
64+
};
65+
66+
struct DateTime {
67+
Date date;
68+
Time time;
69+
};
70+
71+
struct Version {
72+
uint8_t flags;
73+
uint16_t major;
74+
uint16_t minor;
75+
uint16_t patch;
76+
};
77+
78+
struct Commit {
79+
pyVersioning::FixedString_t hash;
80+
DateTime datetime;
81+
};
82+
83+
struct Git {
84+
Commit commit;
85+
pyVersioning::FixedString_t reference;
86+
pyVersioning::FixedString_t repository;
87+
};
88+
89+
struct Project {
90+
pyVersioning::FixedString_t name;
91+
pyVersioning::FixedString_t variant;
92+
};
93+
94+
struct Compiler {
95+
pyVersioning::FixedString_t name;
96+
Version version;
97+
pyVersioning::FixedString_t configuration;
98+
pyVersioning::FixedString_t options;
99+
};
100+
101+
struct Build {
102+
DateTime datetime;
103+
Compiler compiler;
104+
};
105+
106+
struct VersioningInformation {
107+
Version version;
108+
Git git;
109+
Project project;
110+
Build build;
111+
};
112+
113+
} // namespace pyVersioning
114+
115+
extern const pyVersioning::VersioningInformation versioningInformation;
116+
117+
#endif /* VERSIONING_H */

templates/C/versioning.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
/* *
2727
/* SPDX-License-Identifier: Apache-2.0 *
2828
/**********************************************************************************************************************/
29-
#include <stdint.h>
3029

3130
#ifndef VERSIONING_H
3231
#define VERSIONING_H
3332

33+
#include <stdint.h>
34+
3435
typedef struct {
3536
uint8_t day;
3637
uint8_t month;

0 commit comments

Comments
 (0)