C++ std::vector<> map in Scripts Languages

We use C++ std::vector<T> when we need an array or a list. The benefits of the std::vector<T>, are:

  1. C++ manages the memory for us. We don’t need to allocate or free.
  2. The vector can grow as we do not need to allocate on advance.

In the following discussion we shall call:

SWIG does a special translation of std::vector<T> in a way which differs from one script language to another. In addition, SWIG requires explicit announcement for the mapping.

Currently the following std::vector<T> maps are announced in libptpmgmt.i:

Class name in scriptsC++ std::vector<T>header
FaultRecord_vFaultRecord_ttypes.h
ClockIdentity_vClockIdentity_ttypes.h
PortAddress_vPortAddress_ttypes.h
AcceptableMaster_vAcceptableMaster_ttypes.h
LinuxptpUnicastMaster_vLinuxptpUnicastMaster_ttypes.h
PtpEvent_vPtpEvent_tptp.h
PtpSample_vPtpSample_tptp.h
PtpSampleExt_vPtpSampleExt_tptp.h
SigTimeSLAVE_RX_SYNC_TIMING_DATA_rec_tsig.h
SigCompSLAVE_RX_SYNC_COMPUTED_DATA_rec_tsig.h
SigEventSLAVE_TX_EVENT_TIMESTAMPS_rec_tsig.h
SigDelaySLAVE_DELAY_TIMING_DATA_NP_rec_tsig.h

The container can be part of another class or structure. For example, std::vector<FaultRecord_t> is part of FAULT_LOG_t, the structure used for FAULT_LOG Managment TLV.

It could be an argument of a function. For example, std::vector<PtpEvent_t> is an argument of readEvents().


Container Constructors

The allocation and release of both container and element are done in the same way we allocate other SWIG classes. The container provides the following constructors, following std::vector<> constructors.

#resultargument 1argument 2
0empty
1copy constructorcontainer
1insert default elementsnumber
2copies of elementnumberelement

PHP does not support the last, 2 arguments constructor!


Comparison of C++ Class Methods with Scripts

The C++ column lists the methods of std::vector<>.

-NA- means the method is not available.

Python and Ruby support more methods.

C++PerlPHPTclGoLuaPythonRuby
sizesizesizesizeSizesizesizesize
emptyemptyis_emptyemptyIsEmptyemptyemptyempty?
clearclearclearclearClearclearclearclear
push_backpushpushpushAddpush_backpush_backpush
back+pop_backpoppoppop-NA--NA-poppop
back-NA--NA--NA--NA-backbackback
pop_back-NA--NA--NA--NA-pop_backpop_back-NA-
front-NA--NA--NA--NA-frontfrontfront
[]getgetgetGet[][][]
[]=setsetsetSet[]=[]=[]=
capacity-NA-capacity-NA-Capacity-NA-capacitycapacity
reserve-NA-reserve-NA-Reserve-NA-reservereserve
max_size-NA--NA--NA--NA-max_size-NA--NA-

More Documentation

The container in Python acts as list and in Ruby as array.

Perl, Lua, PHP, Tcl, and Go use only the methods declared in vecDef.h. See the documentation of vecDef.h for more details on the container methods.