vcl: sfx2: LOK: Support per-view popup windows
[LibreOffice.git] / store / source / storbase.hxx
blobb9dc53247a8df32fd7511cc559856f7ed8735287
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_STORE_SOURCE_STORBASE_HXX
21 #define INCLUDED_STORE_SOURCE_STORBASE_HXX
23 #include <sal/config.h>
24 #include <salhelper/simplereferenceobject.hxx>
26 #include <sal/types.h>
28 #include <rtl/crc.h>
29 #include <rtl/ref.hxx>
31 #include <osl/diagnose.h>
32 #include <osl/endian.h>
34 #include <store/types.h>
36 #include <memory>
37 #include <utility>
39 /** @file store common internals.
42 namespace store
45 #ifdef htons
46 #undef htons
47 #endif
48 #ifdef ntohs
49 #undef ntohs
50 #endif
52 #ifdef htonl
53 #undef htonl
54 #endif
55 #ifdef ntohl
56 #undef ntohl
57 #endif
59 #ifdef OSL_BIGENDIAN
60 inline sal_uInt16 htons (sal_uInt16 h) { return OSL_SWAPWORD(h); }
61 inline sal_uInt16 ntohs (sal_uInt16 n) { return OSL_SWAPWORD(n); }
63 inline sal_uInt32 htonl (sal_uInt32 h) { return OSL_SWAPDWORD(h); }
64 inline sal_uInt32 ntohl (sal_uInt32 n) { return OSL_SWAPDWORD(n); }
65 #else
66 inline sal_uInt16 htons (sal_uInt16 h) { return h; }
67 inline sal_uInt16 ntohs (sal_uInt16 n) { return n; }
69 inline sal_uInt32 htonl (sal_uInt32 h) { return h; }
70 inline sal_uInt32 ntohl (sal_uInt32 n) { return n; }
71 #endif /* OSL_BIGENDIAN */
73 struct OStorePageGuard
75 /** Representation.
77 sal_uInt32 m_nMagic;
78 sal_uInt32 m_nCRC32;
80 /** Construction.
82 explicit OStorePageGuard (sal_uInt32 nMagic = 0)
83 : m_nMagic (store::htonl(nMagic)),
84 m_nCRC32 (store::htonl(0))
87 void swap (OStorePageGuard & rhs)
89 std::swap(m_nMagic, rhs.m_nMagic);
90 std::swap(m_nCRC32, rhs.m_nCRC32);
93 OStorePageGuard (OStorePageGuard const & rhs)
94 : m_nMagic (rhs.m_nMagic),
95 m_nCRC32 (rhs.m_nCRC32)
98 OStorePageGuard& operator= (const OStorePageGuard& rhs)
100 m_nMagic = rhs.m_nMagic;
101 m_nCRC32 = rhs.m_nCRC32;
102 return *this;
105 /** Comparison.
107 bool operator== (const OStorePageGuard& rhs) const
109 return ((m_nMagic == rhs.m_nMagic) &&
110 (m_nCRC32 == rhs.m_nCRC32) );
114 #define STORE_PAGE_NULL (sal_uInt32(~0))
116 struct OStorePageDescriptor
118 /** Representation.
120 sal_uInt32 m_nAddr;
121 sal_uInt16 m_nSize;
122 sal_uInt16 m_nUsed;
124 /** Construction.
126 explicit OStorePageDescriptor (
127 sal_uInt32 nAddr,
128 sal_uInt16 nSize,
129 sal_uInt16 nUsed)
130 : m_nAddr (store::htonl(nAddr)),
131 m_nSize (store::htons(nSize)),
132 m_nUsed (store::htons(nUsed))
135 void swap (OStorePageDescriptor & rhs)
137 std::swap(m_nAddr, rhs.m_nAddr);
138 std::swap(m_nSize, rhs.m_nSize);
139 std::swap(m_nUsed, rhs.m_nUsed);
142 OStorePageDescriptor (const OStorePageDescriptor & rhs)
143 : m_nAddr (rhs.m_nAddr),
144 m_nSize (rhs.m_nSize),
145 m_nUsed (rhs.m_nUsed)
148 OStorePageDescriptor & operator= (const OStorePageDescriptor & rhs)
150 m_nAddr = rhs.m_nAddr;
151 m_nSize = rhs.m_nSize;
152 m_nUsed = rhs.m_nUsed;
153 return *this;
156 /** Comparison.
158 bool operator== (const OStorePageDescriptor & rhs) const
160 return ((m_nAddr == rhs.m_nAddr) &&
161 (m_nSize == rhs.m_nSize) );
165 struct OStorePageKey
167 /** Representation.
169 sal_uInt32 m_nLow;
170 sal_uInt32 m_nHigh;
172 /** Construction.
174 explicit OStorePageKey (sal_uInt32 nLow = 0, sal_uInt32 nHigh = 0)
175 : m_nLow (store::htonl(nLow)),
176 m_nHigh (store::htonl(nHigh))
179 /** Comparison.
181 bool operator== (const OStorePageKey & rhs) const
183 return ((m_nLow == rhs.m_nLow ) &&
184 (m_nHigh == rhs.m_nHigh) );
187 bool operator< (const OStorePageKey & rhs) const
189 if (m_nHigh == rhs.m_nHigh)
190 return (store::ntohl(m_nLow) < store::ntohl(rhs.m_nLow));
191 else
192 return (store::ntohl(m_nHigh) < store::ntohl(rhs.m_nHigh));
196 struct OStorePageLink
198 /** Representation.
200 sal_uInt32 m_nAddr;
202 /** Construction.
204 explicit OStorePageLink (sal_uInt32 nAddr = STORE_PAGE_NULL)
205 : m_nAddr (store::htonl(nAddr))
208 void swap (OStorePageLink & rhs)
210 std::swap(m_nAddr, rhs.m_nAddr);
213 OStorePageLink & operator= (sal_uInt32 nAddr)
215 m_nAddr = store::htonl(nAddr);
216 return *this;
219 /** Comparison.
221 bool operator== (const OStorePageLink & rhs) const
223 return (m_nAddr == rhs.m_nAddr);
226 /** Operation.
228 sal_uInt32 location() const
230 return store::ntohl(m_nAddr);
235 struct PageData
237 typedef OStorePageGuard G;
238 typedef OStorePageDescriptor D;
239 typedef OStorePageLink L;
241 /** Representation.
243 G m_aGuard;
244 D m_aDescr;
245 L m_aMarked;
246 L m_aUnused;
248 /** theSize.
250 static const size_t theSize = sizeof(G) + sizeof(D) + 2 * sizeof(L);
251 static const sal_uInt16 thePageSize = theSize;
252 static_assert(STORE_MINIMUM_PAGESIZE >= thePageSize, "must be at least thePageSize");
254 /** location.
256 sal_uInt32 location() const
258 return store::ntohl(m_aDescr.m_nAddr);
260 void location (sal_uInt32 nAddr)
262 m_aDescr.m_nAddr = store::htonl(nAddr);
265 /** size.
267 sal_uInt16 size() const
269 return store::ntohs(m_aDescr.m_nSize);
272 /** type.
274 sal_uInt32 type() const
276 return store::ntohl(m_aGuard.m_nMagic);
279 /** Allocation.
281 class Allocator_Impl;
282 class Allocator : public virtual salhelper::SimpleReferenceObject
284 public:
285 template< class T > T * construct()
287 void * page = nullptr; sal_uInt16 size = 0;
288 if (allocate (&page, &size))
290 return new(page) T(size);
292 return 0;
295 bool allocate (void ** ppPage, sal_uInt16 * pnSize)
297 allocate_Impl (ppPage, pnSize);
298 return ((*ppPage != nullptr) && (*pnSize != 0));
301 void deallocate (void * pPage)
303 if (pPage != nullptr)
304 deallocate_Impl (pPage);
307 static storeError createInstance (
308 rtl::Reference< PageData::Allocator > & rxAllocator, sal_uInt16 nPageSize);
310 protected:
311 virtual ~Allocator() override {}
313 private:
314 /** Implementation (abstract).
316 virtual void allocate_Impl (void ** ppPage, sal_uInt16 * pnSize) = 0;
317 virtual void deallocate_Impl (void * pPage) = 0;
320 class Deallocate {
321 public:
322 explicit Deallocate(rtl::Reference<Allocator> const & allocator):
323 allocator_(allocator) {};
325 void operator ()(void * page) const { allocator_->deallocate(page); }
327 private:
328 rtl::Reference<Allocator> allocator_;
331 static void* operator new (size_t, void * p) { return p; }
332 static void operator delete (void * , void *) {}
334 /** Construction.
336 explicit PageData (sal_uInt16 nPageSize = thePageSize)
337 : m_aGuard(),
338 m_aDescr(STORE_PAGE_NULL, nPageSize, thePageSize),
339 m_aMarked(),
340 m_aUnused()
343 void swap (PageData & rhs) // nothrow
345 m_aGuard.swap(rhs.m_aGuard);
346 m_aDescr.swap(rhs.m_aDescr);
347 m_aMarked.swap(rhs.m_aMarked);
348 m_aUnused.swap(rhs.m_aUnused);
351 PageData (PageData const & rhs) // nothrow
352 : m_aGuard (rhs.m_aGuard),
353 m_aDescr (rhs.m_aDescr),
354 m_aMarked(rhs.m_aMarked),
355 m_aUnused(rhs.m_aUnused)
358 PageData & operator= (PageData const & rhs) // nothrow
360 PageData tmp (rhs);
361 swap (tmp);
362 return *this;
365 /** guard (external representation).
367 void guard (sal_uInt32 nAddr)
369 sal_uInt32 nCRC32 = rtl_crc32 (0, &m_aGuard.m_nMagic, sizeof(sal_uInt32));
370 m_aDescr.m_nAddr = store::htonl(nAddr);
371 nCRC32 = rtl_crc32 (nCRC32, &m_aDescr, static_cast<sal_uInt32>(theSize - sizeof(G)));
372 m_aGuard.m_nCRC32 = store::htonl(nCRC32);
375 /** verify (external representation).
377 storeError verify (sal_uInt32 nAddr) const
379 sal_uInt32 nCRC32 = rtl_crc32 (0, &m_aGuard.m_nMagic, sizeof(sal_uInt32));
380 nCRC32 = rtl_crc32 (nCRC32, &m_aDescr, static_cast<sal_uInt32>(theSize - sizeof(G)));
381 if (m_aGuard.m_nCRC32 != store::htonl(nCRC32))
382 return store_E_InvalidChecksum;
383 if (m_aDescr.m_nAddr != store::htonl(nAddr))
384 return store_E_InvalidAccess;
385 return store_E_None;
390 template< class T >
391 class PageHolderObject
393 /** Representation.
395 std::shared_ptr<PageData> m_xPage;
397 /** Checked cast.
399 template< class U >
400 static bool isA (PageData const * p)
402 return ((p != nullptr) && (p->type() == U::theTypeId));
405 template< class U >
406 static U * dynamic_page_cast (PageData * p)
408 return isA<U>(p) ? static_cast<U*>(p) : 0;
411 template< class U >
412 static U const * dynamic_page_cast (PageData const * p)
414 return isA<U>(p) ? static_cast<U const *>(p) : 0;
417 public:
418 bool construct (rtl::Reference< PageData::Allocator > const & rxAllocator)
420 if ((m_xPage.get() == nullptr) && rxAllocator.is())
422 std::shared_ptr<PageData> tmp (rxAllocator->construct<T>(), PageData::Deallocate(rxAllocator));
423 m_xPage.swap (tmp);
425 return (m_xPage.get() != nullptr);
428 explicit PageHolderObject (std::shared_ptr<PageData> const & rxPage = std::shared_ptr<PageData>())
429 : m_xPage (rxPage)
432 void swap (PageHolderObject<T> & rhs)
434 m_xPage.swap (rhs.m_xPage);
437 PageHolderObject (PageHolderObject<T> const & rhs)
438 : m_xPage (rhs.m_xPage)
441 PageHolderObject<T> & operator= (PageHolderObject<T> const & rhs)
443 PageHolderObject<T> tmp (rhs);
444 this->swap (tmp);
445 return *this;
448 bool is() const
450 return (m_xPage.get() != nullptr);
453 std::shared_ptr<PageData> & get() { return m_xPage; }
454 std::shared_ptr<PageData> const & get() const { return m_xPage; }
456 T * operator->()
458 T * pImpl = dynamic_page_cast<T>(m_xPage.get());
459 OSL_PRECOND(pImpl != nullptr, "store::PageHolder<T>::operator*(): Null pointer");
460 return pImpl;
463 T const * operator->() const
465 T const * pImpl = dynamic_page_cast<T>(m_xPage.get());
466 OSL_PRECOND(pImpl != nullptr, "store::PageHolder<T>::operator*(): Null pointer");
467 return pImpl;
470 T & operator*()
472 T * pImpl = dynamic_page_cast<T>(m_xPage.get());
473 OSL_PRECOND(pImpl != nullptr, "store::PageHolder<T>::operator*(): Null pointer");
474 return (*pImpl);
477 T const & operator*() const
479 T const * pImpl = dynamic_page_cast<T>(m_xPage.get());
480 OSL_PRECOND(pImpl != nullptr, "store::PageHolder<T>::operator*(): Null pointer");
481 return (*pImpl);
484 static storeError guard (std::shared_ptr<PageData> const & rxPage, sal_uInt32 nAddr)
486 PageData * pHead = rxPage.get();
487 if (!pHead)
488 return store_E_InvalidAccess;
489 pHead->guard(nAddr);
491 T * pImpl = dynamic_page_cast<T>(pHead);
492 OSL_PRECOND(pImpl != nullptr, "store::PageHolder<T>::guard(): Null pointer");
493 pImpl->guard();
495 return store_E_None;
498 static storeError verify (std::shared_ptr<PageData> const & rxPage, sal_uInt32 nAddr)
500 PageData const * pHead = rxPage.get();
501 if (!pHead)
502 return store_E_InvalidAccess;
504 storeError eErrCode = pHead->verify(nAddr);
505 if (eErrCode != store_E_None)
506 return eErrCode;
508 T const * pImpl = dynamic_page_cast<T>(pHead);
509 if (!pImpl)
510 return store_E_WrongVersion;
512 return pImpl->verify();
516 class OStorePageObject
518 typedef PageData page;
520 public:
521 /** Allocation.
523 static void * operator new (size_t n)
525 return std::malloc(sal_uInt32(n));
527 static void operator delete (void * p)
529 std::free (p);
532 /** State.
534 inline bool dirty() const;
535 inline void clean();
536 inline void touch();
538 /** Location.
540 inline sal_uInt32 location() const;
542 protected:
543 /** Representation.
545 std::shared_ptr<PageData> m_xPage;
546 bool m_bDirty;
548 /** Construction.
550 explicit OStorePageObject (std::shared_ptr<PageData> const & rxPage)
551 : m_xPage (rxPage), m_bDirty (false)
554 /** Destruction.
556 virtual ~OStorePageObject();
558 public:
559 template< class U >
560 PageHolderObject<U> makeHolder() const
562 return PageHolderObject<U>(m_xPage);
565 template< class U >
566 storeError construct (rtl::Reference< PageData::Allocator > const & rxAllocator)
568 if (!rxAllocator.is())
569 return store_E_InvalidAccess;
571 std::shared_ptr<PageData> tmp (rxAllocator->construct<U>(), PageData::Deallocate(rxAllocator));
572 if (!tmp.get())
573 return store_E_OutOfMemory;
575 m_xPage.swap (tmp);
576 return store_E_None;
579 std::shared_ptr<PageData> & get() { return m_xPage; }
581 virtual storeError guard (sal_uInt32 nAddr) = 0;
582 virtual storeError verify (sal_uInt32 nAddr) const = 0;
585 inline bool OStorePageObject::dirty() const
587 return m_bDirty;
590 inline void OStorePageObject::clean()
592 m_bDirty = false;
595 inline void OStorePageObject::touch()
597 m_bDirty = true;
600 inline sal_uInt32 OStorePageObject::location() const
602 return m_xPage->location();
605 } // namespace store
607 #endif // INCLUDED_STORE_SOURCE_STORBASE_HXX
609 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */