/*
 * Copyright (C) 2014 by
 *   MetraLabs GmbH (MLAB), GERMANY
 * and
 *   Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
 * All rights reserved.
 *
 * Contact: info@mira-project.org
 *
 * Commercial Usage:
 *   Licensees holding valid commercial licenses may use this file in
 *   accordance with the commercial license agreement provided with the
 *   software or, alternatively, in accordance with the terms contained in
 *   a written agreement between you and MLAB or NICR.
 *
 * GNU General Public License Usage:
 *   Alternatively, this file may be used under the terms of the GNU
 *   General Public License version 3.0 as published by the Free Software
 *   Foundation and appearing in the file LICENSE.GPL3 included in the
 *   packaging of this file. Please review the following information to
 *   ensure the GNU General Public License version 3.0 requirements will be
 *   met: http://www.gnu.org/copyleft/gpl.html.
 *   Alternatively you may (at your option) use any later version of the GNU
 *   General Public License if such license has been publicly approved by
 *   MLAB and NICR (or its successors, if any).
 *
 * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
 * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
 * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
 */

/**
 * @file bitset
 *    Reflect for std::bitset.
 *
 * @author Tim Langner
 * @date   2014/02/28
 */

#ifndef _MIRA_BITSET_
#define _MIRA_BITSET_

#include <bitset>
#include <serialization/ReflectorInterface.h>
#include <serialization/SplitReflect.h>
#include <serialization/IsTransparentSerializable.h>
#include <serialization/BinarySerializer.h>

namespace mira {

///////////////////////////////////////////////////////////////////////////////

/// Specialization of the non-intrusive reflect for std::bitset<N>.
template<typename Reflector, std::size_t N>
void reflectRead(Reflector& r, std::bitset<N>& t)
{
	std::string v = t.to_string();
	r.delegate(v);
}

/// Specialization of the non-intrusive reflect for std::bitset<N>.
template<typename Reflector, std::size_t N>
void reflectWrite(Reflector& r, std::bitset<N>& t)
{
	std::string v;
	r.delegate(v);
	t = std::bitset<N>(v);
}

/**
 * specialization for binary serialization, where we don't need to store
 * value as a string
*/
template<typename Derived, std::size_t N>
void reflectRead(BinarySerializer<Derived>& r, std::bitset<N>& t)
{
	uint64 v = t.to_ulong();
	r.delegate(v);
}

template<typename Derived, std::size_t N>
void reflectWrite(BinaryDeserializer<Derived>& r, std::bitset<N>& t)
{
	uint64 v;
	r.delegate(v);
	t = std::bitset<N>(v);
}

template<typename Reflector, std::size_t N>
void reflect(Reflector& r, std::bitset<N>& t)
{
	splitReflect(r, t);
}

template<std::size_t N, typename SerializerTag>
class IsTransparentSerializable<std::bitset<N>,SerializerTag> : public std::true_type {};
///////////////////////////////////////////////////////////////////////////////

} // namespace

#endif
