Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_CANVAS_VERIFYINPUT_HXX
21 : #define INCLUDED_CANVAS_VERIFYINPUT_HXX
22 :
23 : #include <com/sun/star/uno/Reference.hxx>
24 : #include <com/sun/star/uno/Sequence.hxx>
25 : #include <com/sun/star/uno/RuntimeException.hpp>
26 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 : #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
28 :
29 : #include <algorithm>
30 : #include <boost/bind.hpp>
31 : #include <boost/current_function.hpp>
32 :
33 : #include <canvas/canvastoolsdllapi.h>
34 :
35 : namespace com { namespace sun { namespace star { namespace geometry
36 : {
37 : struct RealPoint2D;
38 : struct RealSize2D;
39 : struct RealBezierSegment2D;
40 : struct RealRectangle2D;
41 : struct AffineMatrix2D;
42 : struct Matrix2D;
43 : struct IntegerPoint2D;
44 : struct IntegerSize2D;
45 : struct IntegerRectangle2D;
46 : } } } }
47 :
48 : namespace com { namespace sun { namespace star { namespace rendering
49 : {
50 : struct RenderState;
51 : struct StrokeAttributes;
52 : struct Texture;
53 : struct ViewState;
54 : struct IntegerBitmapLayout;
55 : struct FontRequest;
56 : struct FontInfo;
57 : class XCanvas;
58 : } } } }
59 :
60 :
61 : namespace canvas
62 : {
63 : namespace tools
64 : {
65 :
66 : // Input checking facilities
67 :
68 :
69 : // This header provides methods to check all common
70 : // css::rendering::* method input parameters against
71 : // compliance to the API specification.
72 :
73 : /** Verify that the given transformation contains valid floating point
74 : values.
75 :
76 : @param rMatrix
77 : Matrix to check
78 :
79 : @param xIf
80 : The interface that should be reported as the one
81 : generating the exception.
82 :
83 : @param nArgPos
84 : Argument position on the call site (i.e. the position of
85 : the argument, checked here, on the UNO interface
86 : method. Counting starts at 0).
87 :
88 : @throws an lang::IllegalArgumentException, if anything is wrong
89 : */
90 : CANVASTOOLS_DLLPUBLIC void verifyInput( const ::com::sun::star::geometry::AffineMatrix2D& rMatrix,
91 : const char* pStr,
92 : const ::com::sun::star::uno::Reference<
93 : ::com::sun::star::uno::XInterface >& xIf,
94 : ::sal_Int16 nArgPos );
95 :
96 : /** Verify that the given transformation contains valid floating point
97 : values.
98 :
99 : @param rMatrix
100 : Matrix to check
101 :
102 : @param xIf
103 : The interface that should be reported as the one
104 : generating the exception.
105 :
106 : @param nArgPos
107 : Argument position on the call site (i.e. the position of
108 : the argument, checked here, on the UNO interface
109 : method. Counting starts at 0).
110 :
111 : @throws an lang::IllegalArgumentException, if anything is wrong
112 : */
113 : CANVASTOOLS_DLLPUBLIC void verifyInput( const ::com::sun::star::geometry::Matrix2D& rMatrix,
114 : const char* pStr,
115 : const ::com::sun::star::uno::Reference<
116 : ::com::sun::star::uno::XInterface >& xIf,
117 : ::sal_Int16 nArgPos );
118 :
119 : /** Verify that the given point contains valid floating point
120 : values.
121 :
122 : @param rPoint
123 : Point to check
124 :
125 : @param xIf
126 : The interface that should be reported as the one
127 : generating the exception.
128 :
129 : @param nArgPos
130 : Argument position on the call site (i.e. the position of
131 : the argument, checked here, on the UNO interface
132 : method. Counting starts at 0).
133 :
134 : @throws an lang::IllegalArgumentException, if anything is wrong
135 : */
136 : CANVASTOOLS_DLLPUBLIC void verifyInput( const ::com::sun::star::geometry::RealPoint2D& rPoint,
137 : const char* pStr,
138 : const ::com::sun::star::uno::Reference<
139 : ::com::sun::star::uno::XInterface >& xIf,
140 : ::sal_Int16 nArgPos );
141 :
142 : /** Verify that the given bezier segment contains valid
143 : floating point values.
144 :
145 : @param rSegment
146 : Segment to check
147 :
148 : @param xIf
149 : The interface that should be reported as the one
150 : generating the exception.
151 :
152 : @param nArgPos
153 : Argument position on the call site (i.e. the position of
154 : the argument, checked here, on the UNO interface
155 : method. Counting starts at 0).
156 :
157 : @throws an lang::IllegalArgumentException, if anything is wrong
158 : */
159 : CANVASTOOLS_DLLPUBLIC void verifyInput( const ::com::sun::star::geometry::RealBezierSegment2D& rSegment,
160 : const char* pStr,
161 : const ::com::sun::star::uno::Reference<
162 : ::com::sun::star::uno::XInterface >& xIf,
163 : ::sal_Int16 nArgPos );
164 :
165 : /** Verify that the given rectangle contains valid floating
166 : point values.
167 :
168 : @param rRect
169 : Rect to check
170 :
171 : @param xIf
172 : The interface that should be reported as the one
173 : generating the exception.
174 :
175 : @param nArgPos
176 : Argument position on the call site (i.e. the position of
177 : the argument, checked here, on the UNO interface
178 : method. Counting starts at 0).
179 :
180 : @throws an lang::IllegalArgumentException, if anything is wrong
181 : */
182 : CANVASTOOLS_DLLPUBLIC void verifyInput( const ::com::sun::star::geometry::RealRectangle2D& rRect,
183 : const char* pStr,
184 : const ::com::sun::star::uno::Reference<
185 : ::com::sun::star::uno::XInterface >& xIf,
186 : ::sal_Int16 nArgPos );
187 :
188 : /** Basic check for view state validity.
189 :
190 : @param viewState
191 : Viewstate to check
192 :
193 : @param xIf
194 : The interface that should be reported as the one
195 : generating the exception.
196 :
197 : @param nArgPos
198 : Argument position on the call site (i.e. the position of
199 : the argument, checked here, on the UNO interface
200 : method. Counting starts at 0).
201 :
202 : @throws an lang::IllegalArgumentException, if anything is wrong
203 : */
204 : CANVASTOOLS_DLLPUBLIC void verifyInput( const ::com::sun::star::rendering::ViewState& viewState,
205 : const char* pStr,
206 : const ::com::sun::star::uno::Reference<
207 : ::com::sun::star::uno::XInterface >& xIf,
208 : ::sal_Int16 nArgPos );
209 :
210 : /** Basic check for render state validity.
211 :
212 : @param renderState
213 : Renderstate to check
214 :
215 : @param xIf
216 : The interface that should be reported as the one
217 : generating the exception.
218 :
219 : @param nArgPos
220 : Argument position on the call site (i.e. the position of
221 : the argument, checked here, on the UNO interface
222 : method. Counting starts at 0).
223 :
224 : @param nMinColorComponents
225 : Minimal number of color components available in
226 : RenderState::DeviceColor
227 :
228 : @throws an lang::IllegalArgumentException, if anything is wrong
229 : */
230 : CANVASTOOLS_DLLPUBLIC void verifyInput( const ::com::sun::star::rendering::RenderState& renderState,
231 : const char* pStr,
232 : const ::com::sun::star::uno::Reference<
233 : ::com::sun::star::uno::XInterface >& xIf,
234 : ::sal_Int16 nArgPos,
235 : sal_Int32 nMinColorComponents=0 );
236 :
237 : /** Basic check for stroke attributes validity.
238 :
239 : @param strokeAttributes
240 : Attributes to check
241 :
242 : @param xIf
243 : The interface that should be reported as the one
244 : generating the exception.
245 :
246 : @param nArgPos
247 : Argument position on the call site (i.e. the position of
248 : the argument, checked here, on the UNO interface
249 : method. Counting starts at 0).
250 :
251 : @throws an lang::IllegalArgumentException, if anything is wrong
252 : */
253 : CANVASTOOLS_DLLPUBLIC void verifyInput( const ::com::sun::star::rendering::StrokeAttributes& strokeAttributes,
254 : const char* pStr,
255 : const ::com::sun::star::uno::Reference<
256 : ::com::sun::star::uno::XInterface >& xIf,
257 : ::sal_Int16 nArgPos );
258 :
259 : /** Basic check for texture validity.
260 :
261 : @param texture
262 : Texture to check
263 :
264 : @param xIf
265 : The interface that should be reported as the one
266 : generating the exception.
267 :
268 : @param nArgPos
269 : Argument position on the call site (i.e. the position of
270 : the argument, checked here, on the UNO interface
271 : method. Counting starts at 0).
272 :
273 : @throws an lang::IllegalArgumentException, if anything is wrong
274 : */
275 : CANVASTOOLS_DLLPUBLIC void verifyInput( const ::com::sun::star::rendering::Texture& texture,
276 : const char* pStr,
277 : const ::com::sun::star::uno::Reference<
278 : ::com::sun::star::uno::XInterface >& xIf,
279 : ::sal_Int16 nArgPos );
280 :
281 : /** Basic check for bitmap layout validity.
282 :
283 : @param bitmapLayout
284 : Bitmap layout to check
285 :
286 : @param xIf
287 : The interface that should be reported as the one
288 : generating the exception.
289 :
290 : @param nArgPos
291 : Argument position on the call site (i.e. the position of
292 : the argument, checked here, on the UNO interface
293 : method. Counting starts at 0).
294 :
295 : @throws an lang::IllegalArgumentException, if anything is wrong
296 : */
297 : CANVASTOOLS_DLLPUBLIC void verifyInput( const ::com::sun::star::rendering::IntegerBitmapLayout& bitmapLayout,
298 : const char* pStr,
299 : const ::com::sun::star::uno::Reference<
300 : ::com::sun::star::uno::XInterface >& xIf,
301 : ::sal_Int16 nArgPos );
302 :
303 : /** Basic check for font info validity.
304 :
305 : @param fontInfo
306 : Font info to check
307 :
308 : @param xIf
309 : The interface that should be reported as the one
310 : generating the exception.
311 :
312 : @param nArgPos
313 : Argument position on the call site (i.e. the position of
314 : the argument, checked here, on the UNO interface
315 : method. Counting starts at 0).
316 :
317 : @throws an lang::IllegalArgumentException, if anything is wrong
318 : */
319 : CANVASTOOLS_DLLPUBLIC void verifyInput( const ::com::sun::star::rendering::FontInfo& fontInfo,
320 : const char* pStr,
321 : const ::com::sun::star::uno::Reference<
322 : ::com::sun::star::uno::XInterface >& xIf,
323 : ::sal_Int16 nArgPos );
324 :
325 : /** Basic check for font request validity.
326 :
327 : @param fontRequest
328 : Font request to check
329 :
330 : @param xIf
331 : The interface that should be reported as the one
332 : generating the exception.
333 :
334 : @param nArgPos
335 : Argument position on the call site (i.e. the position of
336 : the argument, checked here, on the UNO interface
337 : method. Counting starts at 0).
338 :
339 : @throws an lang::IllegalArgumentException, if anything is wrong
340 : */
341 : CANVASTOOLS_DLLPUBLIC void verifyInput( const ::com::sun::star::rendering::FontRequest& fontRequest,
342 : const char* pStr,
343 : const ::com::sun::star::uno::Reference<
344 : ::com::sun::star::uno::XInterface >& xIf,
345 : ::sal_Int16 nArgPos );
346 :
347 : /** Templatized check for uno::Reference validity.
348 :
349 : @param rRef
350 : Reference to check against non-NILness
351 :
352 : @param xIf
353 : The interface that should be reported as the one
354 : generating the exception.
355 :
356 : @param nArgPos
357 : Argument position on the call site (i.e. the position of
358 : the argument, checked here, on the UNO interface
359 : method. Counting starts at 0).
360 :
361 : @throws an lang::IllegalArgumentException, if anything is wrong
362 : */
363 0 : template< class Interface > void verifyInput(
364 : const ::com::sun::star::uno::Reference< Interface >& rRef,
365 : const char* pStr,
366 : const ::com::sun::star::uno::Reference<
367 : ::com::sun::star::uno::XInterface >& xIf,
368 : ::sal_Int16 nArgPos )
369 : {
370 : (void)pStr; (void)xIf; (void)nArgPos;
371 :
372 0 : if( !rRef.is() )
373 : {
374 : #if OSL_DEBUG_LEVEL > 0
375 : throw ::com::sun::star::lang::IllegalArgumentException(
376 : OUString::createFromAscii(pStr) + ": reference is NULL",
377 : xIf,
378 : nArgPos );
379 : #else
380 0 : throw ::com::sun::star::lang::IllegalArgumentException();
381 : #endif
382 : }
383 0 : }
384 :
385 : /** Templatized check for content-of-sequence validity.
386 :
387 : @param rSequence
388 : Sequence of things to check
389 :
390 : @param xIf
391 : The interface that should be reported as the one
392 : generating the exception.
393 :
394 : @param nArgPos
395 : Argument position on the call site (i.e. the position of
396 : the argument, checked here, on the UNO interface
397 : method. Counting starts at 0).
398 :
399 : @throws an lang::IllegalArgumentException, if anything is wrong
400 : */
401 0 : template< typename SequenceContent > void verifyInput(
402 : const ::com::sun::star::uno::Sequence< SequenceContent >& rSequence,
403 : const char* pStr,
404 : const ::com::sun::star::uno::Reference<
405 : ::com::sun::star::uno::XInterface >& xIf,
406 : ::sal_Int16 nArgPos )
407 : {
408 0 : const SequenceContent* pCurr = rSequence.getConstArray();
409 0 : const SequenceContent* pEnd = pCurr + rSequence.getLength();
410 0 : while( pCurr != pEnd )
411 0 : verifyInput( *pCurr++, pStr, xIf, nArgPos );
412 0 : }
413 :
414 : /// Catch-all, to handle cases that DON'T need input checking (i.e. the Integer geometry ones)
415 0 : template< typename T > void verifyInput( const T& /*rDummy*/,
416 : const char* /*pStr*/,
417 : const ::com::sun::star::uno::Reference<
418 : ::com::sun::star::uno::XInterface >& /*xIf*/,
419 : ::sal_Int16 /*nArgPos*/ )
420 : {
421 0 : }
422 :
423 : // TODO(Q2): Employ some template arglist magic here, to avoid
424 : // this duplication of code...
425 :
426 0 : template< typename Arg0 > void verifyArgs( const Arg0& rArg0,
427 : const char* pStr,
428 : const ::com::sun::star::uno::Reference<
429 : ::com::sun::star::uno::XInterface >& xIf )
430 : {
431 0 : verifyInput( rArg0, pStr, xIf, 0 );
432 0 : }
433 :
434 : template< typename Arg0,
435 0 : typename Arg1 > void verifyArgs( const Arg0& rArg0,
436 : const Arg1& rArg1,
437 : const char* pStr,
438 : const ::com::sun::star::uno::Reference<
439 : ::com::sun::star::uno::XInterface >& xIf )
440 : {
441 0 : verifyInput( rArg0, pStr, xIf, 0 );
442 0 : verifyInput( rArg1, pStr, xIf, 1 );
443 0 : }
444 :
445 : template< typename Arg0,
446 : typename Arg1,
447 0 : typename Arg2 > void verifyArgs( const Arg0& rArg0,
448 : const Arg1& rArg1,
449 : const Arg2& rArg2,
450 : const char* pStr,
451 : const ::com::sun::star::uno::Reference<
452 : ::com::sun::star::uno::XInterface >& xIf )
453 : {
454 0 : verifyInput( rArg0, pStr, xIf, 0 );
455 0 : verifyInput( rArg1, pStr, xIf, 1 );
456 0 : verifyInput( rArg2, pStr, xIf, 2 );
457 0 : }
458 :
459 : template< typename Arg0,
460 : typename Arg1,
461 : typename Arg2,
462 0 : typename Arg3 > void verifyArgs( const Arg0& rArg0,
463 : const Arg1& rArg1,
464 : const Arg2& rArg2,
465 : const Arg3& rArg3,
466 : const char* pStr,
467 : const ::com::sun::star::uno::Reference<
468 : ::com::sun::star::uno::XInterface >& xIf )
469 : {
470 0 : verifyInput( rArg0, pStr, xIf, 0 );
471 0 : verifyInput( rArg1, pStr, xIf, 1 );
472 0 : verifyInput( rArg2, pStr, xIf, 2 );
473 0 : verifyInput( rArg3, pStr, xIf, 3 );
474 0 : }
475 :
476 : template< typename Arg0,
477 : typename Arg1,
478 : typename Arg2,
479 : typename Arg3,
480 0 : typename Arg4 > void verifyArgs( const Arg0& rArg0,
481 : const Arg1& rArg1,
482 : const Arg2& rArg2,
483 : const Arg3& rArg3,
484 : const Arg4& rArg4,
485 : const char* pStr,
486 : const ::com::sun::star::uno::Reference<
487 : ::com::sun::star::uno::XInterface >& xIf )
488 : {
489 0 : verifyInput( rArg0, pStr, xIf, 0 );
490 0 : verifyInput( rArg1, pStr, xIf, 1 );
491 0 : verifyInput( rArg2, pStr, xIf, 2 );
492 0 : verifyInput( rArg3, pStr, xIf, 3 );
493 0 : verifyInput( rArg4, pStr, xIf, 4 );
494 0 : }
495 :
496 : template< typename Arg0,
497 : typename Arg1,
498 : typename Arg2,
499 : typename Arg3,
500 : typename Arg4,
501 0 : typename Arg5 > void verifyArgs( const Arg0& rArg0,
502 : const Arg1& rArg1,
503 : const Arg2& rArg2,
504 : const Arg3& rArg3,
505 : const Arg4& rArg4,
506 : const Arg5& rArg5,
507 : const char* pStr,
508 : const ::com::sun::star::uno::Reference<
509 : ::com::sun::star::uno::XInterface >& xIf )
510 : {
511 0 : verifyInput( rArg0, pStr, xIf, 0 );
512 0 : verifyInput( rArg1, pStr, xIf, 1 );
513 0 : verifyInput( rArg2, pStr, xIf, 2 );
514 0 : verifyInput( rArg3, pStr, xIf, 3 );
515 0 : verifyInput( rArg4, pStr, xIf, 4 );
516 0 : verifyInput( rArg5, pStr, xIf, 5 );
517 0 : }
518 :
519 : template< typename Arg0,
520 : typename Arg1,
521 : typename Arg2,
522 : typename Arg3,
523 : typename Arg4,
524 : typename Arg5,
525 0 : typename Arg6 > void verifyArgs( const Arg0& rArg0,
526 : const Arg1& rArg1,
527 : const Arg2& rArg2,
528 : const Arg3& rArg3,
529 : const Arg4& rArg4,
530 : const Arg5& rArg5,
531 : const Arg6& rArg6,
532 : const char* pStr,
533 : const ::com::sun::star::uno::Reference<
534 : ::com::sun::star::uno::XInterface >& xIf )
535 : {
536 0 : verifyInput( rArg0, pStr, xIf, 0 );
537 0 : verifyInput( rArg1, pStr, xIf, 1 );
538 0 : verifyInput( rArg2, pStr, xIf, 2 );
539 0 : verifyInput( rArg3, pStr, xIf, 3 );
540 0 : verifyInput( rArg4, pStr, xIf, 4 );
541 0 : verifyInput( rArg5, pStr, xIf, 5 );
542 0 : verifyInput( rArg6, pStr, xIf, 6 );
543 0 : }
544 :
545 :
546 : /** Range checker, which throws ::com::sun::star::lang::IllegalArgument exception, when
547 : range is violated
548 : */
549 0 : template< typename NumType > inline void verifyRange( NumType arg, NumType lowerBound, NumType upperBound )
550 : {
551 0 : if( arg < lowerBound ||
552 : arg > upperBound )
553 : {
554 0 : throw ::com::sun::star::lang::IllegalArgumentException();
555 : }
556 0 : }
557 :
558 : /** Range checker, which throws ::com::sun::star::lang::IllegalArgument exception, when
559 : range is violated
560 :
561 : The checked range is half open, i.e. only bound by the specified value.
562 :
563 : @param arg
564 : Arg to check
565 :
566 : @param bound
567 : Bound to check against
568 :
569 : @param bLowerBound
570 : When true, given bound is the lower bound. When false,
571 : given bound is the upper bound.
572 : */
573 0 : template< typename NumType > inline void verifyRange( NumType arg, NumType bound, bool bLowerBound=true )
574 : {
575 0 : if( (bLowerBound && arg < bound) ||
576 0 : (!bLowerBound && arg > bound) )
577 : {
578 0 : throw ::com::sun::star::lang::IllegalArgumentException();
579 : }
580 0 : }
581 :
582 : /** Range checker, which throws ::com::sun::star::lang::IndexOutOfBounds exception, when
583 : index range is violated
584 : */
585 : template< typename NumType > inline void verifyIndexRange( NumType arg, NumType lowerBound, NumType upperBound )
586 : {
587 : if( arg < lowerBound ||
588 : arg > upperBound )
589 : {
590 : throw ::com::sun::star::lang::IndexOutOfBoundsException();
591 : }
592 : }
593 :
594 : /** Range checker, which throws ::com::sun::star::lang::IndexOutOfBounds exception, when
595 : index range is violated
596 :
597 : @param rect
598 : Rect to verify
599 :
600 : @param size
601 : Given rectangle must be within ((0,0), (size.Width, size.Height))
602 : */
603 : CANVASTOOLS_DLLPUBLIC void verifyIndexRange( const ::com::sun::star::geometry::IntegerRectangle2D& rect,
604 : const ::com::sun::star::geometry::IntegerSize2D& size );
605 :
606 : /** Range checker, which throws ::com::sun::star::lang::IndexOutOfBounds exception, when
607 : index range is violated
608 :
609 : @param pos
610 : Position to verify
611 :
612 : @param size
613 : Given position must be within ((0,0), (size.Width, size.Height))
614 : */
615 : CANVASTOOLS_DLLPUBLIC void verifyIndexRange( const ::com::sun::star::geometry::IntegerPoint2D& pos,
616 : const ::com::sun::star::geometry::IntegerSize2D& size );
617 :
618 : /** Range checker, which throws ::com::sun::star::lang::IndexOutOfBounds exception, when
619 : the size is negative or null
620 :
621 : @param size
622 : Size to verify
623 : */
624 : CANVASTOOLS_DLLPUBLIC void verifyBitmapSize( const ::com::sun::star::geometry::IntegerSize2D& size,
625 : const char* pStr,
626 : const ::com::sun::star::uno::Reference<
627 : ::com::sun::star::uno::XInterface >& xIf );
628 :
629 : /** Range checker, which throws ::com::sun::star::lang::IndexOutOfBounds exception, when
630 : the size is negative or null
631 :
632 : @param size
633 : Size to verify
634 : */
635 : CANVASTOOLS_DLLPUBLIC void verifySpriteSize( const ::com::sun::star::geometry::RealSize2D& size,
636 : const char* pStr,
637 : const ::com::sun::star::uno::Reference<
638 : ::com::sun::star::uno::XInterface >& xIf );
639 : }
640 : }
641 :
642 : #endif /* INCLUDED_CANVAS_VERIFYINPUT_HXX */
643 :
644 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|