LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
LSSTDataManagementBasePackage
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
Field base class default implementation (used for numeric scalars and lsst::geom::Angle).
Definition: FieldBase.h:43
table::Key< table::Array< int > > _size
Definition: PsfexPsf.cc:364
T copy(T... args)
Reports attempts to exceed implementation-defined length limits for some classes. ...
Definition: Runtime.h:76
STL namespace.
T end(T... args)
#define AFW_TABLE_FIELD_TYPE_TUPLE
Definition: types.h:44
STL class.
#define INSTANTIATE_FIELD_BASE(r, data, elem)
Definition: FieldBase.cc:109
A base class for image defects.
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
void setValue(Element *p, ndarray::Manager::Ptr const &, Value v) const
Used to implement BaseRecord::set.
Definition: FieldBase.h:86
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
T find(T... args)
T size(T... args)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
T begin(T... args)
Value getValue(Element const *p, ndarray::Manager::Ptr const &) const
Used to implement BaseRecord::get.
Definition: FieldBase.h:83
int m
Definition: SpanSet.cc:49
lsst::geom::Angle Angle
Definition: misc.h:33
T fill(T... args)
static std::string getTypeString()
Return a string description of the field type.
Definition: FieldBase.cc:56
char Element
the type of subfields and array elements
Definition: FieldBase.h:233
int end
#define AFW_TABLE_FIELD_TYPE_N
Definition: types.h:39