source: cpp/common/nonstd_stl.h @ 1314

Last change on this file since 1314 was 1130, checked in by Maciej Komosinski, 4 years ago

Used std::min(), std::max() explicitly to avoid compiler confusion. Used std::size() explicitly instead of the equivalent macro

  • Property svn:eol-style set to native
File size: 1.9 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#ifndef _NONSTD_STL_H_
6#define _NONSTD_STL_H_
7
8//making STL more standard
9
10#include <string>
11using std::string;
12#ifndef SHP //STL in the bada OS has no wstring
13using std::wstring;
14#endif
15
16#include <vector>
17using std::vector;
18
19
20//below: not used since 2020 (these macros are replaced by std::ssize()), may be removed...
21
22// ------------------- ARRAY_LENGTH -------------------
23
24//old-fashioned macro, unprotected against the use of the pointer as "x"
25//#define ARRAY_LENGTH(x) (sizeof(x)/sizeof((x)[0]))
26
27//hacker macro that detects some of the misuse cases
28//#define ARRAY_LENGTH(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
29
30//template function by intermediate-level devs, as a function it unfortunately does not give a constant at compile time
31//template<typename T, std::size_t N> inline std::size_t ARRAY_LENGTH( T(&)[N] ) { return N; } //"constexpr" dopiero w C++0x
32
33//hacker templates: array of bytes of length N - as long as the array we are asking for...
34//template <typename T, std::size_t N>
35//char (&array_temp(T (&a)[N]))[N];
36
37// As litb noted in comments, you need this overload to handle array rvalues
38// correctly (e.g. when array is a member of a struct returned from function),
39// since they won't bind to non-const reference in the overload above.
40//template <typename T, std::size_t N>
41//char (&array_temp(const T (&a)[N]))[N];
42
43//...which can then be used in sizeof and thus we have const in compile-time. This is as useful as the old-fashioned macro above, but with full error control
44//#define ARRAY_LENGTH(x) sizeof(array_temp(x))
45
46//final and no longer needed version ;-) (c++17)
47//#define ARRAY_LENGTH(x) int(std::size(x))
48//(still room for improvement because unsigned=risky, ssize() upcoming since C++20)
49
50
51#endif
Note: See TracBrowser for help on using the repository browser.