- Set a default PCM volume so that something can be heard when driver is
[AROS.git] / test / uae-tmpl / be_ptr
bloba4fda6765dfa5751f7ce09c36759c5a28d2a5d09
2 #include "be_val"
4 typedef long uae_ptr;
6 template<class T> // big endian pointer
7 class be_ptr
9   public:
10     typedef be_ptr<T> my_type;
11     typedef T value_type;
12     typedef T* pointer_type;
14 #ifdef DEBUG
15     /* Debugging */
16     inline void print ()
17     {
18       unsigned long *lp = (unsigned long *)((void *) &ptr);
19       printf ("%08lx", *lp);
20     }
21 #endif
23     be_ptr () {}
25     // constructor element template: constructs be_ptr from any other be_ptr
26     //   Bug: too few warnings when incompatible be_ptrs are assigned
27     template<class C>
28     explicit be_ptr (const be_ptr<C> &p) : ptr (p.ptr) {}
30     // explicit: this constructor is not used
31     //           for automatic type promotion
32     // reinterpret_cast: required to cast pointer_type to uae_ptr,
33     //                   which is actually a long.
34     explicit be_ptr (const pointer_type p) : ptr (reinterpret_cast<uae_ptr> (p)) {}
36     // reinterpret_cast: required to cast void * to uae_ptr,
37     //                   which is actually a long.
38     be_ptr (void *p) : ptr (reinterpret_cast<uae_ptr> (p)) {}
40     // static_cast returns a native endian uae_ptr (long) and
41     // reinterpret_cast is required to cast it into a void *.
42     inline operator void * ()
43     {
44       return reinterpret_cast<void *> (static_cast<uae_ptr> (ptr));
45     }
47     // same as above for C*
48     //   Bug: too few warnings when incompatible be_ptrs are assigned
49     template<class C>
50     operator C* ()
51     {
52       return reinterpret_cast<C*> (static_cast<uae_ptr> (ptr));
53     }
55     // same as above for const C*
56     //   Bug: too few warnings when incompatible be_ptrs are assigned
57     template<class C>
58     operator const C* () const
59     {
60       return reinterpret_cast<const C*> (static_cast<uae_ptr> (ptr));
61     }
63     // cast the object itself into a native endian pointer
64     // using the pointer_type method above.
65     value_type& operator* ()
66     {
67       return *(static_cast<pointer_type> (*this));
68     }
70     // same as above but without indirection
71     pointer_type operator-> ()
72     {
73       return static_cast<pointer_type> (*this);
74     }
76     // assigns a native endian pointer (of pointer_type)
77     // to this object
78     const my_type& operator= (const pointer_type p)
79     {
80       *this = my_type (p);
81       return *this;
82     }
84     // assigns another big endian pointer of any type
85     // to this object
86     template<class C>
87     const my_type& operator= (const be_ptr<C>& other)
88     {
89       ptr=other.ptr;
90       return *this;
91     }
92     
93     value_type& operator[] (size_t dist) // index operator
94     {
95       return *(*this + dist);
96     }
98     const my_type& operator++ () // pre increment operator
99     {
100       ptr ++;
101       return *this;
102     }
103     const my_type operator++ (int) // post increment operator
104     {
105       my_type tmp = *this;
106       (*this)++;
107       return tmp;
108     }
109     const my_type& operator-- () // pre increment operator
110     {
111       ptr --;
112       return *this;
113     }
114     const my_type operator-- (int) // post increment operator
115     {
116       my_type tmp = *this;
117       (*this)--;
118       return tmp;
119     }
120     const my_type& operator+= (const size_t dist)
121     {
122       ptr += dist;
123     }
124     const my_type& operator-= (const size_t dist)
125     {
126       ptr -= dist;
127     }
128     my_type operator+ (const size_t dist)
129     {
130       my_type tmp = *this;
131       tmp += dist;
132       return tmp;
133     }
134     my_type operator- (const size_t dist)
135     {
136       my_type tmp = *this;
137       tmp -= dist;
138       return tmp;
139     }
141   protected:
142     be_val<uae_ptr> ptr;
144   // required for the constructor element template
145   template<class C>
146   friend be_ptr<C>;
149 template<> // specialization for void pointer
150 class be_ptr<void>
152   public:
153     typedef be_ptr<void> my_type;
154     typedef void* pointer_type;
155     typedef const void* const_pointer_type;
157 #ifdef DEBUG
158     /* Debugging */
159     inline void print ()
160     {
161       unsigned long *lp = (unsigned long *)((void *) &ptr);
162       printf ("%08lx", *lp);
163     }
164 #endif
166     be_ptr () {}
168     be_ptr (const pointer_type p) : ptr (reinterpret_cast<uae_ptr> (p)) {}
170     operator const_pointer_type () const
171     {
172       return reinterpret_cast<pointer_type> (static_cast<uae_ptr> (ptr));
173     }
174     operator pointer_type ()
175     {
176       return reinterpret_cast<pointer_type> (static_cast<uae_ptr> (ptr));
177     }
178     const my_type& operator= (const pointer_type p)
179     {
180       *this = my_type (p);
181       return *this;
182     }
184   protected:
185     be_val<uae_ptr> ptr;
188 template<class T>
189 inline size_t operator- (const be_ptr<T>& a, const be_ptr<T>& b)
191   return static_cast<T*> (a) - static_cast<T*> (b);