source: to-imperative/trunk/libp++/pxx_common.hh @ 1734

Last change on this file since 1734 was 1734, checked in by orlov, 16 years ago
  • Fixes for building with Visual C++.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1//
2// Copyright (C) 2000, 2001 Andrey Slepuhin <pooh@msu.ru>
3//
4// libp++ is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 2 of the License, or
7// (at your option) any later version.
8//
9// libp++ is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with libp++; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17//
18// $Source$
19// $Revision: 1734 $
20// $Date: 2005-02-25 01:15:45 +0000 (Fri, 25 Feb 2005) $
21// Author: Andrey Slepuhin <pooh@msu.ru>
22
23#ifndef __pxx_common_hh__
24#define __pxx_common_hh__
25
26//
27// This is needed to allow the use of macros for integer constant
28//  initialization.
29#define __STDC_CONSTANT_MACROS
30#define __STDC_LIMIT_MACROS
31#define __STDC_FORMAT_MACROS
32
33#ifndef WINDOWS
34#include <inttypes.h>
35#include <unistd.h>    // ssize_t in FreeBSD
36#else
37#include <stddef.h>    // for integer types like intptr_t and size_t
38#include <malloc.h>    // for _alloca()
39#endif
40
41#include <stdio.h>
42#include <stdlib.h>
43#include <errno.h>
44
45#ifdef WINDOWS
46#define WSTRPREFIX  L
47#else
48#define WSTRPREFIX  L""
49#endif
50
51#ifdef WINDOWS
52#define snprintf             _snprintf
53#define alloca               _alloca
54#define __PRETTY_FUNCTION__  __FUNCTION__
55#define sleep                ::Sleep
56
57#ifdef  _WIN64
58typedef __int64    ssize_t;
59#else
60typedef _W64 int   ssize_t;
61#endif
62
63#ifdef  _WIN64
64#define __PRIPTR_PREFIX      "l"
65#else
66#define __PRIPTR_PREFIX
67#endif
68
69/* Macros for printing `intptr_t' and `uintptr_t'.  */
70#define PRIdPTR        __PRIPTR_PREFIX "d"
71#define PRIiPTR        __PRIPTR_PREFIX "i"
72#define PRIoPTR        __PRIPTR_PREFIX "o"
73#define PRIuPTR        __PRIPTR_PREFIX "u"
74#define PRIxPTR        __PRIPTR_PREFIX "x"
75#define PRIXPTR        __PRIPTR_PREFIX "X"
76
77typedef          __int8         int8_t;
78typedef          __int16        int16_t;
79typedef          __int32        int32_t;
80typedef          __int64        int64_t;
81
82typedef unsigned __int8         uint8_t;
83typedef unsigned __int16        uint16_t;
84typedef unsigned __int32        uint32_t;
85typedef unsigned __int64        uint64_t;
86
87/* Values to test for integral types holding `void *' pointer.  */
88#if _WIN64
89# define INTPTR_MIN            (-9223372036854775807L-1)
90# define INTPTR_MAX            (9223372036854775807L)
91# define UINTPTR_MAX           (18446744073709551615UL)
92#else
93# define INTPTR_MIN            (-2147483647-1)
94# define INTPTR_MAX            (2147483647)
95# define UINTPTR_MAX           (4294967295U)
96#endif
97
98#if __WIN64
99# define __INT64_C(c)  c ## L
100# define __UINT64_C(c) c ## UL
101#else
102# define __INT64_C(c)  c ## LL
103# define __UINT64_C(c) c ## ULL
104#endif
105
106#define UINT32_MAX             (4294967295U)
107#define UINT64_MAX             (__UINT64_C(18446744073709551615))
108
109/* Limit of `size_t' type.  */
110#if __WIN64
111# define SIZE_MAX              (18446744073709551615UL)
112#else
113# define SIZE_MAX              (4294967295U)
114#endif
115
116//#define NOGDI
117
118#endif // WINDOWS
119
120
121namespace pxx
122{
123
124#ifndef DEBUG
125#define DEBUG 0
126#endif
127
128#ifndef PARANOIA
129#define PARANOIA 0
130#endif
131
132//
133// Define macros for uintptr_t type.
134#if UINTPTR_MAX == UINT32_MAX
135#define UINTPTR_C(x) UINT32_C(x)
136#elif UINTPTR_MAX == UINT64_MAX
137#define UINTPTR_C(x) UINT64_C(x)
138#else
139#error "Unsupported pointer size"
140#endif
141
142//
143// Useful macros.
144#define null (0)
145#define self (*this)
146#define zref(_type) (*((_type*)(0)))
147
148//
149// A macro to use in a case of fatal errors. Prints a message and source file
150// information (function name, file name and line number).
151#define FATAL \
152  fprintf( \
153    stderr,"Fatal error in %s\nat %s:%d\n", \
154    __PRETTY_FUNCTION__, __FILE__, __LINE__ \
155  ), \
156  pxx::fatal
157
158#define PMETHOD \
159  fprintf(stderr, "%p: %s\n", this, __PRETTY_FUNCTION__)
160
161#define TRY(a) \
162  try { \
163    a; \
164  } catch (pxx::SysError const& err) { \
165    FATAL("System error code = %d", err.get_code()); \
166  }
167
168#define NO_COPY_CTOR(cls) \
169  cls (cls const&) ;
170
171#define NO_ASSIGN(cls) \
172  cls& operator = (cls const&) ;
173
174extern unsigned debug_level ;
175
176//
177// A function to be called when fatal error occured. By default exit is called.
178extern void (*terminate) (int _error_code) ;
179//
180// A function to print fatal error messages.
181extern void fatal (char* _fmt, ...) ;
182//
183// A function to print debug messages.
184extern void dprint (unsigned _level, char* _fmt, ...) ;
185//
186// A memory page size. Should be initialized at runtime due to architectures
187// with a variable page size.
188extern size_t& page_size ;
189//
190// An array containing ceilings of binary logarythms of small integers.
191extern size_t (&orders)[256] ;
192//
193// An array containing exponents of 2.
194extern size_t (&exps_of_two)[sizeof(size_t) * 8] ;
195
196//
197// This function returns a ceiling of a binary logarythm of its argument.
198inline size_t get_order (size_t _size) ;
199//
200// This function checks whether a pointer is properly aligned. The second
201// argument should be an exponent of 2.
202inline bool ptr_is_aligned (void const* _ptr, uintptr_t _align) ;
203//
204// This function aligns a pointer, rounding it to a smaller value.
205inline void* ptr_align (void const* _ptr, uintptr_t _align) ;
206//
207// This function aligns an integer size, rounding it to a higher value.
208inline size_t size_align (size_t _size, size_t _align) ;
209//
210// These functions implement bit operations on pointer values.
211inline void* ptr_and (void const* _p1, void const* _p2) ;
212inline void* ptr_or (void const* _p1, void const* _p2) ;
213inline void* ptr_xor (void const* _p1, void const* _p2) ;
214//
215// This function finds the least significant bit by which two pointers differ
216// and returns an integer where this bit is set.
217inline uintptr_t ptr_diff (void const* _p1, void const* _p2) ;
218//
219// Pointer arithmetic.
220template <typename type_t>
221  inline type_t* ptr_add_offset (type_t* _ptr, ssize_t _value) ;
222template <typename type_t>
223  inline type_t* ptr_sub_offset (type_t* _ptr, ssize_t _value) ;
224template <typename type1_t, typename type2_t>
225  inline ssize_t ptr_sub (type1_t* _ptr1, type2_t* _ptr2) ;
226
227//
228// We provide our own maximum and minimum macros to avoid naming conflicts
229#define pxx_max(a,b) ((a) > (b) ? (a) : (b))
230#define pxx_min(a,b) ((a) < (b) ? (a) : (b))
231
232///
233/// This enumeration type used as argument of constructors of some data types
234/// may indicate that constructor argument is static constant data that
235/// shouldn't be copied.
236typedef enum
237{
238  const_data,
239  non_const_data
240} const_t ;
241
242typedef enum
243{
244  mode_read = 1,
245  mode_write = 2,
246  mode_append = mode_read | 4,
247  mode_read_write = mode_read | mode_write
248} io_mode_t ;
249
250//class Init ;
251//extern Init* __pinit ;
252//static Init* __static_pinit = __pinit ;
253
254}
255
256#endif // __pxx_common_hh__
Note: See TracBrowser for help on using the repository browser.