Feature request: Support checkpointing and logging of std::array #480

Added std::array as an STL type Trick recognizes.  We can now create io code
that can save and restore std::arrays like std::vectors and other similar
types. Like other STL types, an std::array cannot be logged or viewed in
trick-tv.
This commit is contained in:
Alex Lin 2017-09-27 16:01:52 -05:00
parent a1ee0e93da
commit de012dde71
7 changed files with 120 additions and 4 deletions

View File

@ -5,6 +5,7 @@
#ifndef CHECKPOINT_FWD_DECLARE
#define CHECKPOINT_FWD_DECLARE
#include <array>
#include <vector>
#include <list>
#include <deque>
@ -23,6 +24,15 @@
// Checkpoint routines
// array
template <typename ITEM_TYPE, std::size_t N,
typename std::enable_if<is_stl_container<ITEM_TYPE>::value>::type* = nullptr >
int checkpoint_stl(std::array<ITEM_TYPE,N> & in_stl , std::string object_name , std::string var_name ) ;
template <typename ITEM_TYPE, std::size_t N,
typename std::enable_if<!is_stl_container<ITEM_TYPE>::value>::type* = nullptr >
int checkpoint_stl(std::array<ITEM_TYPE,N> & in_stl , std::string object_name , std::string var_name ) ;
// vector
template <typename ITEM_TYPE, typename _Alloc,
typename std::enable_if<is_stl_container<ITEM_TYPE>::value>::type* = nullptr >
@ -164,6 +174,15 @@ int checkpoint_stl(std::stack<ITEM_TYPE, _Sequence> & in_stl , std::string objec
// Restore routines
// array
template <typename ITEM_TYPE, std::size_t N,
typename std::enable_if<is_stl_container<ITEM_TYPE>::value>::type* = nullptr >
int restore_stl(std::array<ITEM_TYPE,N> & in_stl , std::string object_name , std::string var_name ) ;
template <typename ITEM_TYPE, std::size_t N,
typename std::enable_if<!is_stl_container<ITEM_TYPE>::value>::type* = nullptr >
int restore_stl(std::array<ITEM_TYPE,N> & in_stl , std::string object_name , std::string var_name ) ;
// vector
template <typename ITEM_TYPE, typename _Alloc,
typename std::enable_if<is_stl_container<ITEM_TYPE>::value>::type* = nullptr >

View File

@ -5,6 +5,7 @@
#ifndef CHECKPOINT_IS_STL_CONTAINER
#define CHECKPOINT_IS_STL_CONTAINER
#include <array>
#include <vector>
#include <list>
#include <deque>
@ -19,6 +20,11 @@ struct is_stl_container {
static const bool value = false;
};
template <typename T,std::size_t N>
struct is_stl_container<std::array<T,N> > {
static const bool value = true;
};
template <typename T,typename _Alloc>
struct is_stl_container<std::vector<T,_Alloc> > {
static const bool value = true;

View File

@ -7,6 +7,7 @@
#define CHECKPOINT_SEQUENCE_STL_HH
#include <string>
#include <array>
#include <vector>
#include <list>
#include <deque>
@ -101,6 +102,23 @@ int checkpoint_sequence_s(STL & in_stl , std::string object_name , std::string v
return 0 ;
}
// -----------
// std::array
// This template is only enabled if the items in the array are an STL
template <typename ITEM_TYPE, std::size_t N,
typename std::enable_if<is_stl_container<ITEM_TYPE>::value>::type* >
int checkpoint_stl(std::array<ITEM_TYPE,N> & in_stl , std::string object_name , std::string var_name ) {
return checkpoint_sequence_s( in_stl , object_name , var_name ) ;
}
// This template is only enabled if the items in the array are NOT an STL, except for std::string
template <typename ITEM_TYPE, std::size_t N,
typename std::enable_if<!is_stl_container<ITEM_TYPE>::value>::type* >
int checkpoint_stl(std::array<ITEM_TYPE,N> & in_stl , std::string object_name , std::string var_name ) {
return checkpoint_sequence_i( in_stl , object_name , var_name ) ;
}
// -----------
// std::vector
@ -196,6 +214,11 @@ int delete_sequence_alloc(STL & in_stl __attribute__ ((unused)), std::string obj
return 0 ;
}
template <class ITEM_TYPE, std::size_t N>
int delete_stl(std::array<ITEM_TYPE,N> & in_stl , std::string object_name , std::string var_name ) {
return delete_sequence_alloc( in_stl , object_name , var_name ) ;
}
template <class ITEM_TYPE, typename _Alloc>
int delete_stl(std::vector<ITEM_TYPE,_Alloc> & in_stl , std::string object_name , std::string var_name ) {
return delete_sequence_alloc( in_stl , object_name , var_name ) ;
@ -289,6 +312,69 @@ int restore_sequence_s(STL & in_stl , std::string object_name , std::string var_
return 0 ;
}
// -----------
// std::array
// This template is only enabled if the items in the array are an STL
template <typename ITEM_TYPE, std::size_t N,
typename std::enable_if<is_stl_container<ITEM_TYPE>::value>::type* >
int restore_stl(std::array<ITEM_TYPE,N> & in_stl , std::string object_name , std::string var_name ) {
unsigned int ii ;
unsigned int cont_size ;
REF2 * items_ref ;
std::string * items ;
std::replace_if(object_name.begin(), object_name.end(), std::ptr_fun<int,int>(&std::ispunct), '_');
//message_publish(1, "%s\n", __PRETTY_FUNCTION__) ;
items_ref = ref_attributes((char *)(object_name + "_" + var_name).c_str()) ;
if ( items_ref != NULL ) {
items = (std::string *)items_ref->address ;
cont_size = get_size((char *)items) ;
for ( ii = 0 ; ii < cont_size ; ii++ ) {
typename std::array<ITEM_TYPE,N>::value_type vt ;
std::ostringstream index_string ;
index_string << ii ;
restore_stl(vt, object_name + "_" + var_name , index_string.str()) ;
in_stl[ii] = vt ;
}
delete_stl( in_stl , object_name , var_name ) ;
}
return 0 ;
}
// This template is only enabled if the items in the array are NOT an STL, except for std::string
template <typename ITEM_TYPE, std::size_t N,
typename std::enable_if<!is_stl_container<ITEM_TYPE>::value>::type* >
int restore_stl(std::array<ITEM_TYPE,N> & in_stl , std::string object_name , std::string var_name ) {
unsigned int ii ;
unsigned int cont_size ;
REF2 * items_ref ;
typename std::array<ITEM_TYPE,N>::value_type * items ;
std::replace_if(object_name.begin(), object_name.end(), std::ptr_fun<int,int>(&std::ispunct), '_');
//message_publish(1, "RESTORE_SEQUENCE_STL %s_%s\n", object_name.c_str() , var_name.c_str()) ;
items_ref = ref_attributes((char *)(object_name + std::string("_") + var_name).c_str()) ;
if ( items_ref != NULL ) {
items = (typename std::array<ITEM_TYPE,N>::value_type *)items_ref->address ;
cont_size = get_size((char *)items) ;
for ( ii = 0 ; ii < cont_size ; ii++ ) {
in_stl[ii] = items[ii] ;
}
delete_stl( in_stl , object_name , var_name ) ;
}
return 0 ;
}
// -----------
// std::vector

View File

@ -6,7 +6,7 @@
#ifndef CHECKPOINT_STL_HH
#define CHECKPOINT_STL_HH
// vector, list, deque, set , multiset
// array, vector, list, deque, set, multiset
#include "trick/checkpoint_sequence_stl.hh"
// map, multimap

View File

@ -101,16 +101,18 @@ my $template_var_def = qr/(?:\:\:)?[_A-Za-z][:\w]*\s* # template name
# This list is the list of all STL types to ignore.
my %all_stl_names = qw(vector 1 list 1 deque 1 set 1 multiset 1 map 1 multimap 1 hash_set 1
hash_multiset 1 hash_map 1 hash_multimap 1 stack 1 queue 1 priority_queue 1 bitset 1 auto_ptr 1
array 1
std::vector 1 std::list 1 std::deque 1 std::set 1 std::multiset 1 std::map 1 std::multimap 1 std::hash_set 1
std::hash_multiset 1 std::hash_map 1 std::hash_multimap 1 std::stack 1 std::queue 1 std::priority_queue 1
std::bitset 1 std::auto_ptr 1 pair 1 std::pair 1 std::tr1::shared_ptr 1) ;
std::bitset 1 std::auto_ptr 1 pair 1 std::pair 1 std::tr1::shared_ptr 1 std::array 1) ;
# This is a partial list of STL types to ignore. We do not ignore vector, map, list if we allow STLs
my %stl_names = qw(deque 1 set 1 multiset 1 multimap 1 hash_set 1
hash_multiset 1 hash_map 1 hash_multimap 1 stack 1 queue 1 priority_queue 1 bitset 1 auto_ptr 1
array 1
std::deque 1 std::set 1 std::multiset 1 std::multimap 1 std::hash_set 1
std::hash_multiset 1 std::hash_map 1 std::hash_multimap 1 std::stack 1 std::queue 1 std::priority_queue 1
std::bitset 1 std::auto_ptr 1 pair 1 std::pair 1 std::tr1::shared_ptr 1) ;
std::bitset 1 std::auto_ptr 1 pair 1 std::pair 1 std::tr1::shared_ptr 1 std::array 1) ;
Getopt::Long::Configure ("bundling");
GetOptions ( "stl|s" => sub { $stls = 1 } ,

View File

@ -1 +1 @@
current_version = "17.1.dev"
current_version = "17.2.dev"

View File

@ -301,6 +301,7 @@ bool FieldVisitor::ProcessTemplate(std::string in_name , clang::CXXRecordDecl *
static std::map<std::string, bool> init_stl_classes() {
std::map<std::string, bool> my_map ;
my_map.insert(std::pair<std::string, bool>("std::array", 0)) ;
my_map.insert(std::pair<std::string, bool>("std::deque", 1)) ;
my_map.insert(std::pair<std::string, bool>("std::list", 1)) ;
my_map.insert(std::pair<std::string, bool>("std::map", 1)) ;
@ -312,6 +313,7 @@ static std::map<std::string, bool> init_stl_classes() {
my_map.insert(std::pair<std::string, bool>("std::set", 1)) ;
my_map.insert(std::pair<std::string, bool>("std::stack", 0)) ;
my_map.insert(std::pair<std::string, bool>("std::vector", 1)) ;
my_map.insert(std::pair<std::string, bool>("std::__1::array", 0)) ;
my_map.insert(std::pair<std::string, bool>("std::__1::deque", 1)) ;
my_map.insert(std::pair<std::string, bool>("std::__1::list", 1)) ;
my_map.insert(std::pair<std::string, bool>("std::__1::map", 1)) ;
@ -323,6 +325,7 @@ static std::map<std::string, bool> init_stl_classes() {
my_map.insert(std::pair<std::string, bool>("std::__1::set", 1)) ;
my_map.insert(std::pair<std::string, bool>("std::__1::stack", 0)) ;
my_map.insert(std::pair<std::string, bool>("std::__1::vector", 1)) ;
my_map.insert(std::pair<std::string, bool>("std::__cxx11::array", 0)) ;
my_map.insert(std::pair<std::string, bool>("std::__cxx11::deque", 1)) ;
my_map.insert(std::pair<std::string, bool>("std::__cxx11::list", 1)) ;
my_map.insert(std::pair<std::string, bool>("std::__cxx11::map", 1)) ;