// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tools.DotNETCommon.Perforce
{
///
/// Base class for returning untyped response data
///
public class PerforceResponse
{
///
/// Stores the response data
///
private object InternalData;
///
/// Constructor
///
/// The response data
public PerforceResponse(object Data)
{
this.InternalData = Data;
}
///
/// True if the response is successful
///
public bool Succeeded
{
get { return !(InternalData is PerforceError); }
}
///
/// True if the response is an error
///
public bool Failed
{
get { return InternalData is PerforceError; }
}
///
/// Accessor for the succcessful response data. Throws an exception if the response is an error.
///
public object Data
{
get
{
RequireSuccess();
return InternalData;
}
}
///
/// Returns the info data.
///
public PerforceInfo Info
{
get
{
RequireSuccess();
return InternalData as PerforceInfo;
}
}
///
/// Returns the error data, or null if this is a succesful response.
///
public PerforceError Error
{
get { return InternalData as PerforceError; }
}
///
/// Throws an exception if the response is an error
///
public void RequireSuccess()
{
PerforceError Error = InternalData as PerforceError;
if(Error != null)
{
throw new PerforceException(Error);
}
}
///
/// Returns a string representation of this object for debugging
///
/// String representation of the object for debugging
public override string ToString()
{
return InternalData.ToString();
}
}
///
/// Represents a successful Perforce response of the given type, or an error. Throws a PerforceException with the error
/// text if the response value is attempted to be accessed and an error has occurred.
///
/// Type of data returned on success
public class PerforceResponse : PerforceResponse
{
///
/// Constructor
///
/// The successful response data
public PerforceResponse(T Data)
: base(Data)
{
}
///
/// Constructor
///
/// The info data
public PerforceResponse(PerforceInfo Info)
: base(Info)
{
}
///
/// Constructor
///
/// The error data
public PerforceResponse(PerforceError Error)
: base(Error)
{
}
///
/// Construct a typed response from an untyped response
///
/// The untyped response
public PerforceResponse(PerforceResponse UntypedResponse)
: base(UntypedResponse.Error ?? UntypedResponse.Info ?? (object)(T)UntypedResponse.Data)
{
}
///
/// Accessor for the succcessful response data. Throws an exception if the response is an error.
///
public new T Data
{
get { return (T)base.Data; }
}
}
}