LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
FieldBase.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 #include <cstdint>
4 #include <limits>
5 
6 #include "boost/format.hpp"
7 #include "boost/preprocessor/seq/for_each.hpp"
8 #include "boost/preprocessor/tuple/to_seq.hpp"
9 
11 #include "lsst/afw/table/Flag.h"
12 
13 namespace lsst {
14 namespace afw {
15 namespace table {
16 
17 namespace {
18 
19 template <typename T>
20 struct TypeTraits;
21 
22 template <>
23 struct TypeTraits<std::uint8_t> {
24  static char const *getName() { return "B"; }
25 };
26 template <>
27 struct TypeTraits<std::uint16_t> {
28  static char const *getName() { return "U"; }
29 };
30 template <>
31 struct TypeTraits<std::int32_t> {
32  static char const *getName() { return "I"; }
33 };
34 template <>
35 struct TypeTraits<std::int64_t> {
36  static char const *getName() { return "L"; }
37 };
38 template <>
39 struct TypeTraits<float> {
40  static char const *getName() { return "F"; }
41 };
42 template <>
43 struct TypeTraits<double> {
44  static char const *getName() { return "D"; }
45 };
46 template <>
47 struct TypeTraits<lsst::geom::Angle> {
48  static char const *getName() { return "Angle"; }
49 };
50 
51 } // namespace
52 
53 //----- POD scalars -----------------------------------------------------------------------------------------
54 
55 template <typename T>
57  return TypeTraits<T>::getName();
58 }
59 
60 //----- POD array -------------------------------------------------------------------------------------------
61 
62 template <typename U>
63 std::string FieldBase<Array<U> >::getTypeString() {
64  return (boost::format("Array%s") % TypeTraits<U>::getName()).str();
65 }
66 
67 //----- String ----------------------------------------------------------------------------------------------
68 
70  if (size < 0)
72  "Size must be provided when constructing a string field.");
73 }
74 
76 
77 std::string FieldBase<std::string>::getValue(Element const *p, ndarray::Manager::Ptr const &m) const {
78  if (isVariableLength()) {
79  // p is a pointer to a std::string; return a copy
80  return std::string(*reinterpret_cast<std::string const *>(p));
81  } else {
82  // p is a char * that is null-terminated only if the string has fewer than _size chars;
83  // return a copy as a std::string
84  Element const *end = p + _size;
85  end = std::find(p, end, 0);
86  return std::string(p, end);
87  }
88 }
89 
90 void FieldBase<std::string>::setValue(Element *p, ndarray::Manager::Ptr const &,
91  std::string const &value) const {
92  if (isVariableLength()) {
93  // p is a pointer to a std::string; replace its contents with a copy of `value`
94  *reinterpret_cast<std::string *>(p) = value;
95  } else {
96  // copy the contents of `value` to p through p + _size, null extra characters, if any
97  if (value.size() > std::size_t(_size)) {
98  throw LSST_EXCEPT(
100  (boost::format("String (%d) is too large for field (%d).") % value.size() % _size).str());
101  }
102  std::copy(value.begin(), value.end(), p);
103  std::fill(p + value.size(), p + _size, char(0)); // null extra characters, if any
104  }
105 }
106 
107 //----- Explicit instantiation ------------------------------------------------------------------------------
108 
109 #define INSTANTIATE_FIELD_BASE(r, data, elem) template struct FieldBase<elem>;
110 
112  BOOST_PP_TUPLE_TO_SEQ(AFW_TABLE_FIELD_TYPE_N, AFW_TABLE_FIELD_TYPE_TUPLE))
113 } // namespace table
114 } // namespace afw
115 } // namespace lsst
int end
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
#define INSTANTIATE_FIELD_BASE(r, data, elem)
Definition: FieldBase.cc:109
int m
Definition: SpanSet.cc:49
T begin(T... args)
A class representing an angle.
Definition: Angle.h:127
Reports attempts to exceed implementation-defined length limits for some classes.
Definition: Runtime.h:76
T copy(T... args)
T end(T... args)
T fill(T... args)
T find(T... args)
std::string const & getName() const noexcept
Return a filter's name.
Definition: Filter.h:78
BOOST_PP_SEQ_FOR_EACH(INSTANTIATE_COLUMNVIEW_SCALAR, _, BOOST_PP_TUPLE_TO_SEQ(AFW_TABLE_SCALAR_FIELD_TYPE_N, AFW_TABLE_SCALAR_FIELD_TYPE_TUPLE)) BOOST_PP_SEQ_FOR_EACH(INSTANTIATE_COLUMNVIEW_ARRAY
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
A base class for image defects.
STL namespace.
T size(T... args)
table::Key< table::Array< int > > _size
Definition: PsfexPsf.cc:364
char Element
the type of subfields and array elements
Definition: FieldBase.h:233
Field base class default implementation (used for numeric scalars and lsst::geom::Angle).
Definition: FieldBase.h:43
static std::string getTypeString()
Return a string description of the field type.
Definition: FieldBase.cc:56
Value getValue(Element const *p, ndarray::Manager::Ptr const &) const
Used to implement BaseRecord::get.
Definition: FieldBase.h:83
void setValue(Element *p, ndarray::Manager::Ptr const &, Value v) const
Used to implement BaseRecord::set.
Definition: FieldBase.h:86
#define AFW_TABLE_FIELD_TYPE_N
Definition: types.h:39
#define AFW_TABLE_FIELD_TYPE_TUPLE
Definition: types.h:44