| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- /*
- Copyright (c) 2013 Daniele Bartolini, Michele Rossi
- Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- */
- #include "Generic.h"
- #include "Types.h"
- namespace crown
- {
- //-----------------------------------------------------------------------------
- Generic::Generic() : m_type(GT_UNDEFINED)
- {
- }
- //-----------------------------------------------------------------------------
- Generic::Generic(const Generic& other)
- {
- *this = other;
- }
- //-----------------------------------------------------------------------------
- Generic::~Generic()
- {
- }
- //-----------------------------------------------------------------------------
- Generic::Generic(int32_t value) : m_type(GT_INT)
- {
- m_data.int32_t_value = value;
- }
- //-----------------------------------------------------------------------------
- Generic::Generic(uint32_t value) : m_type(GT_UINT)
- {
- m_data.uint32_t_value = value;
- }
- //-----------------------------------------------------------------------------
- Generic::Generic(uint16_t value) : m_type(GT_USHORT)
- {
- m_data.uint16_t_value = value;
- }
- //-----------------------------------------------------------------------------
- Generic::Generic(float value) : m_type(GT_FLOAT)
- {
- m_data.float_value = value;
- }
- //-----------------------------------------------------------------------------
- Generic::Generic(bool value) : m_type(GT_BOOL)
- {
- m_data.bool_value = value;
- }
- //-----------------------------------------------------------------------------
- bool Generic::as_int32_t(int32_t& out) const
- {
- if (m_type == GT_INT)
- {
- out = m_data.int32_t_value;
- return true;
- }
- if (m_type == GT_UINT)
- {
- out = (int32_t)m_data.uint32_t_value;
- return true;
- }
- if (m_type == GT_USHORT)
- {
- out = (int32_t)m_data.uint16_t_value;
- return true;
- }
- if (m_type == GT_FLOAT)
- {
- out = (int32_t)m_data.float_value;
- return true;
- }
- if (m_type == GT_BOOL)
- {
- out = m_data.bool_value ? 1 : 0;
- return true;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- int32_t Generic::as_int32_t() const
- {
- int32_t value = 0;
- as_int32_t(value);
- return value;
- }
- //-----------------------------------------------------------------------------
- bool Generic::as_uint32_t(uint32_t& out) const
- {
- if (m_type == GT_INT)
- {
- out = (uint32_t)m_data.int32_t_value;
- return true;
- }
- if (m_type == GT_UINT)
- {
- out = m_data.uint32_t_value;
- return true;
- }
- if (m_type == GT_USHORT)
- {
- out = (uint32_t)m_data.uint16_t_value;
- return true;
- }
- if (m_type == GT_FLOAT)
- {
- out = (uint32_t)m_data.float_value;
- return true;
- }
- if (m_type == GT_BOOL)
- {
- out = m_data.bool_value?1:0;
- return true;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- uint32_t Generic::as_uint32_t() const
- {
- uint32_t value = 0;
- as_uint32_t(value);
- return value;
- }
- //-----------------------------------------------------------------------------
- bool Generic::as_uint16_t(uint16_t& out) const
- {
- if (m_type == GT_INT)
- {
- out = (uint16_t)m_data.int32_t_value;
- return true;
- }
- if (m_type == GT_UINT)
- {
- out = (uint16_t)m_data.uint32_t_value;
- return true;
- }
- if (m_type == GT_USHORT)
- {
- out = m_data.uint16_t_value;
- return true;
- }
- if (m_type == GT_FLOAT)
- {
- out = (uint16_t)m_data.float_value;
- return true;
- }
- if (m_type == GT_BOOL)
- {
- out = m_data.bool_value?1:0;
- return true;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- uint16_t Generic::as_uint16_t() const
- {
- uint16_t value = 0;
- as_uint16_t(value);
- return value;
- }
- //-----------------------------------------------------------------------------
- bool Generic::as_float(float& out) const
- {
- if (m_type == GT_INT)
- {
- out = (float)m_data.int32_t_value;
- return true;
- }
- if (m_type == GT_UINT)
- {
- out = (float)m_data.uint32_t_value;
- return true;
- }
- if (m_type == GT_USHORT)
- {
- out = (float)m_data.uint16_t_value;
- return true;
- }
- if (m_type == GT_FLOAT)
- {
- out = m_data.float_value;
- return true;
- }
- if (m_type == GT_BOOL)
- {
- out = m_data.bool_value?1.0f:0.0f;
- return true;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- float Generic::as_float() const
- {
- float value = 0.0f;
- as_float(value);
- return value;
- }
- //-----------------------------------------------------------------------------
- bool Generic::as_bool(bool& out) const
- {
- if (m_type == GT_INT)
- {
- out = m_data.int32_t_value==0?false:true;
- return true;
- }
- if (m_type == GT_UINT)
- {
- out = m_data.uint32_t_value==0?false:true;
- return true;
- }
- if (m_type == GT_USHORT)
- {
- out = m_data.uint16_t_value==0?false:true;
- return true;
- }
- if (m_type == GT_FLOAT)
- {
- //Does it make sense?
- out = m_data.float_value==0.0f?false:true;
- return true;
- }
- if (m_type == GT_BOOL)
- {
- out = m_data.bool_value;
- return true;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- bool Generic::as_bool() const
- {
- bool value = false;
- as_bool(value);
- return value;
- }
- //-----------------------------------------------------------------------------
- bool Generic::operator==(const Generic& other)
- {
- if (m_type != other.m_type)
- {
- return false;
- }
- switch (m_type)
- {
- case GT_INT:
- return m_data.int32_t_value == other.m_data.int32_t_value;
- break;
- case GT_UINT:
- return m_data.uint32_t_value == other.m_data.uint32_t_value;
- break;
- case GT_USHORT:
- return m_data.uint16_t_value == other.m_data.uint16_t_value;
- break;
- case GT_FLOAT:
- return m_data.float_value == other.m_data.float_value;
- break;
- case GT_BOOL:
- return m_data.bool_value == other.m_data.bool_value;
- break;
- case GT_UNDEFINED:
- return true;
- break;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- const Generic& Generic::operator=(const Generic& other)
- {
- m_type = other.m_type;
- switch (m_type)
- {
- case GT_INT:
- m_data.int32_t_value = other.m_data.int32_t_value;
- break;
- case GT_UINT:
- m_data.uint32_t_value = other.m_data.uint32_t_value;
- break;
- case GT_USHORT:
- m_data.uint16_t_value = other.m_data.uint16_t_value;
- break;
- case GT_FLOAT:
- m_data.float_value = other.m_data.float_value;
- break;
- case GT_BOOL:
- m_data.bool_value = other.m_data.bool_value;
- break;
- case GT_UNDEFINED:
- //Copy nothing
- break;
- }
- return other;
- }
- } // namespace crown
|