Removed dangerous implicit float conversion from LayoutLength and removed some of...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / layouting / layout-length.h
1 #ifndef DALI_TOOLKIT_DEVEL_LAYOUT_LENGTH_H
2 #define DALI_TOOLKIT_DEVEL_LAYOUT_LENGTH_H
3
4 /*
5  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 #include <cstdint>
20 #include <iostream>
21
22 namespace Dali
23 {
24 namespace Toolkit
25 {
26
27 /**
28  * @brief A type that represents a layout length.
29  *
30  * Currently, this implies pixels, but could be extended to handle device dependant sizes, etc.
31  */
32 class LayoutLength
33 {
34 public:
35   using IntType = int;
36
37   LayoutLength()
38   : mValue( 0 )
39   {
40   }
41
42   LayoutLength( IntType value )
43   : mValue( value )
44   {
45   }
46
47   LayoutLength( const LayoutLength& layoutLength )
48   : mValue( layoutLength.mValue )
49   {
50   }
51
52   LayoutLength& operator=(const LayoutLength& rhs)
53   {
54     if( this != &rhs )
55     {
56       mValue = rhs.mValue;
57     }
58     return *this;
59   }
60
61   bool operator==( const LayoutLength& rhs )
62   {
63     return mValue == rhs.mValue;
64   }
65
66   bool operator==( LayoutLength::IntType rhs )
67   {
68     return mValue == rhs;
69   }
70
71   bool operator!=( const LayoutLength& rhs )
72   {
73     return !operator==(rhs);
74   }
75
76   bool operator<( const LayoutLength& rhs )
77   {
78     return mValue < rhs.mValue;
79   }
80
81   bool operator<( const LayoutLength rhs ) const
82   {
83     return mValue < rhs.mValue;
84   }
85
86   bool operator<=( const LayoutLength& rhs )
87   {
88     return mValue <= rhs.mValue;
89   }
90   bool operator<=( LayoutLength rhs )
91   {
92     return mValue <= rhs.mValue;
93   }
94   bool operator>( const LayoutLength& rhs )
95   {
96     return mValue > rhs.mValue;
97   }
98   bool operator>( LayoutLength rhs )
99   {
100     return mValue > rhs.mValue;
101   }
102   bool operator>=( const LayoutLength& rhs )
103   {
104     return mValue >= rhs.mValue;
105   }
106   bool operator>=( LayoutLength rhs )
107   {
108     return mValue >= rhs.mValue;
109   }
110
111   LayoutLength operator+( const LayoutLength& rhs )
112   {
113     return mValue + rhs.mValue;
114   }
115
116   LayoutLength operator+( LayoutLength::IntType rhs )
117   {
118     return mValue + rhs;
119   }
120
121   LayoutLength operator-( const LayoutLength& rhs )
122   {
123     return mValue - rhs.mValue;
124   }
125   LayoutLength operator-( LayoutLength::IntType rhs )
126   {
127     return mValue - rhs;
128   }
129
130   LayoutLength& operator+=( const LayoutLength& rhs )
131   {
132     mValue += rhs.mValue;
133     return *this;
134   }
135   LayoutLength& operator+=( LayoutLength::IntType rhs )
136   {
137     mValue += rhs;
138     return *this;
139   }
140
141   LayoutLength& operator-=( const LayoutLength& rhs )
142   {
143     mValue -= rhs.mValue;
144     return *this;
145   }
146
147   LayoutLength& operator-=( LayoutLength::IntType rhs )
148   {
149     mValue -= rhs;
150     return *this;
151   }
152
153   LayoutLength operator/( const LayoutLength& rhs )
154   {
155     return mValue / rhs.mValue;
156   }
157   LayoutLength operator/(  LayoutLength::IntType rhs )
158   {
159     return mValue / rhs;
160   }
161
162   LayoutLength operator*( const LayoutLength& rhs )
163   {
164     return mValue * rhs.mValue;
165   }
166   LayoutLength operator*( LayoutLength::IntType rhs )
167   {
168     return mValue * rhs;
169   }
170   LayoutLength operator*( float rhs )
171   {
172     return LayoutLength(LayoutLength::IntType(AsFloat() * rhs));
173   }
174
175   /**
176    * @brief explicit method to return the value as float
177    * @return the LayoutLength as float
178    */
179   float AsFloat() const
180   {
181     return static_cast<float>( mValue );
182   }
183
184   IntType mValue;
185 };
186
187 /**
188  * @brief Prints a LayoutLength
189  *
190  * @param[in] o The output stream operator
191  * @param[in] layoutLength the layout length to print
192  * @return The output stream operator
193  */
194 inline std::ostream& operator<<( std::ostream& o, const LayoutLength& layoutLength )
195 {
196   return o<<layoutLength.mValue;
197 }
198
199 } // namespace Toolkit
200
201 } // namespace Dali
202
203 #endif //DALI_TOOLKIT_DEVEL_LAYOUT_LENGTH_H