SystemC 3.0.0
Accellera SystemC proof-of-concept library
sc_ptr_flag.h
Go to the documentation of this file.
1/*****************************************************************************
2
3 Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4 more contributor license agreements. See the NOTICE file distributed
5 with this work for additional information regarding copyright ownership.
6 Accellera licenses this file to you under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with the
8 License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15 implied. See the License for the specific language governing
16 permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22 sc_ptr_flag.h - Helper to store a flag with a pointer
23
24 Original Author: Philipp A. Hartmann, Intel
25
26 *****************************************************************************/
27
28#ifndef SC_PTR_FLAG_H_INCLUDED_
29#define SC_PTR_FLAG_H_INCLUDED_
30
31#include <cstdint>
32
34
35namespace sc_core {
36
37template<typename T>
39{
40 typedef std::uintptr_t uintptr_type;
41 static_assert( alignof(T) > 1
42 , "Unsupported platform/type, need spare LSB of pointer to store flag" );
43public:
44 typedef T* pointer;
45 typedef T& reference;
46
48 : m_data() {}
49
50 sc_ptr_flag(pointer p, bool f = false)
51 : m_data(reinterpret_cast<uintptr_type>(p))
52 {
53 set_flag(f);
54 }
55
57 { reset(p); return *this; }
58
59 operator pointer() const { return get(); }
60 pointer operator->() const { return get(); }
61 reference operator*() const { return *get(); }
62
63 pointer get() const
64 { return reinterpret_cast<pointer>( m_data & pointer_mask ); }
65
66 void reset(pointer p)
67 {
68 m_data = reinterpret_cast<uintptr_type>(p) | static_cast<uintptr_type>(get_flag());
69 }
70
71 bool get_flag() const
72 { return ( m_data & flag_mask ) != 0x0; }
73
74 void set_flag(bool f)
75 { m_data = ( m_data & pointer_mask ) | static_cast<uintptr_type>(f); }
76
77private:
78 static const uintptr_type flag_mask = 0x1;
79 static const uintptr_type pointer_mask = ~flag_mask;
80 uintptr_type m_data;
81
82}; // struct sc_ptr_flag
83
84} // namespace sc_core
85#endif // SC_PTR_FLAG_H_INCLUDED_
void set_flag(bool f)
Definition: sc_ptr_flag.h:74
pointer operator->() const
Definition: sc_ptr_flag.h:60
sc_ptr_flag & operator=(pointer p)
Definition: sc_ptr_flag.h:56
sc_ptr_flag(pointer p, bool f=false)
Definition: sc_ptr_flag.h:50
reference operator*() const
Definition: sc_ptr_flag.h:61
bool get_flag() const
Definition: sc_ptr_flag.h:71
pointer get() const
Definition: sc_ptr_flag.h:63
void reset(pointer p)
Definition: sc_ptr_flag.h:66