help with templates and perl
Michele Joyce <erb <at> jlab.org>
2007-01-03 18:50:34 GMT
Hi,
I'm trying to wrap perl with swig 1.39 for linux...
I'm sure that this problem has an easy answer, I just can't come up
with it. Relevant code is in RED
The relevant header file:
event.h
#ifndef MYA_EVENT_H
#define MYA_EVENT_H
////////////////////////////////////////////////////////////////////////////////
/*! \file event.h
* \brief Contains MYA channel event class definitions
* \author Christopher J. Slominski
* \par Revision History:
* - 09/01/2006 Initial development
*/
#include "eventtype.h"
#include <time.h>
#include <string>
#include <iostream>
#include <vector>
// Provide forward references to make the compiler happy.
namespace MYA { class BasicEvent; }
std::ostream & operator << (std::ostream &, const
MYA::BasicEvent &);
namespace MYA {
/*! \brief Abstract base for channel events.
*
* This class serves as the base for the set of concrete MYA
* channel events. It is responsible for the handling of event time
* and event type code common to all channel events. Note that event
* timestamps are kept in the MYA time format, which is a 64-bit
* integer, scaled as seconds * 2^32, offset from the UNIX epoch.
*/
class BasicEvent
{ friend std::ostream & ::operator << (std::ostream &,
const BasicEvent &);
public: // Construction/Destruction.
//! Zero all fields on construction.
BasicEvent(void) { memset(this, 0, sizeof(BasicEvent)); }
//! Only provided for proper polymorphic destruction.
virtual ~BasicEvent(void) {}
public: // Timestamp methods.
//! Return time difference in seconds.
inline double operator - (const BasicEvent &);
//! Return timestamp in UNIX struct format.
inline struct timespec & DateTime(struct timespec &) const;
//! Return timestamp in seconds past UNIX epoch.
inline time_t Seconds(void) const;
//! Return timestamp as formatted text string.
std::string DateString(void) const;
//! Return timestamp in MYA format.
long long DateTime(void) const { return m_time; };
public:
unsigned Code(void) const { return m_code; } //!< Event type
code
bool Empty(void) const { return m_time == 0; } //!<
Uninitialized event?
static unsigned m_precision; //!< Output
formatting
static char m_separator; //!< Output
formatting
protected:
//! Insert event's time into a stream.
void StreamTime(std::ostream &) const;
//! Insert event's value into a stream.
virtual void StreamValue(std::ostream &) const = 0;
protected:
long long m_time; //!< MYA Timestamp associated with channel
event.
unsigned m_code; //!< Event type code.
};
/*! \brief Array type of channel event.
*
* This class represents multiple value channel events of any data type.
* It can be used for single value channel events, but adds a little
* complexity to get the value and is less efficient than the
ScalarEvent.
*/
template <typename Type>
class ArrayEvent : public BasicEvent
{ friend class ArchivePortal;
public:
//! Accessor of channel event values.
const std::vector<Type> & Value(void) const { return
m_value; }
private:
//! Insert event values into the passed stream.
void StreamValue(std::ostream &) const;
private:
std::vector<Type> m_value; // Collection of values for this
event.
};
/*!
* The timespec UNIX struct provides seconds past the epoch and
* nano-seconds within the second.
* \return structure with .tv_sec and .tv_nsec
*/
inline struct timespec & BasicEvent::DateTime(struct timespec
&ts) const
{
// The seconds if the upper 32 bits of the MYA time. The lower
// 32-bits must be convert to nano-seconds by scaling with
// 10^9/2^32 = 0.232830643.
ts.tv_sec = m_time >> 32;
unsigned fraction = static_cast<unsigned>(m_time &
0xFFFFFFFF);
ts.tv_nsec = static_cast<int>(fraction * 0.232830643);
return ts;
}
/*!
* The seconds value is simply the upper 32 bits of MYA time.
* \return UNIX time_t
*/
inline time_t BasicEvent::Seconds(void) const
{
return m_time >> 32;
}
/*!
* \return Time difference in floating point seconds.
*/
inline double BasicEvent::operator - (const BasicEvent &ev)
{
// Subtract the MYA times and scale by 1/2^32 to get seconds.
return static_cast<double>(m_time - ev.m_time) *
2.328306437e-10;
}
/*!
* Insert each of the set of values associated with this event into
* the passed stream, separating then by the chosen seperator character.
*/
template <typename Type>
void ArrayEvent<Type>::StreamValue(std::ostream &ostr) const
{
// The event's values are in a stl vector.
typename std::vector<Type>::const_iterator it = m_value.begin();
// Iterate through the vector, inserting values and seperators.
while (it != m_value.end())
{ if (it != m_value.begin()) ostr << m_separator;
ostr << *it++;
}
}
}
#endif
My interface file:
%module event.i
#pragma SWIG nowarn=503
%include std_string.i
%include std_vector.i
%include std_map.i
%include typemaps.i
%{
#bunch of include files that I don't think are relevant to this problem
#include "/cs/certified/apps/myapi/v2.0/inc/event.h"
%}
%{
using namespace MYA;
%}
#this is creating a a vector of Array Events (which is itself a vector
of std::string)
template <typename T, typename E>
struct Era : public std::vector< E > {};
#not sure what's really necessary here. But, I this combination is how
I got it to compile...(help is appreciated)
%typedef MYA::ArrayEvent<std::string>
ArrayEvent<std::string>;
%template(stringArrayEvent) MYA::ArrayEvent<std::string>;
%typedef std::vector<ArrayEvent<std::string> > arrayVector;
%template(arrayVector) std::vector<ArrayEvent<std::string>
>;
%template(arrayEra) Era<std::string, ArrayEvent<std::string>
>;
%template(eraPast) MYA::ArchivePortal::Past<std::string>;
Perl code:
#create an ERA (vector of array events)
my $era = new myapi::arrayEra();
print $era, "\n";
#gives me myapi::arrayEra=HASH(0x8e54178)
#now, pass this hash into eraPast so that eraPast can fill the era
with events...an event should have
$ap->eraPast($channelkey, $ago, $now, $era, 24);
for (my $i = 0; $i < $era->size(); $i++) {
my $event = $era->get($i);
print "$event \n";
#gives me a scalar foreach event in the
era!! _p_ArrayEventTstd__string_t=SCALAR(0x8e54220)
print "$$event\n";
#gives me an integer that i can't
decipher: 141225152
}
So, basically, I'm either not accessing the event information
correctly, or I've messed up with the templates and typedefs in my
initialization file. I'm so confused now, I don't know where to begin.
Any help is appreciated.
Michele
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user