e21c826804b4c74d9c4be2b593929ae0b712c5bf
[platform/core/uifw/dali-core.git] / dali / public-api / actors / custom-actor.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/public-api/actors/custom-actor.h>
20
21 #include <dali/public-api/object/type-registry.h>
22
23 // INTERNAL INCLUDES
24 #include <dali/internal/event/actors/custom-actor-internal.h>
25
26 namespace Dali
27 {
28
29 namespace
30 {
31
32 using namespace Dali;
33
34 BaseHandle Create()
35 {
36   // not directly creatable
37   return BaseHandle();
38 }
39
40 TypeRegistration mType( typeid(Dali::CustomActor), typeid(Dali::Actor), Create );
41
42 }
43
44 CustomActor::CustomActor()
45 {
46 }
47
48 CustomActor CustomActor::DownCast( BaseHandle handle )
49 {
50   return CustomActor( dynamic_cast<Dali::Internal::CustomActor*>(handle.GetObjectPtr()) );
51 }
52
53 CustomActor::~CustomActor()
54 {
55 }
56
57 CustomActorImpl& CustomActor::GetImplementation()
58 {
59   Internal::CustomActor& internal = GetImpl(*this);
60
61   return internal.GetImplementation();
62 }
63
64 const CustomActorImpl& CustomActor::GetImplementation() const
65 {
66   const Internal::CustomActor& internal = GetImpl(*this);
67
68   return internal.GetImplementation();
69 }
70
71 CustomActor::CustomActor(CustomActorImpl& implementation)
72 : Actor(Internal::CustomActor::New(implementation).Get())
73 {
74 }
75
76 CustomActor::CustomActor(Internal::CustomActor* internal)
77 : Actor(internal)
78 {
79   // Check we haven't just constructed a new handle, while in the internal custom actors destructor.
80   // This can happen if the user defined CustomActorImpl destructor calls Self(), to create a new handle
81   // to the CustomActor.
82   //
83   // If it's in the destructor then the ref count is zero, so once we've created a new handle it will be 1
84   // Without this check, the actor will be deleted a second time, when the handle is disposed of
85   // causing a crash.
86
87   if (internal)
88   {
89     DALI_ASSERT_ALWAYS(internal->ReferenceCount() != 1 && "Are you trying to use CustomActorImpl::Self() inside a CustomActorImpl destructor?");
90   }
91 }
92
93 } // namespace Dali