Integer8 and Integer8Comparer class implementation
authordarpan.ka <darpan.ka@samsung.com>
Mon, 19 Aug 2013 01:59:25 +0000 (10:59 +0900)
committerdarpan.ka <darpan.ka@samsung.com>
Wed, 21 Aug 2013 06:59:35 +0000 (15:59 +0900)
Change-Id: Ia355954c7a215a5d5ba0e7140fe307891e2e492b
Signed-off-by: darpan.ka <darpan.ka@samsung.com>
inc/FBase.h
src/base/CMakeLists.txt
src/base/FBaseInteger8.cpp [new file with mode: 0755]
src/base/FBaseInteger8Comparer.cpp [new file with mode: 0644]

index 98e5852..ee229f3 100644 (file)
@@ -35,6 +35,7 @@
 #include <FBaseBoolean.h>
 #include <FBaseInt8.h>
 #include <FBaseInteger.h>
+#include <FBaseInteger8.h>
 #include <FBaseShort.h>
 #include <FBaseLong.h>
 #include <FBaseLongLong.h>
@@ -43,6 +44,7 @@
 #include <FBaseString.h>
 #include <FBaseInt8Comparer.h>
 #include <FBaseIntegerComparer.h>
+#include <FBaseInteger8Comparer.h>
 #include <FBaseShortComparer.h>
 #include <FBaseLongComparer.h>
 #include <FBaseLongLongComparer.h>
