Changeset 1005 for cpp/common


Ignore:
Timestamp:
07/14/20 15:54:43 (4 years ago)
Author:
Maciej Komosinski
Message:

Higher conformance with C++17, but gave up after missing M_PI, M_PI_2, strdup() and more; other cosmetic improvements

Location:
cpp/common
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/Convert.cpp

    r400 r1005  
    44
    55#include "Convert.h"
    6 
    76#include <sstream>
    87
     
    9190        struct tm ret;
    9291#if defined LINUX // || android?
    93         return *::localtime_r(&timep,&ret);
     92        return *::localtime_r(&timep, &ret);
    9493#elif defined _WIN32 && !defined __BORLANDC__
    9594        ::localtime_s(&ret, &timep);
     
    9796#else //borland?
    9897        pthread_mutex_lock(&fix_unsafe_mutex);
    99         ret=*::localtime(&timep);
     98        ret = *::localtime(&timep);
    10099        pthread_mutex_unlock(&fix_unsafe_mutex);
    101100        return ret;
     
    110109#ifndef MULTITHREADED
    111110
    112         ret=::asctime(&tm);
     111        ret = ::asctime(&tm);
    113112
    114113#else //MULTITHREADED
     
    116115        char buf[26];
    117116#if defined LINUX // || android?
    118         ret=::asctime_r(&tm,buf);
     117        ret = ::asctime_r(&tm, buf);
    119118#elif defined _WIN32 && !defined __BORLANDC__
    120119        asctime_s(buf, sizeof(buf), &tm);
     
    122121#else //borland?
    123122        pthread_mutex_lock(&fix_unsafe_mutex);
    124         strcpy(buf,::asctime(&tm));
    125         ret=buf;
     123        strcpy(buf, ::asctime(&tm));
     124        ret = buf;
    126125        pthread_mutex_unlock(&fix_unsafe_mutex);
    127126#endif
  • cpp/common/Convert.h

    r913 r1005  
    1111#include "2d.h"
    1212#include <stdint.h>
     13#include <cmath>
    1314
    1415
     
    3940        static uint32_t hexToInt(const string& col);
    4041
    41         static double toRadians(double angle) { return angle*M_PI / 180; }
     42        static double toRadians(double angle) { return angle * M_PI / 180; }
    4243        static double toDegrees(double angle) { return angle / M_PI * 180; }
    4344        static double atan_2(double y, double x) { if (x == 0 && y == 0) return 0; else return atan2(y, x); } //needed by borland 5/6 only?
     
    4647        {
    4748                double dx = x2 - x1, dy = y2 - y1;
    48                 return dx*dx + dy*dy;
     49                return dx * dx + dy * dy;
    4950        }
    5051        static double odleglosc_sq(const Pt2D& p1, const Pt2D& p2) //odleglosc do kwadratu
     
    5354        }
    5455
    55         static double odleglosc(double x1, double y1, double x2, double y2)     { return sqrt(odleglosc_sq(x1, y1, x2, y2)); }
     56        static double odleglosc(double x1, double y1, double x2, double y2) { return sqrt(odleglosc_sq(x1, y1, x2, y2)); }
    5657        static double odleglosc(const Pt2D& p1, const Pt2D& p2)
    5758        {
  • cpp/common/random.h

    r896 r1005  
    66
    77#ifdef _MSC_VER
    8  #define NOMINMAX //we don't want Windows headers (minwindef.h) to define min() and max() as macros
    9  #undef min //for some reason, NOMINMAX did not work so we have to #undef anyway
    10  #undef max
     8#define NOMINMAX //we don't want Windows headers (minwindef.h) to define min() and max() as macros
     9#undef min //for some reason, NOMINMAX did not work so we have to #undef anyway
     10#undef max
    1111#endif
    1212
     
    1414#include <stdint.h> //uintptr_t
    1515#ifdef MULTITHREADED
    16  #include "threads.h"
     16#include "threads.h"
    1717#endif
    1818#ifdef LINUX
    19  #include <unistd.h>
    20  #include <sys/stat.h>
    21  #include <fcntl.h>
     19#include <unistd.h>
     20#include <sys/stat.h>
     21#include <fcntl.h>
    2222#endif
    2323#ifdef _WIN32
    24  #define _WINSOCKAPI_ //http://stackoverflow.com/questions/1372480/c-redefinition-header-files
    25  #include <rpc.h> //UUID
    26  #pragma comment(lib, "Rpcrt4.lib")
     24#define _WINSOCKAPI_ //http://stackoverflow.com/questions/1372480/c-redefinition-header-files
     25#include <rpc.h> //UUID
     26#pragma comment(lib, "Rpcrt4.lib")
    2727#endif
    2828
     
    6363                mt[0] = seed;
    6464                for (unsigned int i = 1; i < length; i++)
    65                         mt[i] = (1812433253 * (mt[i - 1] ^ (mt[i - 1] >> 30)) + i)&bitMask_32;
     65                        mt[i] = (1812433253 * (mt[i - 1] ^ (mt[i - 1] >> 30)) + i) & bitMask_32;
    6666#ifdef MULTITHREADED
    6767                pthread_mutex_unlock(&lock);
     
    7474                //for ms visual, could use http://msdn.microsoft.com/en-us/library/sxtz2fa8.aspx
    7575#ifdef LINUX
    76                 int fd=open("/dev/urandom",O_RDONLY);
    77                 if (fd>=0)
     76                int fd = open("/dev/urandom", O_RDONLY);
     77                if (fd >= 0)
    7878                {
    79                         read(fd,&seed,sizeof(seed));
     79                        read(fd, &seed, sizeof(seed));
    8080                        close(fd);
    8181                }
     
    8484                {
    8585                        counter++;
    86                         seed = (unsigned int)time(NULL);                         //time (seconds); could use hi-res timer but then we would depend on common/timer.h
     86                        seed = (unsigned int)time(NULL);           //time (seconds); could use hi-res timer but then we would depend on common/timer.h
    8787                        seed ^= counter;                           //incremented value, possibly randomly initialized
    8888                        seed ^= (unsigned int)(uintptr_t)&counter; //memory address
     
    9191                UUID uuid;
    9292                ::UuidCreate(&uuid);
    93                 seed ^= uuid.Data1^uuid.Data2^uuid.Data3^uuid.Data4[0];
     93                seed ^= uuid.Data1 ^ uuid.Data2 ^ uuid.Data3 ^ uuid.Data4[0];
    9494#endif
    9595                setSeed(seed);
     
    117117        //UniformRandomBitGenerator
    118118        typedef unsigned int result_type;
    119         static constexpr unsigned int min() {return 0;}
    120         static constexpr unsigned int max() {return MAXVALUE;}
    121         inline unsigned int operator()() {return getUint32();}
     119        static constexpr unsigned int min() { return 0; }
     120        static constexpr unsigned int max() { return MAXVALUE; }
     121        inline unsigned int operator()() { return getUint32(); }
    122122
    123123        inline double getDouble() // [0,1)
Note: See TracChangeset for help on using the changeset viewer.