5 #include "boost/shared_ptr.hpp"
36 class Block :
public ndarray::Manager {
38 typedef boost::intrusive_ptr<Block> Ptr;
43 static void reclaim(
std::size_t recordSize,
void *
data, ndarray::Manager::Ptr
const &manager) {
44 Ptr block = boost::static_pointer_cast<Block>(manager);
45 if (
reinterpret_cast<char *
>(
data) + recordSize == block->_next) {
46 block->_next -= recordSize;
53 Ptr block = boost::static_pointer_cast<Block>(manager);
54 if (!block ||
static_cast<std::size_t>(block->_end - block->_next) < recordSize * recordCount) {
55 block = Ptr(
new Block(recordSize, recordCount));
61 Ptr block = boost::static_pointer_cast<Block>(manager);
62 return static_cast<std::size_t>(block->_end - block->_next) / recordSize;
67 static void *get(
std::size_t recordSize, ndarray::Manager::Ptr &manager) {
68 Ptr block = boost::static_pointer_cast<Block>(manager);
69 if (!block || block->_next == block->_end) {
73 void *r = block->_next;
74 block->_next += recordSize;
81 static void padSchema(Schema &
schema) {
82 static int const MIN_RECORD_ALIGN =
sizeof(AllocType);
95 : _mem(new AllocType[(recordSize * recordCount) / sizeof(AllocType)]),
96 _next(reinterpret_cast<char *>(_mem.get())),
97 _end(_next + recordSize * recordCount) {
98 assert((recordSize * recordCount) %
sizeof(AllocType) == 0);
115 return Block::getBufferSize(_schema.
getRecordSize(), _manager);
131 output->assign(input);
137 output->assign(input,
mapper);
142 return std::make_shared<io::FitsWriter>(fitsfile, flags);
150 return constructRecord<BaseRecord>();
154 Block::padSchema(_schema);
165 struct RecordDestroyer {
166 template <
typename T>
169 template <
typename T>
170 void operator()(SchemaItem<Array<T> >
const &item)
const {
171 typedef ndarray::Array<T, 1, 1> Element;
172 if (item.key.isVariableLength()) {
173 (*
reinterpret_cast<Element *
>(
data + item.key.getOffset())).~Element();
177 void operator()(SchemaItem<std::string>
const &item)
const {
178 if (item.key.isVariableLength()) {
181 (*
reinterpret_cast<string *
>(
data + item.key.getOffset())).~string();
190 detail::RecordData BaseTable::_makeNewRecordData() {
192 return detail::RecordData{
199 void BaseTable::_destroy(BaseRecord &record) {
200 assert(record._table.get() ==
this);
201 RecordDestroyer f = {
reinterpret_cast<char *
>(record._data)};
203 if (record._manager == _manager) Block::reclaim(_schema.
getRecordSize(), record._data, _manager);
216 template class CatalogT<BaseRecord>;
217 template class CatalogT<BaseRecord const>;
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Base class for all records.
std::shared_ptr< BaseRecord > makeRecord()
Default-construct an associated record.
virtual std::shared_ptr< BaseRecord > _makeRecord()
Default-construct an associated record (protected implementation).
virtual std::shared_ptr< io::FitsWriter > makeFitsWriter(fits::Fits *fitsfile, int flags) const
void preallocate(std::size_t nRecords)
Allocate contiguous space for new records in advance.
std::shared_ptr< BaseRecord > copyRecord(BaseRecord const &input)
Deep-copy a record, requiring that it have the same schema as this table.
BaseTable(Schema const &schema)
Construct from a schema.
std::size_t getBufferSize() const
Return the number of additional records space has been already been allocated for.
static Schema makeMinimalSchema()
Return a minimal schema for Base tables and records.
static int nRecordsPerBlock
Number of records in each memory block.
static std::shared_ptr< BaseTable > make(Schema const &schema)
Construct a new table.
virtual std::shared_ptr< BaseTable > _clone() const
Clone implementation with noncovariant return types.
Defines the fields and offsets for a table.
void forEach(F &func) const
Apply a functor to each SchemaItem in the Schema.
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
int getRecordSize() const
Return the raw size of a record in bytes.
std::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
A mapping between the keys of two Schemas, used to copy data between them.
static void padSchema(Schema &schema, int bytes)
A base class for image defects.
T shared_from_this(T... args)
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...