source: cpp/common/nonstd_stl.h @ 1258

Last change on this file since 1258 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
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[1130]2// Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
[286]3// See LICENSE.txt for details.
[122]4
[109]5#ifndef _NONSTD_STL_H_
6#define _NONSTD_STL_H_
7
[1130]8//making STL more standard
[109]9
10#include <string>
11using std::string;
[1130]12#ifndef SHP //STL in the bada OS has no wstring
[109]13using std::wstring;
14#endif
15
16#include <vector>
17using std::vector;
18
19
[1130]20//below: not used since 2020 (these macros are replaced by std::ssize()), may be removed...
[109]21
22// ------------------- ARRAY_LENGTH -------------------
23
[1130]24//old-fashioned macro, unprotected against the use of the pointer as "x"
[109]25//#define ARRAY_LENGTH(x) (sizeof(x)/sizeof((x)[0]))
26
[1130]27//hacker macro that detects some of the misuse cases
[109]28//#define ARRAY_LENGTH(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
29
[1130]30//template function by intermediate-level devs, as a function it unfortunately does not give a constant at compile time
[109]31//template<typename T, std::size_t N> inline std::size_t ARRAY_LENGTH( T(&)[N] ) { return N; } //"constexpr" dopiero w C++0x
32
[1130]33//hacker templates: array of bytes of length N - as long as the array we are asking for...
[1027]34//template <typename T, std::size_t N>
35//char (&array_temp(T (&a)[N]))[N];
[109]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.
[1027]40//template <typename T, std::size_t N>
41//char (&array_temp(const T (&a)[N]))[N];
[109]42
[1130]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
[1027]44//#define ARRAY_LENGTH(x) sizeof(array_temp(x))
[109]45
[1027]46//final and no longer needed version ;-) (c++17)
[1130]47//#define ARRAY_LENGTH(x) int(std::size(x))
48//(still room for improvement because unsigned=risky, ssize() upcoming since C++20)
[109]49
[1027]50
[109]51#endif
Note: See TracBrowser for help on using the repository browser.