Files

388 lines
7.0 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 <cstddef>
#include <cstring>
#include <string>
#include <stdexcept>
#include <algorithm>
#include <ostream>
#include <OnixS/CME/MDH/Bootstrap.h>
ONIXS_CMEMDH_NAMESPACE_BEGIN
/// Character type alias.
typedef char Char;
/// Provides efficient way of accessing text-based FIX field values.
ONIXS_CMEMDH_LTWT_CLASS StrRef
{
public:
/// STL-like iterator.
typedef const Char* Iterator;
/// Initializes blank instance.
StrRef()
: items_(0)
, size_(0)
{
}
/// Full initialization.
StrRef(
const Char* chars,
size_t size)
: items_(chars)
, size_(size)
{
}
/// Initializes as clone of other instance.
/// Data referenced by clone is not copied.
/// Instead both instances will refer to same
/// text segment.
StrRef(
const StrRef& other)
: items_(other.items_)
, size_(other.size_)
{
}
/// Indicates whether array of zero length.
bool empty() const
{
return (0 == size_);
}
/// Read-only content.
const Char* items() const
{
return items_;
}
/// Number of chars.
size_t size() const
{
return size_;
}
/// STL-like begin().
Iterator begin() const
{
return items_;
}
/// STL-like end().
Iterator end() const
{
return (items_ + size_);
}
/// Resets reference to nothing.
void reset()
{
reset(0, 0);
}
/// Updates data being referenced.
void
reset(
const Char* chars,
size_t size)
{
items_ = chars;
size_ = size;
}
const Char&
operator [](
size_t index) const
{
return items_[index];
}
const Char&
at(
size_t index) const
{
if (index < size_)
return items_[index];
throw std::invalid_argument("index");
}
/// Reinitializes from another instance.
StrRef&
operator =(
const StrRef& other)
{
items_ = other.items_;
size_ = other.size_;
return *this;
}
/// Swaps content with other instance.
void
swap(
StrRef& other)
{
std::swap(items_, other.items_);
std::swap(size_, other.size_);
}
private:
/// Items being referenced.
const Char* items_;
/// Number of chars being referenced.
size_t size_;
};
/// Constructs StrRef instance over std::string content.
inline
StrRef
toStrRef(
const std::string& str)
{
return StrRef(str.data(), str.size());
}
/// Initializes instance from zero-terminated/C-like string.
inline
StrRef
toStrRef(
const Char* cStr)
{
return
StrRef(
cStr,
cStr
? strlen(cStr)
: 0
);
}
/// Constructs std::string instance from StrRef one.
inline
std::string
toStr(
const StrRef& ref)
{
return std::string(ref.items(), ref.size());
}
/// Appends text referenced by StrRef to given std::string instance.
inline
void
toStr(
std::string& str,
const StrRef& ref)
{
str.append(ref.items(), ref.size());
}
/// Constructs std::string from a character.
inline
std::string
toStr(
Char character)
{
return std::string(1, character);
}
/// Appends character to given std::string instance.
inline
void
toStr(
std::string& str,
Char character)
{
str.append(1, character);
}
/// Appends one string another one.
inline
void
toStr(
std::string& str,
const std::string& value)
{
str.append(value);
}
/// Compares StrRef instance with another one.
inline
bool
operator ==(
const StrRef& left,
const StrRef& right)
{
return (
left.size() == right.size()
&&
0 == memcmp(
left.items(), right.items(), left.size()));
}
/// Compares with another instance.
inline
bool
operator !=(
const StrRef& left,
const StrRef& right)
{
return !(left == right);
}
/// Compares StrRef with std::string.
inline
bool
operator ==(
const StrRef& ref,
const std::string& str)
{
return ref == toStrRef(str);
}
/// Compares StrRef with std::string.
inline
bool
operator !=(
const StrRef& ref,
const std::string& str)
{
return ref != toStrRef(str);
}
/// Compares StrRef with std::string.
inline
bool
operator ==(
const std::string& str,
const StrRef& ref)
{
return ref == toStrRef(str);
}
/// Compares StrRef with std::string.
inline
bool
operator !=(
const std::string& str,
const StrRef& ref)
{
return ref != toStrRef(str);
}
/// Compares StrRef with zero-terminated/C-like string.
inline
bool
operator ==(
const StrRef& ref,
const Char* str)
{
return ref == toStrRef(str);
}
/// Compares StrRef with zero-terminated/C-like string.
inline
bool
operator !=(
const StrRef& ref,
const Char* str)
{
return ref != toStrRef(str);
}
/// Compares StrRef with zero-terminated/C-like string.
inline
bool
operator ==(
const Char* str,
const StrRef& ref)
{
return ref == toStrRef(str);
}
/// Compares StrRef with zero-terminated/C-like string.
inline
bool
operator !=(
const Char* str,
const StrRef& ref)
{
return ref != toStrRef(str);
}
/// Establishes order over string refs.
inline
bool
operator <(
const StrRef& left,
const StrRef& right)
{
ptrdiff_t
compareResult =
memcmp(
left.items(),
right.items(),
left.size() < right.size()
? left.size()
: right.size());
if (0 == compareResult)
{
compareResult =
static_cast<ptrdiff_t>(
left.size() - right.size());
}
return (0 > compareResult);
}
/// Establishes order over string refs.
inline
bool
operator >(
const StrRef& left,
const StrRef& right)
{
return (right < left);
}
/// StrRef serialization operator.
inline
std::ostream&
operator <<(
std::ostream& stream,
const StrRef& text)
{
stream.write(text.items(), text.size());
return stream;
}
ONIXS_CMEMDH_NAMESPACE_END