整理代码

This commit is contained in:
xiongziliang
2019-09-20 10:37:41 +08:00
parent 5ad4103ce0
commit 9735d891da
2 changed files with 161 additions and 149 deletions

View File

@@ -76,8 +76,8 @@ inline void AMFValue::init() {
}
}
AMFValue::AMFValue(AMFType type) :
_type(type) {
AMFValue::AMFValue() :
_type(AMF_NULL) {
init();
}
@@ -161,6 +161,120 @@ AMFValue& AMFValue::operator =(AMFValue &&from) {
}
void AMFValue::clear() {
switch (_type) {
case AMF_STRING:
_value.string->clear();
break;
case AMF_OBJECT:
case AMF_ECMA_ARRAY:
_value.object->clear();
break;
default:
break;
}
}
AMFType AMFValue::type() const {
return _type;
}
const std::string &AMFValue::as_string() const {
if(_type != AMF_STRING){
throw std::runtime_error("AMF not a string");
}
return *_value.string;
}
double AMFValue::as_number() const {
switch (_type) {
case AMF_NUMBER:
return _value.number;
case AMF_INTEGER:
return _value.integer;
case AMF_BOOLEAN:
return _value.boolean;
default:
throw std::runtime_error("AMF not a number");
}
}
int AMFValue::as_integer() const {
switch (_type) {
case AMF_NUMBER:
return _value.number;
case AMF_INTEGER:
return _value.integer;
case AMF_BOOLEAN:
return _value.boolean;
default:
throw std::runtime_error("AMF not a integer");
}
}
bool AMFValue::as_boolean() const {
switch (_type) {
case AMF_NUMBER:
return _value.number;
case AMF_INTEGER:
return _value.integer;
case AMF_BOOLEAN:
return _value.boolean;
default:
throw std::runtime_error("AMF not a boolean");
}
}
const AMFValue& AMFValue::operator[](const char *str) const {
if (_type != AMF_OBJECT && _type != AMF_ECMA_ARRAY) {
throw std::runtime_error("AMF not a object");
}
auto i = _value.object->find(str);
if (i == _value.object->end()) {
static AMFValue val(AMF_NULL);
return val;
}
return i->second;
}
void AMFValue::object_for_each(const function<void(const string &key, const AMFValue &val)> &fun) const {
if (_type != AMF_OBJECT && _type != AMF_ECMA_ARRAY) {
throw std::runtime_error("AMF not a object");
}
for (auto & pr : *(_value.object)) {
fun(pr.first, pr.second);
}
}
AMFValue::operator bool() const{
return _type != AMF_NULL;
}
void AMFValue::set(const std::string &s, const AMFValue &val) {
if (_type != AMF_OBJECT && _type != AMF_ECMA_ARRAY) {
throw std::runtime_error("AMF not a object");
}
_value.object->emplace(s, val);
}
void AMFValue::add(const AMFValue &val) {
if (_type != AMF_STRICT_ARRAY) {
throw std::runtime_error("AMF not a array");
}
assert(_type == AMF_STRICT_ARRAY);
_value.array->push_back(val);
}
const AMFValue::mapType &AMFValue::getMap() const {
if (_type != AMF_OBJECT && _type != AMF_ECMA_ARRAY) {
throw std::runtime_error("AMF not a object");
}
return *_value.object;
}
const AMFValue::arrayType &AMFValue::getArr() const {
if (_type != AMF_STRICT_ARRAY) {
throw std::runtime_error("AMF not a array");
}
return *_value.array;
}
///////////////////////////////////////////////////////////////////////////
enum {
@@ -316,6 +430,14 @@ void AMFEncoder::write_key(const std::string& s) {
buf += s;
}
void AMFEncoder::clear() {
buf.clear();
}
const std::string& AMFEncoder::data() const {
return buf;
}
//////////////////Decoder//////////////////
uint8_t AMFDecoder::front() {
@@ -551,3 +673,8 @@ AMFValue AMFDecoder::load_arr() {
}*/
return object;
}
AMFDecoder::AMFDecoder(const std::string &buf_in, size_t pos_in, int version_in) :
buf(buf_in), pos(pos_in), version(version_in) {
}