[dali_1.0.9] Merge branch 'tizen'
[platform/core/uifw/dali-core.git] / dali / public-api / common / dali-common.h
1 #ifndef __DALI_COMMON_H__
2 #define __DALI_COMMON_H__
3
4 /*
5  * Copyright (c) 2014 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  */
20
21 // EXTERNAL INCLUDES
22 #include <string>
23 #include <cstdio>
24
25 #ifdef EMSCRIPTEN
26 #include <emscripten/emscripten.h>
27 #endif
28 /*
29  * Definitions for shared library support
30  *
31  * If a library is configured with --enable-exportall or --enable-debug
32  * then HIDE_DALI_INTERNALS is not defined, and nothing is hidden.
33  * If it is configured without these options (the default), then HIDE_INTERNALS
34  * is defined when building the library, visibility is automatically hidden, and the explicit
35  * defines below come into use.
36  * When building a library that uses DALI, HIDE_DALI_INTERNALS
37  */
38 #if __GNUC__ >= 4
39 #  ifndef HIDE_DALI_INTERNALS
40 #    define DALI_EXPORT_API
41 #    define DALI_IMPORT_API
42 #    define DALI_INTERNAL
43 #  else
44 #    define DALI_EXPORT_API __attribute__ ((visibility ("default")))
45 #    define DALI_IMPORT_API __attribute__ ((visibility ("default")))
46 #    define DALI_INTERNAL   __attribute__ ((visibility ("hidden")))
47 #  endif
48 #else
49 /** Visibility attribute to show method definitions */
50 #  define DALI_EXPORT_API
51 /** Visibility attribute to show declarations */
52 #  define DALI_IMPORT_API
53 /** Visibility attribute to hide declarations */
54 #  define DALI_INTERNAL
55 #endif
56
57 #if defined (__GXX_EXPERIMENTAL_CXX0X__) || (__cplusplus >= 201103L)
58 // C++0x support
59 #define _CPP11
60 #else
61 // C++0x not supported
62 #endif
63
64 #ifdef EMSCRIPTEN
65
66 #ifndef __clang__
67 # error not clang?
68 #endif
69
70 // clang cpp11 check is per feature
71 #if !__has_feature(cxx_constexpr)
72 # error constexpr needed for compile-time-math. Use -std=c+11
73 #endif
74
75 #define _CPP11
76
77 #endif
78
79 /**
80  * @brief The DALi namespace
81  */
82 namespace Dali
83 {
84
85 /**
86  * @brief Method to log assertion message in DALI_ASSERT_ALWAYS macro below.
87  *
88  * @param[in] condition The assertion condition
89  * @param[in] file The file in which the assertion occurred
90  * @param[in] line The line number at which the assertion occured
91  */
92 DALI_IMPORT_API void DaliAssertMessage(const char* condition, const char* file, int line);
93
94 /**
95  * @brief Exception class for Dali Core library - Raised by assertions in codebase.
96  */
97 class DALI_IMPORT_API DaliException
98 {
99 public:
100   /**
101    * @brief Constructor.
102    *
103    * Will always display a backtrace when raised in a debug build.
104    *
105    * @param[in] location  - the location of the assertion
106    * @param[in] condition - The assertion condition
107    */
108   DALI_IMPORT_API DaliException(const char *location, const char* condition);
109
110   std::string mLocation;  ///< Location in code of the assertion
111   std::string mCondition; ///< The assertion string
112 };
113
114 }// Dali
115
116 /**
117  * @brief An invariant concurrent assertion to ensure its argument always evaluates TRUE.
118  *
119  * Use this for rules that must always be true regardless of build options.
120  * For example, Actor must only ever have one parent.
121  * To be clear, this test remains compiled into release builds that are deployed
122  * on the platform.
123  * Semantically, a failure of this test is signalling that dali is giving up and
124  * quitting.
125  * Since we don't catch the exception, a failure on any thread other than event
126  * will propagate up the call stack and kill that thread.
127  * A failure on the event thread may give the application an opportunity to
128  * recover if there is an application-written exception handler on the call
129  * stack between the throw site and the thread root and the application is built
130  * with a compatible ABI.
131  * Handle this macro with care at the level you would if it expanded to:
132  *    if(!cond) { exit(EXIT_FAILURE); }
133  * (which it is often equivalent to in effect).
134  * It should not be used for simple parameter validation for instance.
135  */
136 #ifdef EMSCRIPTEN
137
138 #define DALI_ASSERT_ALWAYS(cond)                \
139   if(!(cond)) \
140   { \
141     Dali::DaliAssertMessage(#cond, __FILE__, __LINE__);   \
142     throw Dali::DaliException(__PRETTY_FUNCTION__, #cond);  \
143     EM_ASM(print(new Error().stack)); \
144   }\
145
146 #else
147
148 #define DALI_ASSERT_ALWAYS(cond)                \
149   if(!(cond)) \
150   { \
151     Dali::DaliAssertMessage(#cond, __FILE__, __LINE__);   \
152     throw Dali::DaliException(__PRETTY_FUNCTION__, #cond);  \
153   }\
154
155 #endif
156
157 /**
158  * @brief An invariant concurrent assertion to ensure its argument evaluates TRUE in debug builds.
159  *
160  * Use this to sanity check algorithms and prevent internal programming errors
161  */
162 #if defined(DEBUG_ENABLED)
163 #define DALI_ASSERT_DEBUG(cond) DALI_ASSERT_ALWAYS(cond)
164 #else
165 #define DALI_ASSERT_DEBUG(cond)
166 #endif
167
168 #endif // __DALI_COMMON_H__