157 lines
3.6 KiB
C++
157 lines
3.6 KiB
C++
/*
|
|
* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
|
*
|
|
* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
|
|
* and international copyright treaties.
|
|
*
|
|
* Access to and use of the software is governed by the terms of the applicable OnixS Software
|
|
* Services Agreement (the Agreement) and Customer end user license agreements granting
|
|
* a non-assignable, non-transferable and non-exclusive license to use the software
|
|
* for it's own data processing purposes under the terms defined in the Agreement.
|
|
*
|
|
* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
|
|
* of this source code or associated reference material to any other location for further reproduction
|
|
* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
|
|
*
|
|
* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
|
|
* the terms of the Agreement is a violation of copyright law.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <OnixS/ICE/iMpact/MarketData/ABI.h>
|
|
|
|
#include <new>
|
|
|
|
namespace OnixS { namespace ICE { namespace iMpact { namespace MarketData {
|
|
|
|
template <typename T>
|
|
class Optional
|
|
{
|
|
public:
|
|
/// Uninitialized instance.
|
|
Optional() : init_(false) {}
|
|
|
|
/// Constructs the optional object.
|
|
Optional(const T& val) : init_(true)
|
|
{
|
|
construct(val);
|
|
}
|
|
|
|
/**
|
|
* Initializes the contained value by copying the contained value of other,
|
|
* but only if other is engaged. other is still in engaged state if it was
|
|
* in engaged state before the call.
|
|
*/
|
|
Optional(const Optional<T>& opt)
|
|
: init_(opt.init_)
|
|
{
|
|
if (init_)
|
|
construct(opt.get());
|
|
}
|
|
|
|
/// Assigns contents.
|
|
Optional& operator=(const Optional<T>& opt)
|
|
{
|
|
destruct();
|
|
|
|
if ((init_ = opt.init_))
|
|
construct(opt.get());
|
|
|
|
return *this;
|
|
}
|
|
|
|
/// Assigns contents.
|
|
Optional& operator=(const T& val)
|
|
{
|
|
destruct();
|
|
|
|
init_ = true;
|
|
|
|
construct(val);
|
|
|
|
return *this;
|
|
}
|
|
|
|
/// If engaged, destroys the contained value.
|
|
~Optional()
|
|
{
|
|
destruct();
|
|
}
|
|
|
|
/// Checks whether the object is in engaged state
|
|
operator bool() const
|
|
{
|
|
return init_;
|
|
}
|
|
|
|
/// Returns a reference to the contained value.
|
|
T& operator*()
|
|
{
|
|
return get(); // undefined if not initialized
|
|
}
|
|
|
|
/// Returns a const reference to the contained value.
|
|
const T& operator*() const
|
|
{
|
|
return get(); // undefined if not initialized
|
|
}
|
|
|
|
/// A reference to the contained value.
|
|
T& value()
|
|
{
|
|
return **this;
|
|
}
|
|
|
|
/// A const reference to the contained value.
|
|
const T& value() const
|
|
{
|
|
return **this;
|
|
}
|
|
|
|
/// Reset stored value and set state to Uninitialized
|
|
void reset()
|
|
{
|
|
destruct();
|
|
|
|
init_ = false;
|
|
}
|
|
|
|
protected:
|
|
void* storage()
|
|
{
|
|
return data_;
|
|
}
|
|
|
|
const void* storage() const
|
|
{
|
|
return data_;
|
|
}
|
|
|
|
T& get()
|
|
{
|
|
return *static_cast<T*>(storage());
|
|
}
|
|
|
|
const T& get() const
|
|
{
|
|
return *static_cast<const T*>(storage());
|
|
};
|
|
|
|
void destruct()
|
|
{
|
|
if (init_) get().~T();
|
|
init_ = false;
|
|
}
|
|
|
|
void construct(const T& data)
|
|
{
|
|
new (storage()) T(data);
|
|
}
|
|
|
|
char data_[sizeof(T)];
|
|
bool init_;
|
|
};
|
|
|
|
}}}} // namespace MarketData, iMpact, ICE, OnixS
|