Skip to content

Commit 0bf5714

Browse files
authored
Merge pull request #6725 from Goober5000/time_lib_stuff
enhance scripted time library a bit
2 parents 6ddb57b + 778e2a5 commit 0bf5714

File tree

7 files changed

+74
-5
lines changed

7 files changed

+74
-5
lines changed

code/globalincs/toolchain/clang.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* include toolchain.h which will pull in the file appropriate to
1717
* the detected toolchain.
1818
*/
19+
#pragma once
1920

2021
#if defined(__clang__)
2122

@@ -63,6 +64,9 @@
6364
#define SIZE_T_ARG "%zu"
6465
#define PTRDIFF_T_ARG "%zd"
6566

67+
#define UINT64_T_ARG "%" PRIu64
68+
#define INT64_T_ARG "%" PRId64
69+
6670
#define likely(x) __builtin_expect((long) !!(x), 1L)
6771
#define unlikely(x) __builtin_expect((long) !!(x), 0L)
6872

code/globalincs/toolchain/doxygen.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* or otherwise commercially exploit the source or things you created based on
66
* the source.
77
*/
8+
#pragma once
89

910
#if defined(DOXYGEN)
1011
/**
@@ -46,6 +47,22 @@
4647
*/
4748
#define PTRDIFF_T_ARG "%zd"
4849

50+
/**
51+
* @brief Format specifier for a @c uint64_t argument
52+
*
53+
* Due to different runtimes using different format specifier for these types
54+
* it's necessary to hide these changes behind a macro.
55+
*/
56+
#define UINT64_T_ARG "%" PRIu64
57+
58+
/**
59+
* @brief Format specifier for an @c int64_t argument
60+
*
61+
* Due to different runtimes using different format specifier for these types
62+
* it's necessary to hide these changes behind a macro.
63+
*/
64+
#define INT64_T_ARG "%" PRId64
65+
4966
/**
5067
* @brief Attribute for forcing a static variable to be instantiated
5168
*

code/globalincs/toolchain/gcc.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
* include toolchain.h which will pull in the file appropriate to
1717
* the detected toolchain.
1818
*/
19+
#pragma once
1920

20-
#if defined(__GNUC__)
21+
// per toolchain.h, clang takes precedence over gcc if they are both defined
22+
#if defined(__GNUC__) && !defined(__clang__)
2123

2224
#define SCP_FORMAT_STRING
2325
#define SCP_FORMAT_STRING_ARGS(x,y) __attribute__((format(printf, x, y)))
@@ -62,6 +64,9 @@
6264
#define SIZE_T_ARG "%zu"
6365
#define PTRDIFF_T_ARG "%zd"
6466

67+
#define UINT64_T_ARG "%" PRIu64
68+
#define INT64_T_ARG "%" PRId64
69+
6570
#define likely(x) __builtin_expect((long) !!(x), 1L)
6671
#define unlikely(x) __builtin_expect((long) !!(x), 0L)
6772

code/globalincs/toolchain/mingw.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* include toolchain.h which will pull in the file appropriate to
1717
* the detected toolchain.
1818
*/
19+
#pragma once
1920

2021
#if defined(__MINGW32__)
2122

@@ -59,6 +60,9 @@
5960
#define SIZE_T_ARG "%zu"
6061
#define PTRDIFF_T_ARG "%zd"
6162

63+
#define UINT64_T_ARG "%" PRIu64
64+
#define INT64_T_ARG "%" PRId64
65+
6266
#define likely(x) __builtin_expect((long) !!(x), 1L)
6367
#define unlikely(x) __builtin_expect((long) !!(x), 0L)
6468

code/globalincs/toolchain/msvc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* include toolchain.h which will pull in the file appropriate to
1717
* the detected toolchain.
1818
*/
19+
#pragma once
1920

2021
#if defined(_MSC_VER)
2122

@@ -60,6 +61,9 @@
6061
#define SIZE_T_ARG "%Iu"
6162
#define PTRDIFF_T_ARG "%Id"
6263

