/usr/include/loki/flex/cowstringopt.h is in libloki-dev 0.1.7-3ubuntu1.
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 | ////////////////////////////////////////////////////////////////////////////////
// flex_string
// Copyright (c) 2001 by Andrei Alexandrescu
// Permission to use, copy, modify, distribute and sell this software for any
// purpose is hereby granted without fee, provided that the above copyright
// notice appear in all copies and that both that copyright notice and this
// permission notice appear in supporting documentation.
// The author makes no representations about the
// suitability of this software for any purpose. It is provided "as is"
// without express or implied warranty.
////////////////////////////////////////////////////////////////////////////////
#ifndef COW_STRING_OPT_INC_
#define COW_STRING_OPT_INC_
// $Id: cowstringopt.h 836 2007-09-20 15:51:37Z aandrei $
////////////////////////////////////////////////////////////////////////////////
// class template CowStringOpt
// Implements Copy on Write over any storage
////////////////////////////////////////////////////////////////////////////////
/* This is the template for a storage policy
////////////////////////////////////////////////////////////////////////////////
template <typename E, class A = @>
class StoragePolicy
{
typedef E value_type;
typedef @ iterator;
typedef @ const_iterator;
typedef A allocator_type;
typedef @ size_type;
StoragePolicy(const StoragePolicy& s);
StoragePolicy(const A&);
StoragePolicy(const E* s, size_type len, const A&);
StoragePolicy(size_type len, E c, const A&);
~StoragePolicy();
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
size_type size() const;
size_type max_size() const;
size_type capacity() const;
void reserve(size_type res_arg);
void append(const E* s, size_type sz);
template <class InputIterator>
void append(InputIterator b, InputIterator e);
void resize(size_type newSize, E fill);
void swap(StoragePolicy& rhs);
const E* c_str() const;
const E* data() const;
A get_allocator() const;
};
////////////////////////////////////////////////////////////////////////////////
*/
#include <memory>
#include <algorithm>
#include <functional>
#include <cassert>
#include <limits>
#include <stdexcept>
#include "flex_string_details.h"
////////////////////////////////////////////////////////////////////////////////
// class template CowStringOpt
// Implements Copy on Write over any storage
////////////////////////////////////////////////////////////////////////////////
template <class Storage, typename Align = typename Storage::value_type*>
class CowStringOpt
{
typedef typename Storage::value_type E;
typedef typename flex_string_details::get_unsigned<E>::result RefCountType;
public:
typedef E value_type;
typedef typename Storage::iterator iterator;
typedef typename Storage::const_iterator const_iterator;
typedef typename Storage::allocator_type allocator_type;
typedef typename allocator_type::size_type size_type;
typedef typename Storage::reference reference;
private:
union
{
mutable char buf_[sizeof(Storage)];
Align align_;
};
Storage& Data() const
{ return *reinterpret_cast<Storage*>(buf_); }
RefCountType GetRefs() const
{
const Storage& d = Data();
assert(d.size() > 0);
assert(*d.begin() > 0);
return *d.begin();
}
RefCountType& Refs()
{
Storage& d = Data();
assert(d.size() > 0);
return reinterpret_cast<RefCountType&>(*d.begin());
}
void MakeUnique() const
{
assert(GetRefs() >= 1);
if (GetRefs() == 1) return;
union
{
char buf_[sizeof(Storage)];
Align align_;
} temp;
--(*Data().begin()); // Harmut Kaiser fix:
// decrement the use count of the remaining object
new(buf_) Storage(
*new(temp.buf_) Storage(Data()),
flex_string_details::Shallow());
*Data().begin() = 1;
}
public:
CowStringOpt(const CowStringOpt& s)
{
if (s.GetRefs() == std::numeric_limits<RefCountType>::max())
{
// must make a brand new copy
new(buf_) Storage(s.Data()); // non shallow
Refs() = 1;
}
else
{
new(buf_) Storage(s.Data(), flex_string_details::Shallow());
++Refs();
}
assert(Data().size() > 0);
}
CowStringOpt(const allocator_type& a)
{
new(buf_) Storage(1, 1, a);
}
CowStringOpt(const E* s, size_type len, const allocator_type& a)
{
// Warning - MSVC's debugger has trouble tracing through the code below.
// It seems to be a const-correctness issue
//
new(buf_) Storage(a);
Data().reserve(len + 1);
Data().resize(1, 1);
Data().append(s, s + len);
}
CowStringOpt(size_type len, E c, const allocator_type& a)
{
new(buf_) Storage(len + 1, c, a);
Refs() = 1;
}
CowStringOpt& operator=(const CowStringOpt& rhs)
{
CowStringOpt(rhs).swap(*this);
return *this;
}
~CowStringOpt()
{
assert(Data().size() > 0);
if (--Refs() == 0) Data().~Storage();
}
iterator begin()
{
assert(Data().size() > 0);
MakeUnique();
return Data().begin() + 1;
}
const_iterator begin() const
{
assert(Data().size() > 0);
return Data().begin() + 1;
}
iterator end()
{
MakeUnique();
return Data().end();
}
const_iterator end() const
{
return Data().end();
}
size_type size() const
{
assert(Data().size() > 0);
return Data().size() - 1;
}
size_type max_size() const
{
assert(Data().max_size() > 0);
return Data().max_size() - 1;
}
size_type capacity() const
{
assert(Data().capacity() > 0);
return Data().capacity() - 1;
}
void resize(size_type n, E c)
{
assert(Data().size() > 0);
MakeUnique();
Data().resize(n + 1, c);
}
template <class FwdIterator>
void append(FwdIterator b, FwdIterator e)
{
MakeUnique();
Data().append(b, e);
}
void reserve(size_type res_arg)
{
if (capacity() > res_arg) return;
MakeUnique();
Data().reserve(res_arg + 1);
}
void swap(CowStringOpt& rhs)
{
Data().swap(rhs.Data());
}
const E* c_str() const
{
assert(Data().size() > 0);
return Data().c_str() + 1;
}
const E* data() const
{
assert(Data().size() > 0);
return Data().data() + 1;
}
allocator_type get_allocator() const
{
return Data().get_allocator();
}
};
#endif // COW_STRING_OPT_INC_
|