Well I have a problem, I don't know how to put it properly, but I have included the code below. I have cut alot out of the code so that it is alot easier to understand the problem, but it might not work 100% now, it's just to illustrate.
The problem is that the template specialization doesn't work.
E.g.:
#include <iostream>
#include <swift.hpp>
#include <swift/event.hpp>
#include <swift/event/primitives.tpl>
using namespace swift;
template<class T> void print(const T& value)
{
std::cout << event::representate(value) << std::endl;
}
int main()
{
Int32 x = 23;
print(x);
}
The problem here is, that "~" will be printed. So event::representate() points in this example to the function defined in "swift/event.hpp", and not to the specialized function defined in "swift/event/primitives.hpp".
Ok, this isn't really the problem because it could easily been solved by changing the representate function in "swift/event.hpp" to the function body of the specialized reprensentate.
But I am not able to do that because of the following to reasons:
- It won't work with custom classes, if there is not a std::stream operator << defined for the custom class (as of free functions, it could be possible though).
- It is not only the event::representate function that needs specialization. But also event::serialize and event::deserialize.
Well looking at point 2; class OStream uses event::serialize and event::deserialize in a templated member function. But the specialization of event::serialize and event::deserialize must be able to use the OStream (template) methods.
Well I'm stuck at that point. I hope someone could point me in the right direction!
Source
swift.hpp:
#ifndef HG_swift
#define HG_swift
// C++ Includes
#include <boost/cstdint.hpp>
#include <string>
namespace swift
{
// Cross-platform primairy types:
typedef void Void;
typedef bool Bool;
typedef Bool Bit;
typedef char Char;
typedef unsigned char UChar;
typedef uint8_t Byte;
typedef int16_t Int16;
typedef uint16_t UInt16;
typedef int32_t Int32;
typedef uint32_t UInt32;
typedef Int16 Short;
typedef UInt16 UShort;
typedef Int32 Int;
typedef UInt32 UInt;
typedef double Double;
typedef float Float;
typedef std::string String;
}
#endif //HG_swift
swift/event.hpp
#ifndef swift_event
#define swift_event
#include <iostream>
#include <sstream>
#include <exception>
#include <swift/String.hpp>
namespace swift
{
class OStream;
class IStream;
namespace event
{
template<class T> String representate(T& value)
{
return "~";
}
template<class T> void serialize(T& value, OStream& stream) { }
template<class T> void deserialize(T& value, IStream& stream) { }
}
}
#endif //swift_event
swift/OStream.hpp:
#ifndef HG_swift_OStream
#define HG_swift_OStream
// Imports:
#include "swift.hpp"
// C++ includes:
#include <iostream>
#include <boost/archive/binary_oarchive.hpp>
namespace swift
{
struct OStream
{
boost::archive::binary_oarchive archive;
OStream(std::ostream& os) : archive(os, boost::archive::no_header) {}
template<class T> void write(T value)
{
event::serialize(value, *this);
}
};
}
#endif //HG_swift_io_OStream
swift/event/primitives.tpl:
#include <swift.hpp>
#include <swift/event.hpp>
//#include <swift/IStream.hpp>
#include <swift/OStream.hpp>
#include <iostream>
#include <sstream>
namespace swift
{
// Events
namespace event
{
// Events for primary types:
// Int32
template<> String representate<Int32>(Int32& value)
{
std::stringstream str;
str << value;
return str.str();
}
template<> void serialize<Int32>(Int32& value, OStream& stream)
{
stream.archive & value;
}
template<> void deserialize<Int32>(Int32& value, IStream& stream)
{
stream.archive & value;
}
}
}