/usr/include/soci/values.h is in libsoci-dev 3.2.3-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | //
// Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef SOCI_VALUES_H_INCLUDED
#define SOCI_VALUES_H_INCLUDED
#include "statement.h"
#include "into-type.h"
#include "use-type.h"
// std
#include <cstddef>
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
namespace soci
{
namespace details
{
class copy_base
{
public:
virtual ~copy_base() {}
};
template <typename T>
struct copy_holder : public copy_base
{
copy_holder(T const & v) : value_(v) {}
T value_;
};
} // namespace details
class SOCI_DECL values
{
friend class details::statement_impl;
friend class details::into_type<values>;
friend class details::use_type<values>;
public:
values() : row_(NULL), currentPos_(0), uppercaseColumnNames_(false) {}
indicator get_indicator(std::size_t pos) const;
indicator get_indicator(std::string const & name) const;
template <typename T>
T get(std::size_t pos) const
{
if (row_ != NULL)
{
return row_->get<T>(pos);
}
else if (*indicators_[pos] != i_null)
{
return get_from_uses<T>(pos);
}
else
{
std::ostringstream msg;
msg << "Column at position "
<< static_cast<unsigned long>(pos)
<< " contains NULL value and no default was provided";
throw soci_error(msg.str());
}
}
template <typename T>
T get(std::size_t pos, T const & nullValue) const
{
if (row_ != NULL)
{
return row_->get<T>(pos, nullValue);
}
else if (*indicators_[pos] == i_null)
{
return nullValue;
}
else
{
return get_from_uses<T>(pos);
}
}
template <typename T>
T get(std::string const & name) const
{
return row_ != NULL ? row_->get<T>(name) : get_from_uses<T>(name);
}
template <typename T>
T get(std::string const & name, T const & nullValue) const
{
return row_ != NULL
? row_->get<T>(name, nullValue)
: get_from_uses<T>(name, nullValue);
}
template <typename T>
values const & operator>>(T & value) const
{
if (row_ != NULL)
{
// row maintains its own position counter
// which is automatically reset when needed
*row_ >> value;
}
else if (*indicators_[currentPos_] != i_null)
{
// if there is no row object, then the data can be
// extracted from the locally stored use elements,
// but for this the position counter has to be maintained
// as well
value = get_from_uses<T>(currentPos_);
++currentPos_;
}
else
{
std::ostringstream msg;
msg << "Column at position "
<< static_cast<unsigned long>(currentPos_)
<< " contains NULL value and no default was provided";
throw soci_error(msg.str());
}
return *this;
}
void skip(std::size_t num = 1) const
{
if (row_ != NULL)
{
row_->skip(num);
}
else
{
currentPos_ += num;
}
}
void reset_get_counter() const
{
if (row_ != NULL)
{
row_->reset_get_counter();
}
else
{
currentPos_ = 0;
}
}
template <typename T>
void set(std::string const & name, T const & value, indicator indic = i_ok)
{
typedef typename type_conversion<T>::base_type base_type;
if(index_.find(name) == index_.end())
{
index_.insert(std::make_pair(name, uses_.size()));
indicator * pind = new indicator(indic);
indicators_.push_back(pind);
base_type baseValue;
if (indic == i_ok)
{
type_conversion<T>::to_base(value, baseValue, *pind);
}
details::copy_holder<base_type> * pcopy =
new details::copy_holder<base_type>(baseValue);
deepCopies_.push_back(pcopy);
uses_.push_back(new details::use_type<base_type>(
pcopy->value_, *pind, name));
}
else
{
size_t index = index_.find(name)->second;
*indicators_[index] = indic;
if (indic == i_ok)
{
type_conversion<T>::to_base(
value,
static_cast<details::copy_holder<base_type>*>(deepCopies_[index])->value_,
*indicators_[index]);
}
}
}
template <typename T>
void set(const T & value, indicator indic = i_ok)
{
indicator * pind = new indicator(indic);
indicators_.push_back(pind);
typedef typename type_conversion<T>::base_type base_type;
base_type baseValue;
type_conversion<T>::to_base(value, baseValue, *pind);
details::copy_holder<base_type> * pcopy =
new details::copy_holder<base_type>(baseValue);
deepCopies_.push_back(pcopy);
uses_.push_back(new details::use_type<base_type>(
pcopy->value_, *pind));
}
template <typename T>
values & operator<<(T const & value)
{
set(value);
return *this;
}
void uppercase_column_names(bool forceToUpper)
{
uppercaseColumnNames_ = forceToUpper;
}
column_properties const& get_properties(std::size_t pos) const;
column_properties const& get_properties(std::string const &name) const;
private:
//TODO To make values generally usable outside of type_conversion's,
// these should be reference counted smart pointers
row * row_;
std::vector<details::standard_use_type *> uses_;
std::map<details::use_type_base *, indicator *> unused_;
std::vector<indicator *> indicators_;
std::map<std::string, std::size_t> index_;
std::vector<details::copy_base *> deepCopies_;
mutable std::size_t currentPos_;
bool uppercaseColumnNames_;
// When type_conversion::to() is called, a values object is created
// without an underlying row object. In that case, get_from_uses()
// returns the underlying field values
template <typename T>
T get_from_uses(std::string const & name, T const & nullValue) const
{
std::map<std::string, std::size_t>::const_iterator pos = index_.find(name);
if (pos != index_.end())
{
if (*indicators_[pos->second] == i_null)
{
return nullValue;
}
return get_from_uses<T>(pos->second);
}
throw soci_error("Value named " + name + " not found.");
}
template <typename T>
T get_from_uses(std::string const & name) const
{
std::map<std::string, std::size_t>::const_iterator pos = index_.find(name);
if (pos != index_.end())
{
return get_from_uses<T>(pos->second);
}
throw soci_error("Value named " + name + " not found.");
}
template <typename T>
T get_from_uses(std::size_t pos) const
{
details::standard_use_type* u = uses_[pos];
typedef typename type_conversion<T>::base_type base_type;
if (dynamic_cast<details::use_type<base_type> *>(u))
{
base_type const & baseValue = *static_cast<base_type*>(u->get_data());
T val;
indicator ind = *indicators_[pos];
type_conversion<T>::from_base(baseValue, ind, val);
return val;
}
else
{
std::ostringstream msg;
msg << "Value at position "
<< static_cast<unsigned long>(pos)
<< " was set using a different type"
" than the one passed to get()";
throw soci_error(msg.str());
}
}
row& get_row()
{
row_ = new row();
row_->uppercase_column_names(uppercaseColumnNames_);
return * row_;
}
// this is called by Statement::bind(values)
void add_unused(details::use_type_base * u, indicator * i)
{
static_cast<details::standard_use_type *>(u)->convert_to_base();
unused_.insert(std::make_pair(u, i));
}
// this is called by details::into_type<values>::clean_up()
// and use_type<values>::clean_up()
void clean_up()
{
delete row_;
row_ = NULL;
// delete any uses and indicators which were created by set() but
// were not bound by the Statement
// (bound uses and indicators are deleted in Statement::clean_up())
for (std::map<details::use_type_base *, indicator *>::iterator pos =
unused_.begin(); pos != unused_.end(); ++pos)
{
delete pos->first;
delete pos->second;
}
for (std::size_t i = 0; i != deepCopies_.size(); ++i)
{
delete deepCopies_[i];
}
}
};
} // namespace soci
#endif // SOCI_VALUES_H_INCLUDED
|