Skip to content

Commit

Permalink
introduce AccessorFixedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
fredroy committed Nov 15, 2024
1 parent 468baab commit 7734dad
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sofa/framework/Helper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ set(HEADER_FILES
${SRC_ROOT}/init.h
${SRC_ROOT}/integer_id.h
${SRC_ROOT}/accessor/ReadAccessor.h
${SRC_ROOT}/accessor/ReadAccessorFixedArray.h
${SRC_ROOT}/accessor/ReadAccessorVector.h
${SRC_ROOT}/accessor/WriteAccessor.h
${SRC_ROOT}/accessor/WriteAccessorFixedArray.h
${SRC_ROOT}/accessor/WriteAccessorVector.h
${SRC_ROOT}/accessor/WriteOnlyAccessor.h
${SRC_ROOT}/io/BaseFileAccess.h
Expand Down
12 changes: 12 additions & 0 deletions Sofa/framework/Helper/src/sofa/helper/accessor/ReadAccessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
******************************************************************************/
#pragma once

#include <sofa/helper/accessor/ReadAccessorFixedArray.h>
#include <sofa/helper/accessor/ReadAccessorVector.h>
#include <sofa/type/trait/is_fixed_array.h>

namespace sofa::helper
{
Expand Down Expand Up @@ -70,6 +72,16 @@ class ReadAccessor
const_reference operator* () const { return *vref; }
};

template<class FixedArrayLikeType>
class ReadAccessor<FixedArrayLikeType, std::enable_if_t<sofa::type::trait::is_fixed_array<FixedArrayLikeType>::value>>
: public ReadAccessorFixedArray< FixedArrayLikeType >
{
public:
typedef ReadAccessorFixedArray< FixedArrayLikeType > Inherit;
typedef typename Inherit::container_type container_type;
ReadAccessor(const container_type& c) : Inherit(c) {}
};

template<class VectorLikeType>
class ReadAccessor<VectorLikeType,
std::enable_if_t<sofa::type::trait::is_vector<VectorLikeType>::value> >
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#pragma once

#include <sofa/type/trait/is_fixed_array.h>
#include <iosfwd>

namespace sofa::helper
{
////////////////////////// ReadAccessor for wrapping around fixed array like object //////////////////////
/// ReadAccessor implementation class for fixed array types
template<class T>
class ReadAccessorFixedArray
{
public:
typedef T container_type;
typedef const T const_container_type;
typedef typename container_type::size_type size_type;
typedef typename container_type::value_type value_type;
typedef typename container_type::reference reference;
typedef typename container_type::const_reference const_reference;
typedef typename container_type::iterator iterator;
typedef typename container_type::const_iterator const_iterator;

protected:
const container_type* vref;

public:
ReadAccessorFixedArray(const container_type& container) : vref(&container) {}

bool empty() const { return false; }
size_type size() const { return T::static_size; }
const_reference operator[](size_type i) const { return (*vref)[i]; }

const_iterator begin() const { return vref->begin(); }
const_iterator end() const { return vref->end(); }

///////// Access the container for reading ////////////////
operator const_container_type& () const { return *vref; }
const_container_type* operator->() const { return vref; }
const_container_type& operator* () const { return *vref; }
const_container_type& ref() const { return *vref; } ///< this duplicate operator* (remove ?)
///////////////////////////////////////////////////////////
};

}
12 changes: 12 additions & 0 deletions Sofa/framework/Helper/src/sofa/helper/accessor/WriteAccessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
******************************************************************************/
#pragma once

#include <sofa/helper/accessor/WriteAccessorFixedArray.h>
#include <sofa/helper/accessor/WriteAccessorVector.h>
#include <sofa/type/trait/is_fixed_array.h>

namespace sofa::helper
{
Expand Down Expand Up @@ -81,6 +83,16 @@ class WriteAccessor
}
};

template<class FixedArrayLikeType>
class WriteAccessor<FixedArrayLikeType, std::enable_if_t<sofa::type::trait::is_fixed_array<FixedArrayLikeType>::value>>
: public WriteAccessorFixedArray< FixedArrayLikeType >
{
public:
typedef WriteAccessorFixedArray< FixedArrayLikeType > Inherit;
typedef typename Inherit::container_type container_type;
WriteAccessor(container_type& c) : Inherit(c) {}
};

template<class VectorLikeType>
class WriteAccessor<VectorLikeType,
std::enable_if_t<sofa::type::trait::is_vector<VectorLikeType>::value>>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#pragma once

#include <sofa/helper/config.h>

namespace sofa::helper
{

/// WriteAccessor implementation class for fixed array types
template<class T>
class WriteAccessorFixedArray
{
public:
typedef T container_type;
typedef const T const_container_type;
typedef typename container_type::size_type size_type;
typedef typename container_type::value_type value_type;
typedef typename container_type::reference reference;
typedef typename container_type::const_reference const_reference;
typedef typename container_type::iterator iterator;
typedef typename container_type::const_iterator const_iterator;

protected:
container_type* vref;

public:
WriteAccessorFixedArray(container_type& container) : vref(&container) {}

////// Capacity //////
bool empty() const { return false; }
size_type size() const { return T::static_size; }

////// Element access //////
reference operator[](size_type pos) { return (*vref)[pos]; }
const_reference operator[](size_type pos) const { return (*vref)[pos]; }

reference front() { return vref->front(); }
const_reference front() const { return vref->front(); }

reference back() { return vref->back(); }
const_reference back() const { return vref->back(); }


////// Iterators //////
const_iterator begin() const { return vref->begin(); }
iterator begin() { return vref->begin(); }
const_iterator end() const { return vref->end(); }
iterator end() { return vref->end(); }

////// Access the container in reading & writing //////
operator container_type () { return *vref; }
container_type* operator->() { return vref; }
container_type& operator* () { return *vref; }
container_type& wref() { return *vref; }
///////////////////////////////////////////////////////

///////// Access the container for reading ////////////////
operator const_container_type () const { return *vref; }
const_container_type* operator->() const { return vref; }
const_container_type& operator* () const { return *vref; }

/// this one duplicate operator*
const container_type& ref() const { return *vref; }
///////////////////////////////////////////////////////////
};

}

0 comments on commit 7734dad

Please sign in to comment.