64+
#define UINT64_T_ARG "%I64u"
65+
#define INT64_T_ARG "%I64d"
66+
6367
#define likely(x) (x)
6468
#define unlikely(x) (x)
6569

code/parse/sexp_container.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#include "gamesequence/gamesequence.h"
1212
#include "globalincs/pstypes.h"
13-
#include "globalincs/toolchain.h"
1413
#include "mission/missiongoals.h"
1514
#include "parse/generic_log.h"
1615
#include "parse/parselo.h"

code/scripting/api/objs/time_obj.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//
22
//
33
#include "time_obj.h"
4+
#include "globalincs/pstypes.h"
45

56
namespace scripting {
67
namespace api {
78

8-
ADE_OBJ(l_Timestamp, uint64_t, "timestamp", "A real time time stamp of unspecified precision and resolution.");
9+
ADE_OBJ(l_Timestamp, uint64_t, "timestamp", "A real-time timestamp of unspecified precision and resolution.");
910

1011
ADE_FUNC(__sub, l_Timestamp, "timestamp other", "Computes the difference between two timestamps", "timespan",
1112
"The time difference")
@@ -20,9 +21,44 @@ ADE_FUNC(__sub, l_Timestamp, "timestamp other", "Computes the difference between
2021
return ade_set_args(L, "o", l_TimeSpan.Set(diff));
2122
}
2223

24+
ADE_FUNC(__tostring, l_Timestamp, nullptr, "Converts a timestamp to a string", "string", "Timestamp as string, or empty string if handle is invalid")
25+
{
26+
uint64_t value = 0;
27+
if (!ade_get_args(L, "o", l_Timestamp.Get(&value)))
28+
return ade_set_error(L, "s", "");
29+
30+
char buf[32];
31+
sprintf(buf, UINT64_T_ARG, value);
32+
33+
return ade_set_args(L, "s", buf);
34+
}
35+
36+
ADE_FUNC(getSeconds, l_Timestamp, nullptr, "Gets the value of this timestamp in seconds", "number", "The timestamp value in seconds")
37+
{
38+
uint64_t value = 0;
39+
if (!ade_get_args(L, "o", l_Timestamp.Get(&value)))
40+
return ade_set_error(L, "f", 0.0f);
41+
42+
// Timestamps and spans are stored in nanoseconds
43+
return ade_set_args(L, "f", static_cast<float>(static_cast<long double>(value) / static_cast<long double>(NANOSECONDS_PER_SECOND)));
44+
}
45+
46+
2347
ADE_OBJ(l_TimeSpan, int64_t, "timespan", "A difference between two time stamps");
2448

25-
ADE_FUNC(getSeconds, l_TimeSpan, nullptr, "Gets the value of this timestamp in seconds", "number",
49+
ADE_FUNC(__tostring, l_TimeSpan, nullptr, "Converts a timespan to a string", "string", "Timespan as string, or empty string if handle is invalid")
50+
{
51+
int64_t value = 0;
52+
if (!ade_get_args(L, "o", l_TimeSpan.Get(&value)))
53+
return ade_set_error(L, "s", "");
54+
55+
char buf[32];
56+
sprintf(buf, INT64_T_ARG, value);
57+
58+
return ade_set_args(L, "s", buf);
59+
}
60+
61+
ADE_FUNC(getSeconds, l_TimeSpan, nullptr, "Gets the value of this timespan in seconds", "number",
2662
"The timespan value in seconds")
2763
{
2864
int64_t value = 0;
@@ -31,7 +67,7 @@ ADE_FUNC(getSeconds, l_TimeSpan, nullptr, "Gets the value of this timestamp in s
3167
}
3268

3369
// Timestamps and spans are stored in nanoseconds
34-
return ade_set_args(L, "f", (float)((long double)value / (long double)NANOSECONDS_PER_SECOND));
70+
return ade_set_args(L, "f", static_cast<float>(static_cast<long double>(value) / static_cast<long double>(NANOSECONDS_PER_SECOND)));
3571
}
3672

3773
} // namespace api

0 commit comments

Comments
 (0)