index e90d34a..68a5717 100755 (executable)
@@ -24,8 +24,10 @@ SET (${this_target}_SOURCE_FILES
        FBaseFloatComparer.cpp
        FBaseInt8.cpp
        FBaseInt8Comparer.cpp
+       FBaseInteger8.cpp
        FBaseInteger.cpp
        FBaseIntegerComparer.cpp
+       FBaseInteger8Comparer.cpp
        FBaseLong.cpp
        FBaseLongComparer.cpp
        FBaseLongLong.cpp
diff --git a/src/base/FBaseInteger8.cpp b/src/base/FBaseInteger8.cpp
new file mode 100755 (executable)
index 0000000..a871bf1
--- /dev/null
@@ -0,0 +1,270 @@
+//
+// Copyright (c) 2013 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+/**
+ * @file               FBaseInteger8.cpp
+ * @brief              This is the implementation file for Integer8 class.
+ * @see                        Number class
+ */
+#include <wchar.h>
+#include <errno.h>
+#include <limits.h>
+#include <FBaseInteger8.h>
+#include <FBaseResult.h>
+#include <FBaseCharacter.h>
+#include <FBaseSysLog.h>
+
+namespace Tizen { namespace Base
+{
+
+Integer8::Integer8(void)
+       : value(0)
+       , __pInteger8Impl(null)
+{
+}
+
+Integer8::Integer8(int8_t val)
+       : value(val)
+       , __pInteger8Impl(null)
+{
+}
+
+Integer8::Integer8(const Integer8& rhs)
+       : value(rhs.value)
+       , __pInteger8Impl(null)
+{
+}
+
+Integer8::~Integer8(void)
+{
+}
+
+Integer8&
+Integer8::operator =(const Integer8& rhs)
+{
+       if (&rhs != this)
+       {
+               value = rhs.value;
+       }
+       return *this;
+}
+
+int
+Integer8::Compare(int8_t i81, int8_t i82)
+{
+       return i81 - i82;
+}
+
+int
+Integer8::CompareTo(const Integer8& rhs) const
+{
+       return Integer8::Compare(this->value, rhs.value);
+}
+
+bool
+Integer8::Equals(const Object& obj) const
+{
+       const Integer8* pOther = dynamic_cast< const Integer8* >(&obj);
+       if (pOther == null)
+       {
+               return false;
+       }
+
+       return value == pOther->value;
+}
+
+int
+Integer8::GetHashCode(void) const
+{
+       return static_cast< int >(value);
+}
+
+int
+Integer8::GetHashCode(int8_t val)
+{
+       return static_cast< int >(val);
+}
+
+result
+Integer8::Decode(const String& inputStr, int8_t& ret)
+{
+       SysTryReturnResult(NID_BASE, inputStr.GetLength() >= 1, E_NUM_FORMAT, "The length of input String MUST be greater than 0.");
+
+       long value = 0;
+       int radix = 0;
+       int startIndex = 0;
+       int minLength = 2;
+       wchar_t* pEnd = null;
+       String str(inputStr);
+
+       if (inputStr[0] == L'-' || inputStr[0] == L'+')
+       {
+               startIndex = 1;
+               minLength = 3;
+       }
+
+       // Find radix
+       if (inputStr[startIndex] == L'#')
+       {
+               radix = Character::RADIX_HEXADECIMAL;
+
+               // Remove '#'
+               str.Remove(startIndex, 1);
+       }
+       else if (inputStr[startIndex] == L'0' && (inputStr.GetLength() >= minLength))
+       {
+               if (inputStr[startIndex + 1] == L'x' || inputStr[startIndex + 1] == L'X')
+               {
+                       radix = Character::RADIX_HEXADECIMAL;
+               }
+               else
+               {
+                       radix = Character::RADIX_OCTAL;
+               }
+       }
+       else
+       {
+               radix = Character::RADIX_DECIMAL;
+       }
+
+       result r = E_SUCCESS;
+
+       errno = 0;
+       value = wcstol(str.GetPointer(), &pEnd, radix);
+       int sysErrno = errno;
+
+       SysTryCatch(NID_BASE, pEnd[0] == 0, r = E_NUM_FORMAT, E_NUM_FORMAT,
+               "[%s] Integer8 decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
+       SysTryCatch(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (sysErrno != 0)), r = E_NUM_FORMAT, E_NUM_FORMAT,
+               "[%s] Decoded value cannot fit into an Integer8.", GetErrorMessage(E_NUM_FORMAT));
+
+CATCH:
+       if (value > Integer8::VALUE_MAX)
+       {
+               ret = Integer8::VALUE_MAX;
+       }
+       else if (value < Integer8::VALUE_MIN)
+       {
+               ret = Integer8::VALUE_MIN;
+       }
+       else
+       {
+               ret = static_cast< int8_t >(value);
+       }
+
+       return r;
+}
+
+result
+Integer8::Parse(const String& inputStr, int8_t& ret)
+{
+       return Parse(inputStr, Character::RADIX_DECIMAL, ret);
+}
+
+result
+Integer8::Parse(const String& inputStr, int radix, int8_t& ret)
+{
+       SysTryReturnResult(NID_BASE, ((radix >= Character::RADIX_BINARY) && (radix <= 36)), E_OUT_OF_RANGE,
+               "The radix MUST be between 2 and 36.");
+
+       int len = inputStr.GetLength();
+       SysTryReturnResult(NID_BASE, len > 0, E_NUM_FORMAT, "The length of input String MUST be greater than 0.");
+
+       errno = 0;
+       wchar_t* pEnd = null;
+       long value = wcstol(inputStr.GetPointer(), &pEnd, radix);
+       int sysErrno = errno;
+
+       SysTryReturnResult(NID_BASE, pEnd[0] == 0, E_NUM_FORMAT, "Integer8 parse failed. Scan stopped at (%ls).", pEnd);
+       SysTryReturnResult(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (sysErrno != 0)), E_NUM_FORMAT,
+               "Parsed value cannot fit into an Integer8.");
+
+       if (value > Integer8::VALUE_MAX)
+       {
+               ret = Integer8::VALUE_MAX;
+       }
+       else if (value < Integer8::VALUE_MIN)
+       {
+               ret = Integer8::VALUE_MIN;
+       }
+       else
+       {
+               ret = static_cast< int8_t >(value);
+       }
+
+       return E_SUCCESS;
+}
+
+char
+Integer8::ToChar(void) const
+{
+       return static_cast< signed char >(value);
+}
+
+short
+Integer8::ToShort(void) const
+{
+       return static_cast< short >(value);
+}
+
+int
+Integer8::ToInt(void) const
+{
+       return static_cast< int >(value);
+}
+
+long
+Integer8::ToLong(void) const
+{
+       return static_cast< long >(value);
+}
+
+long long
+Integer8::ToLongLong(void) const
+{
+       return static_cast< long long >(value);
+}
+
+float
+Integer8::ToFloat(void) const
+{
+       return static_cast< float >(value);
+}
+
+double
+Integer8::ToDouble(void) const
+{
+       return static_cast< double >(value);
+}
+
+String
+Integer8::ToString(void) const
+{
+       return (Integer8::ToString(value));
+}
+
+String
+Integer8::ToString(int8_t value)
+{
+       const static unsigned int INTEGER8_LENGTH_MAX = 4;
+
+       wchar_t sValue[INTEGER8_LENGTH_MAX + 1] = {0, };
+       swprintf(sValue, INTEGER8_LENGTH_MAX + 1, L"%d", value);
+
+       return String(sValue);
+}
+
+}} //Tizen::Base
diff --git a/src/base/FBaseInteger8Comparer.cpp b/src/base/FBaseInteger8Comparer.cpp
new file mode 100644 (file)
index 0000000..07a23ef
--- /dev/null
@@ -0,0 +1,53 @@
+//
+// Copyright (c) 2013 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+/**
+ * @file               FBaseInteger8Comparer.cpp
+ * @brief              This is the implementation file for Integer8Comparer class.
+ * @see                        Integer and Tizen::Base::Collection::IComparer
+ */
+#include <FBaseInteger8Comparer.h>
+#include <FBaseInteger8.h>
+#include <FBaseResult.h>
+#include <FBaseSysLog.h>
+
+namespace Tizen { namespace Base
+{
+
+Integer8Comparer::Integer8Comparer(void)
+       : __pInteger8ComparerImpl(null)
+{
+}
+
+Integer8Comparer::~Integer8Comparer(void)
+{
+}
+
+result
+Integer8Comparer::Compare(const Tizen::Base::Object& obj1, const Tizen::Base::Object& obj2, int& cmp) const
+{
+       const Integer8* pInteger1 = dynamic_cast< const Integer8* >(&obj1);
+       const Integer8* pInteger2 = dynamic_cast< const Integer8* >(&obj2);
+
+       SysTryReturnResult(NID_BASE, (pInteger1 != null && pInteger2 != null), E_INVALID_ARG,
+               "Invalid argument is used. Both of the obj1 and obj2 MUST be Integer.");
+
+       cmp = Integer8::Compare(pInteger1->value, pInteger2->value);
+
+       return E_SUCCESS;
+}
+
+}} //Tizen::Base