Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2021 Project CHIP Authors
4 : * All rights reserved.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * 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 implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : #pragma once
20 :
21 : #include <app/util/basic-types.h>
22 :
23 : namespace chip {
24 : namespace app {
25 :
26 : /**
27 : * A representation of a concrete cluster path. This identifies a specific
28 : * cluster instance.
29 : */
30 : struct ConcreteClusterPath
31 : {
32 81579 : ConcreteClusterPath(EndpointId aEndpointId, ClusterId aClusterId) : mEndpointId(aEndpointId), mClusterId(aClusterId) {}
33 35655 : ConcreteClusterPath() = default;
34 :
35 : ConcreteClusterPath(const ConcreteClusterPath & aOther) = default;
36 : ConcreteClusterPath & operator=(const ConcreteClusterPath & aOther) = default;
37 :
38 217 : bool IsValidConcreteClusterPath() const { return !(mEndpointId == kInvalidEndpointId || mClusterId == kInvalidClusterId); }
39 :
40 6297 : bool HasValidIds() const { return IsValidEndpointId(mEndpointId) && IsValidClusterId(mClusterId); }
41 :
42 1767546 : bool operator==(const ConcreteClusterPath & aOther) const
43 : {
44 1767546 : return mEndpointId == aOther.mEndpointId && mClusterId == aOther.mClusterId;
45 : }
46 :
47 14574 : bool operator!=(const ConcreteClusterPath & aOther) const { return !(*this == aOther); }
48 :
49 : EndpointId mEndpointId = 0;
50 : // Note: not all subclasses of ConcreteClusterPath need mExpanded, but due
51 : // to alignment requirements it's "free" in the sense of not needing more
52 : // memory to put it here. But we don't initialize it, because that
53 : // increases codesize for the non-consumers.
54 : bool mExpanded; // NOTE: in between larger members, NOT initialized (see above)
55 : ClusterId mClusterId = 0;
56 :
57 : /// Creates a concrete cluster path as a constexpr. This is a workaround for C++ before C++20 requiring all members
58 : /// to be initialized for constexpr constructors.
59 404 : constexpr static ConcreteClusterPath ConstExpr(EndpointId aEndpointId, ClusterId aClusterId)
60 : {
61 404 : return { aEndpointId, aClusterId, false };
62 : }
63 :
64 : private:
65 : // Setting this as private since as of C++20 we would be allowed to have constructors
66 : // that do not initialize all members. Until then though, we require one that
67 : // initializes everything and this is that constructor. Readibility of a `bool` is poor though
68 : // so we route this throgh the `ConstExpr()` factory instead for readability and eventual
69 : // removal.
70 404 : constexpr ConcreteClusterPath(EndpointId aEndpointId, ClusterId aClusterId, bool expanded) :
71 404 : mEndpointId(aEndpointId), mExpanded(expanded), mClusterId(aClusterId)
72 404 : {}
73 : };
74 :
75 : } // namespace app
76 : } // namespace chip
|