Skip to content

Commit

Permalink
merge in develop/SIDD-3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith authored and Dan Smith committed Dec 8, 2021
1 parent 1e9c2cc commit c56ac71
Show file tree
Hide file tree
Showing 6 changed files with 427 additions and 235 deletions.
115 changes: 6 additions & 109 deletions externals/coda-oss/modules/c++/mem/include/mem/ScopedCloneablePtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@
*
*/

#ifndef __MEM_SCOPED_CLONEABLE_PTR_H__
#define __MEM_SCOPED_CLONEABLE_PTR_H__
#ifndef CODA_OSS_mem_ScopedCloneablePtr_h_INCLUDED_
#define CODA_OSS_mem_ScopedCloneablePtr_h_INCLUDED_
#pragma once

#include <memory>
#include <cstddef>

#include "sys/Conf.h"
#include "mem/ScopedPtr.h"

namespace mem
{
Expand All @@ -50,108 +47,8 @@ namespace mem
* (if all the other member variables are POD or have correct
* copy constructors / assignment operators).
*/
template <class T>
class ScopedCloneablePtr
{
std::unique_ptr<T> mPtr;

public:
explicit ScopedCloneablePtr(T* ptr = nullptr)
{
reset(ptr);
}

explicit ScopedCloneablePtr(std::unique_ptr<T>&& ptr)
{
reset(std::move(ptr));
}
#if CODA_OSS_autoptr_is_std // std::auto_ptr removed in C++17
explicit ScopedCloneablePtr(mem::auto_ptr<T> ptr)
{
reset(ptr);
}
#endif

ScopedCloneablePtr(const ScopedCloneablePtr& rhs)
{
*this = rhs;
}

const ScopedCloneablePtr&
operator=(const ScopedCloneablePtr& rhs)
{
if (this != &rhs)
{
auto rhs_ptr = rhs.get();
if (rhs_ptr != nullptr)
{
reset(rhs_ptr->clone());
}
else
{
reset();
}
}

return *this;
}

ScopedCloneablePtr(ScopedCloneablePtr&&) = default;
ScopedCloneablePtr& operator=(ScopedCloneablePtr&&) = default;

bool operator==(const ScopedCloneablePtr<T>& rhs) const
{
auto ptr = get();
auto rhs_ptr = rhs.get();
if (ptr == nullptr && rhs_ptr == nullptr)
{
return true;
}

if (ptr == nullptr || rhs_ptr == nullptr)
{
return false;
}

return *ptr == *rhs_ptr;
}

bool operator!=(const ScopedCloneablePtr<T>& rhs) const
{
return !(*this == rhs);
}

T* get() const
{
return mPtr.get();
}

T& operator*() const
{
return *get();
}

T* operator->() const
{
return get();
}

void reset(T* ptr = nullptr)
{
mPtr.reset(ptr);
}

void reset(std::unique_ptr<T>&& ptr)
{
mPtr = std::move(ptr);
}
#if CODA_OSS_autoptr_is_std // std::auto_ptr removed in C++17
void reset(mem::auto_ptr<T> ptr)
{
reset(std::unique_ptr<T>(ptr.release()));
}
#endif
};
template <typename T>
using ScopedCloneablePtr = ScopedPtr<T, std::true_type /*CopyIsClone*/>;
}

#endif
#endif // CODA_OSS_mem_ScopedCloneablePtr_h_INCLUDED_
121 changes: 6 additions & 115 deletions externals/coda-oss/modules/c++/mem/include/mem/ScopedCopyablePtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,11 @@
*
*/

#ifndef __MEM_SCOPED_COPYABLE_PTR_H__
#define __MEM_SCOPED_COPYABLE_PTR_H__
#ifndef CODA_OSS_mem_ScopedCopyablePtr_h_INCLUDED_
#define CODA_OSS_mem_ScopedCopyablePtr_h_INCLUDED_
#pragma once

#include <memory>
#include <cstddef>
#include <config/coda_oss_config.h>

#include "sys/Conf.h"
#include "mem/SharedPtr.h"
#include "mem/ScopedPtr.h"

namespace mem
{
Expand All @@ -52,113 +47,9 @@ namespace mem
* (if all the other member variables are POD or have correct
* copy constructors / assignment operators).
*/
template <class T>
class ScopedCopyablePtr
{
std::unique_ptr<T> mPtr;

public:
explicit ScopedCopyablePtr(T* ptr = nullptr)
{
reset(ptr);
}
explicit ScopedCopyablePtr(std::unique_ptr<T>&& ptr)
{
reset(std::move(ptr));
}
#if CODA_OSS_autoptr_is_std // std::auto_ptr removed in C++17
explicit ScopedCopyablePtr(mem::auto_ptr<T> ptr)
{
reset(ptr);
}
#endif

ScopedCopyablePtr(const ScopedCopyablePtr& rhs)
{
*this = rhs;
}

const ScopedCopyablePtr&
operator=(const ScopedCopyablePtr& rhs)
{
if (this != &rhs)
{
auto rhs_ptr = rhs.get();
if (rhs_ptr != nullptr)
{
reset(make::unique<T>(*rhs_ptr));
}
else
{
reset();
}
}

return *this;
}

ScopedCopyablePtr(ScopedCopyablePtr&&) = default;
ScopedCopyablePtr& operator=(ScopedCopyablePtr&&) = default;

bool operator==(const ScopedCopyablePtr<T>& rhs) const
{
auto ptr = get();
auto rhs_ptr = rhs.get();
if (ptr == nullptr && rhs_ptr == nullptr)
{
return true;
}

if (ptr == nullptr || rhs_ptr == nullptr)
{
return false;
}

return *ptr == *rhs_ptr;
}

bool operator!=(const ScopedCopyablePtr<T>& rhs) const
{
return !(*this == rhs);
}

// explicit operators not supported until C++11
explicit operator bool() const
{
return get() == nullptr ? false : true;
}

T* get() const
{
return mPtr.get();
}

T& operator*() const
{
return *get();
}

T* operator->() const
{
return get();
}

void reset(T* ptr = nullptr)
{
mPtr.reset(ptr);
}
template <typename T>
using ScopedCopyablePtr = ScopedPtr<T, std::false_type /*CopyIsClone*/>;

void reset(std::unique_ptr<T>&& ptr)
{
mPtr = std::move(ptr);
}
#if CODA_OSS_autoptr_is_std // std::auto_ptr removed in C++17
void reset(mem::auto_ptr<T> ptr)
{
reset(std::unique_ptr<T>(ptr.release()));
}
#endif
};
}

#endif
#endif // CODA_OSS_mem_ScopedCopyablePtr_h_INCLUDED_
Loading

0 comments on commit c56ac71

Please sign in to comment.