00001 #ifndef SIMOBJECT_H_
00002 #define SIMOBJECT_H_
00003
00004 #include "SpikeEvent.h"
00005 #include "SimObjectCloneFactory.h"
00006 #include "ScalarField.h"
00007 #include "FieldRegistry.h"
00008 #include "SimObjectInformation.h"
00009 #include "AdvancePhase.h"
00010 #include "AdvanceInfo.h"
00011
00012 #ifndef SWIG
00013
00014 #include <iostream>
00015 using std::ostream;
00016
00017 #endif
00018
00019 #include <vector>
00020
00021 #include <limits>
00022
00023 #include <boost/shared_ptr.hpp>
00024 using boost::shared_ptr;
00025
00027
00030 #define ADVANCEFLAG_HASSPIKED 1
00031
00033
00036 #define ADVANCEFLAG_DEACTIVATE 2
00037
00039
00042 #define SPIKEHITFLAG_ACTIVATE 1
00043
00045
00048 #define RESETFLAG_ACTIVATE 1
00049
00050 class SimObject;
00051 class SimObjectRegistry;
00052 class SimNetwork;
00053 class SingleThreadSimEngine;
00054 class SimObjectIDSortedVector;
00055
00056 #include "RandomEngine.h"
00057
00058 #ifndef SWIG
00060 #define SIMOBJECT( T, A ) \
00061 private: \
00062 friend struct A; \
00063 protected: \
00064 static object_type_t type; \
00065 friend class SimObjectRegistry; \
00066 virtual void setObjectTypeID(object_type_t tid) { type = tid; } \
00067 static SimObjectInformation *simObjectInfo; \
00068 virtual void setSimObjectInformation( SimObjectInformation *info ) { simObjectInfo = info; }; \
00069 public: \
00070 virtual SimObjectInformation const*getSimObjectInformation(void) { return simObjectInfo; }; \
00071 public: \
00072 typedef SimObjectCloneFactory<T> CloneFactory; \
00073 static T model; \
00074 virtual SimObject* create(void) const { T* o = new T(*this); o->deepCopy(this); return o; }; \
00075 virtual SimObject* create(RandomEngine *) const { T* o = new T(*this); o->deepCopy(this); return o; }; \
00076 virtual object_type_t getObjectTypeID(void) const { return type; }; \
00077 static object_type_t objectTypeID(void) { return type; }; \
00078 static void registerField( Field const& f ) { simObjectInfo->getFieldRegistry().registerField(f); }; \
00079 static void registerFields(void); \
00080 static void registerSimObject(void); \
00081
00082 #endif
00084
00099 class SimObject : public SimObjectFactory
00100 {
00101 public:
00102
00103
00105
00106
00107 #ifndef SWIG
00109 class ID
00110 {
00111 public :
00112
00114 typedef long long Packed;
00115
00117 typedef shared_ptr< std::vector< Packed > > Vector;
00118
00119 explicit ID( nodeid_t nid, engineid_t eid, object_type_t tid, local_objectid_t oid ) :
00120 localid(oid), node(nid), type(tid), eng(eid)
00121 { };
00122
00123 ID(void) :
00124 localid(INVALID_LOCAL_OBJECT_ID), node(INVALID_NODE_ID), type(INVALID_OBJECT_TYPE_ID), eng(INVALID_LOCAL_ENGINE_ID)
00125 { };
00126
00127 typedef SimObjectIDSortedVector SortedVector;
00128
00130 explicit ID( const Packed &pid )
00131 {
00132 *this = *((const ID*)(&pid));
00133 };
00134
00135 static SimObject::ID makeID( const Packed &pid )
00136 {
00137 return *((ID*)(&pid));
00138 };
00139
00140 bool operator<(const ID &other) const
00141 {
00142 if (this->node == other.node) {
00143 if (this->eng == other.eng) {
00144 if (this->type == other.type) {
00145 return this->localid < other.localid;
00146 }
00147 return this->type < other.type;
00148 }
00149 return this->eng < other.eng;
00150 }
00151 return this->node < other.node;
00152 }
00153
00154 bool isInTheSameSTEngineWith(const ID &other) const
00155 {
00156 return ( this->eng == other.eng ) &&
00157 ( this->node == other.node );
00158 }
00159
00161 local_objectid_t localid ;
00162
00164 nodeid_t node ;
00165
00167 object_type_t type;
00168
00170 engineid_t eng ;
00171
00173 Packed packed();
00174
00176 bool operator==(const ID& other) const;
00177
00179 bool operator!=(const ID& other) const;
00180
00182 string toString(void) const;
00183
00185 static const ID Invalid;
00186 };
00187
00189 #endif
00190
00191
00193
00194
00195 public:
00197 SimObject(engineid_t eid=0);
00198
00199 virtual ~SimObject();
00200
00201
00202
00203
00205
00206
00207 public:
00208 typedef enum { spiking, analog, undefined } PortType;
00209 virtual int nSpikeInputPorts() const { return 0; };
00210 virtual int nSpikeOutputPorts() const { return 0; };
00211 virtual int nAnalogInputPorts() const { return 0; };
00212 virtual int nAnalogOutputPorts() const { return 0; };
00213 virtual PortType outputPortType(port_t p) const { return undefined; };
00214 virtual PortType inputPortType(port_t p) const { return undefined; };
00215
00216
00217
00218
00220
00221
00223 virtual int init(InitializeInfo *ii) { return 0; };
00224
00226 virtual int check(void) { return 0; };
00227
00229
00230
00231 virtual int adjust( double dt ) { return 0; };
00232
00234 virtual int reset( double dt ) { return 0; };
00235
00237
00240 virtual int advance( AdvanceInfo const& ) { return 0; };
00241
00242 #ifndef SWIG
00243
00245
00246
00248 virtual void incoming(SimObject *sender, ID const& snd, ID const& self, SimNetwork & net) {
00249 incoming(sender);
00250 };
00251
00253 virtual void outgoing(SimObject *receiver, ID const& rec, ID const& self, SimNetwork & net ) {
00254 outgoing(receiver);
00255 };
00256
00258
00262 virtual void outgoing(SimObject *receiver) {};
00263
00265
00269 virtual void incoming(SimObject *sender) {};
00270
00271
00273
00274
00276
00277
00278 public:
00281
00288 virtual int spikeHit( spikeport_t port, SpikeEvent const& spike ) { return 0; };
00289
00291
00292
00293
00294
00295 virtual double getManagedDelay() const { return 0; }
00296
00298
00299
00301
00302
00303 #endif
00304
00305 public:
00306
00307 #ifndef SWIG
00309 virtual int getFieldOffset(string const& fieldname);
00310 #endif
00311
00313 virtual double getAnalogOutput(analog_port_id_t port = 0) const { return 0; };
00314
00316 virtual void setAnalogInput(double value, analog_port_id_t port = 0) { };
00317
00318 #ifndef SWIG
00319
00321
00325 virtual bool owned_by_net() {
00326 return true;
00327 }
00328
00329
00331
00336 virtual SimObject * getChainedObject() {
00337 return NULL;
00338 }
00339
00340 virtual bool toBeActivated()
00341 {
00342 return false;
00343 }
00344
00345 virtual SimObject * getInputTarget(int target_id = 0) {
00346 return this;
00347 }
00348
00349 virtual void deepCopy(const SimObject *obj)
00350 {
00351 }
00352
00353
00355
00356
00357 protected:
00358 friend class SimObjectRegistry;
00360 static object_type_t type;
00362 virtual void setObjectTypeID(object_type_t tid) { };
00363
00364 static SimObjectInformation *simObjectInfo;
00365 virtual void setSimObjectInformation( SimObjectInformation *info ) { simObjectInfo = info; };
00366 public:
00367 virtual SimObjectInformation const*getSimObjectInformation(void) { return simObjectInfo; };
00368 Field const* findField(string const & fieldname);
00369 void setScalarField( Field const*f, double v);
00370 double getScalarField( Field const*f);
00372
00373
00375
00376
00377 public:
00379
00380
00381
00382
00384
00385
00387
00388
00389 protected:
00390
00392
00394
00395 private:
00396
00398
00399
00401
00402
00403
00405
00406
00407
00409
00410
00411 public:
00413
00417 class BlockAllocator
00418 {
00419 public:
00420 BlockAllocator( size_t blockSize = SIMOBJECT_ALLOCATOR_BLOCK_SIZE );
00421 ~BlockAllocator();
00422 void *allocate( size_t size );
00423 private:
00424 size_t blk_sz;
00425 std::vector< void* > mem_block;
00426 char *free_pos;
00427 size_t num_free_bytes;
00428 };
00429
00430 void static setAllocator( BlockAllocator & ba );
00431
00432 void *operator new(size_t sz);
00433 void operator delete(void *, size_t);
00434
00435 void *operator new[](size_t sz);
00436 void operator delete[](void *, size_t);
00437
00438 private:
00439
00440 static BlockAllocator defaultAllocator;
00441 static BlockAllocator *currentAllocator;
00443
00444 #endif
00445
00446 };
00447
00448 #ifndef SWIG
00449
00451 typedef std::vector< SimObject* > SpikeReceiverList;
00452
00454 ostream& operator<<(ostream &s, const SimObject::ID &id);
00455
00456 #endif
00457
00458 #endif