GLAMERDOC++
Gravitational Lensing Code Library
Loading...
Searching...
No Matches
Utilities::MixedVector< BaseT > Class Template Reference

A container that can hold mixed objects all derived from a base class and retains the ability to access derived class functions/members. More...

#include <utilities_slsim.h>

Public Member Functions

 MixedVector ()
 default constructor
 
 ~MixedVector ()
 destroy the vector and all contained items
 
MixedVector< BaseT > & operator= (MixedVector< BaseT > rhs)
 assign one MixedVector to another
 
template<typename SubclassT >
void copy_back (const SubclassT &obj)
 add a copy of an object of type SubclassT to the vector, in contrast push_back() deos not copy and will take possession of the object
 
void pop_back ()
 pop element from back of vector
 
template<typename SubclassT >
void pop_back ()
 erase the last element of a specific type
 
void clear ()
 clear all elements
 
template<typename SubclassT >
void clear ()
 clear all elements of a given type
 
BaseT * data ()
 direct access to underlying array
 
const BaseT * data () const
 direct access to underlying array (const)
 
template<typename SubclassT >
bool type (std::size_t i)
 Checks if element i is of the derived type SubclassT.
 
BaseT & operator[] (std::size_t i)
 Indexing operator for all elements.
 
const BaseT & operator[] (std::size_t i) const
 Indexing operator for all elements (const).
 
BaseT & get (std::size_t i)
 indexed access
 
const BaseT & get (std::size_t i) const
 indexed access (const)
 
template<typename SubclassT >
SubclassT & get (std::size_t i)
 indexed access for type SubclassT
 
template<typename SubclassT >
const SubclassT & get (std::size_t i) const
 indexed access for type SubclassT (const)
 
BaseT & at (std::size_t i)
 indexed access with bounds checking
 
const BaseT & at (std::size_t i) const
 indexed access with bounds checking (const)
 
template<typename SubclassT >
SubclassT & at (std::size_t i)
 Templated indexing operator for elements of a specific derived class. The index runs 0 ... size<SubclassT>() - 1 Does bounds checking.
 
template<typename SubclassT >
const SubclassT & at (std::size_t i) const
 Templated indexing operator for elements of a specific derived class (const).
 
std::size_t size () const
 number of all elements
 
template<typename SubclassT >
std::size_t size () const
 number of elements of a specific type
 
bool empty () const
 check if vector of items is empty
 
template<typename SubclassT >
bool empty () const
 check if vector of items of type SubclassT is empty
 
iterator begin ()
 get iterator to first of all items
 
iterator end ()
 get iterator to last of all items
 
template<typename SubclassT >
iterator< SubclassT > begin ()
 get iterator to first of items of type SubclassT
 
template<typename SubclassT >
iterator< SubclassT > end ()
 get iterator to last of items of type SubclassT
 

Friends

void swap (MixedVector< BaseT > &a, MixedVector< BaseT > &b)
 swap two MixedVectors
 

Detailed Description

template<typename BaseT>
class Utilities::MixedVector< BaseT >

A container that can hold mixed objects all derived from a base class and retains the ability to access derived class functions/members.

The indexing operator is overridden to return a reference to the base class and a templated index<>() function provides a reference to elements with derived class type information.

  example:
  InputParams params(paramfile);
  params.put("gauss_r2",1,"non");
  SourceGaussian source1(params);
  SourceUniform source2(params);

  Utilities::MixedVector<Source> mvector;

  mvector.push_back(source1);
  mvector.push_back(source1);
  mvector.push_back(source2);
  mvector.push_back(source2);
  std::cout << "Number of Uniform Sources " << mvector.size<SourceUniform>() << "   Number of Gausssian Sources "
            << mvector.size<SourceGaussian>() << std::endl;

  // change derived class attribute
  mvector.get<SourceGaussian>(0).source_gauss_r2 = 0.5;
  std::cout << "A base class attribute " << mvector[2].getTotalFlux()
            << " A derived class attribute  "
            << mvector.get<SourceGaussian>(0).source_gauss_r2
            << "  " << mvector.get<SourceGaussian>(1).source_gauss_r2
            << std::endl;

  // iterate all sources
  for(Utilities::MixedVector<Source>::iterator<> it = mvector.begin(); it != mvector.end(); ++it)
    std::cout << "A base class attribute " << it->getTotalFlux() << std::endl;

  // iterate SersicSources
  for(Utilities::MixedVector<Source>::iterator<SersicSource> it = mvector.begin<SersicSource>(); it != mvector.end<SersicSource>(); ++it)
    std::cout << "A derived class attribute " << it->getSersicIndex() << std::endl;
*

The documentation for this class was generated from the following file: