File: layer_range.h

package info (click to toggle)
kicad 9.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 769,124 kB
  • sloc: cpp: 960,330; ansic: 121,001; xml: 66,428; python: 18,382; sh: 1,010; awk: 301; asm: 292; makefile: 227; javascript: 167; perl: 10
file content (165 lines) | stat: -rw-r--r-- 5,159 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <layer_ids.h>

#ifndef LAYER_RANGE_H
#define LAYER_RANGE_H


class LAYER_RANGE
{
private:
    PCB_LAYER_ID m_start;
    PCB_LAYER_ID m_stop;
    int  m_layer_count;

    class LAYER_RANGE_ITERATOR
    {
    private:
        int  m_current;
        int  m_stop;
        int  m_layer_count;
        bool m_reverse;

        int next_layer( int aLayer )
        {
            if( m_reverse )
            {
                if( aLayer == B_Cu )
                    aLayer = m_layer_count == 2 ? F_Cu :
                             static_cast<int>( F_Cu ) + 2 * ( m_layer_count - 2 ) + 2;
                else if( aLayer == m_stop || aLayer == UNDEFINED_LAYER )
                    aLayer = UNDEFINED_LAYER;
                else if( aLayer == In1_Cu )
                    aLayer = F_Cu;
                else
                    aLayer = static_cast<int>( aLayer ) - 2;
            }
            else
            {
                if( aLayer == F_Cu && m_layer_count == 2 )
                    aLayer = B_Cu;
                else if( aLayer == m_stop || aLayer == UNDEFINED_LAYER )
                    aLayer = UNDEFINED_LAYER;
                else if( aLayer == static_cast<int>( F_Cu ) + 2 * ( m_layer_count - 2 ) + 2)
                    aLayer = B_Cu;
                else if( aLayer == F_Cu )
                    aLayer = In1_Cu;
                else
                    aLayer = static_cast<int>( aLayer ) + 2;
            }

            return aLayer;
        }

    public:
        using iterator_category = std::bidirectional_iterator_tag;
        using value_type = PCB_LAYER_ID;
        using difference_type = std::ptrdiff_t;
        using pointer = PCB_LAYER_ID*;
        using reference = PCB_LAYER_ID&;

        LAYER_RANGE_ITERATOR( PCB_LAYER_ID start, PCB_LAYER_ID stop, int layer_count ) :
                m_current( start ), m_stop( stop ), m_layer_count( layer_count )
        {
            if( start & 1 || stop & 1 )
                throw std::invalid_argument( "Only works for copper layers" );

            if( stop == B_Cu || m_stop >= m_current )
                m_reverse = false;
            else
                m_reverse = true;
        }

        PCB_LAYER_ID operator*() const { return static_cast<PCB_LAYER_ID>( m_current ); }

        LAYER_RANGE_ITERATOR& operator++()
        {
            m_current = next_layer( m_current );
            return *this;
        }

        LAYER_RANGE_ITERATOR operator++( int )
        {
            LAYER_RANGE_ITERATOR tmp = *this;
            ++( *this );
            return tmp;
        }

        bool operator==( const LAYER_RANGE_ITERATOR& other ) const
        {
            return m_current == other.m_current;
        }

        bool operator!=( const LAYER_RANGE_ITERATOR& other ) const { return !( *this == other ); }
    };

public:
    LAYER_RANGE( PCB_LAYER_ID start, PCB_LAYER_ID stop, int layer_count ) :
            m_start( start ), m_stop( stop ), m_layer_count( layer_count )
    {
            if( start & 1 || stop & 1 )
                throw std::invalid_argument( "Only works for copper layers" );
    }

    LAYER_RANGE_ITERATOR begin() const { return LAYER_RANGE_ITERATOR( m_start, m_stop,
                                                                      m_layer_count ); }

    LAYER_RANGE_ITERATOR end() const
    {
        auto it = LAYER_RANGE_ITERATOR( m_stop, m_stop, m_layer_count );
        return ++it;
    }

    static bool Contains( int aStart_layer, int aEnd_layer, int aTest_layer )
    {
        // B_Cu is the lowest copper layer for Z order copper layers
        // F_cu = top, B_Cu = bottom
        // So set the distance from top for B_Cu to INT_MAX
        if( aTest_layer == B_Cu )
            aTest_layer = INT_MAX;

        if( aStart_layer == B_Cu )
            aStart_layer = INT_MAX;

        if( aEnd_layer == B_Cu )
            aEnd_layer = INT_MAX;

        if( aStart_layer > aEnd_layer )
            std::swap( aStart_layer, aEnd_layer );

        return aTest_layer >= aStart_layer && aTest_layer <= aEnd_layer;
    }

    bool Contains( int aTest_layer )
    {
        return Contains( m_start, m_stop, aTest_layer );
    }

    size_t size() const
    {
        if( m_start == B_Cu )
            return m_layer_count;
        else
            return ( m_stop - m_start ) / 2 + 1;
    }
};

#endif // LAYER_RANGE_H