memory leak fix
[platform/framework/native/uix.git] / src / FUixSensorMotion.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <new>
19 #include <FBaseErrors.h>
20 #include <FUixSensorMotion.h>
21 #include <FBaseSysLog.h>
22 #include <FUixSensor_MotionImpl.h>
23
24 using namespace Tizen::Base;
25
26 namespace Tizen { namespace Uix { namespace Sensor
27 {
28
29 Motion::Motion(void)
30         : __pMotionImpl(null)
31 {
32
33 }
34
35 Motion::~Motion(void)
36 {
37         if (__pMotionImpl != null)
38         {
39                 __pMotionImpl->SetEnabled(MOTION_TYPE_NONE);
40
41                 delete __pMotionImpl;
42                 __pMotionImpl = null;
43         }
44 }
45
46 result
47 Motion::Construct(IMotionEventListener& listener)
48 {
49     result r = E_SUCCESS;
50     ClearLastResult();
51     SysAssertf(__pMotionImpl == null, "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class.");
52     __pMotionImpl = new (std::nothrow) _MotionImpl;
53     SysTryReturn(NID_UIX, __pMotionImpl != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
54     r = __pMotionImpl->Construct(listener);
55     switch (r)
56     {
57     case E_UNSUPPORTED_OPERATION:
58          SysLogException(NID_UIX, r, "[%s] The motion detection feature is not supported", GetErrorMessage(r));
59         break;
60
61     case E_INVALID_STATE:
62         SysLogException(NID_UIX, r, "[%s] This instance is in an invalid state.", GetErrorMessage(r));
63         break;
64
65     default:
66         break;
67     }
68     return r;
69 }
70
71 void
72 Motion::SetEnabled(unsigned long type)
73 {
74     result r = E_SUCCESS;
75     ClearLastResult();
76
77     SysAssert(__pMotionImpl != null);
78
79     SysTryReturnVoidResult(NID_UIX, __pMotionImpl->IsMotionTypeValid(type), E_INVALID_ARG, "[E_INVALID_ARG] The given type is invalid combination.");
80
81     __pMotionImpl->SetEnabled(type);
82     r = GetLastResult();
83     SysTryReturnVoidResult(NID_UIX, r == E_SUCCESS, r, "[%s] This instance has not been constructed as yet.", GetErrorMessage(r));
84 }
85
86 bool
87 Motion::IsSupported(MotionType type) const
88 {
89     ClearLastResult();
90
91     SysAssert(__pMotionImpl != null);
92
93         return __pMotionImpl->IsSupported(type);
94 }
95
96 } } } // Tizen::Uix::Sensor