1/*
2 * Copyright 2010-2018, Rene Gollent, rene@gollent.com
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include "CStringTypeHandler.h"
8
9#include <new>
10
11#include <stdio.h>
12
13#include "CStringValueNode.h"
14#include "Type.h"
15
16
17CStringTypeHandler::~CStringTypeHandler()
18{
19}
20
21
22const char*
23CStringTypeHandler::Name() const
24{
25	return "String";
26}
27
28
29float
30CStringTypeHandler::SupportsType(Type* type) const
31{
32	AddressType* addressType = dynamic_cast<AddressType*>(type);
33	ArrayType* arrayType = dynamic_cast<ArrayType*>(type);
34	PrimitiveType* baseType = NULL;
35	ModifiedType* modifiedType = NULL;
36	if (addressType != NULL && addressType->AddressKind()
37		== DERIVED_TYPE_POINTER) {
38			baseType = dynamic_cast<PrimitiveType*>(
39				addressType->BaseType());
40		if (baseType == NULL) {
41			modifiedType = dynamic_cast<ModifiedType*>(
42				addressType->BaseType());
43		}
44	} else if (arrayType != NULL && arrayType->CountDimensions() == 1) {
45		baseType = dynamic_cast<PrimitiveType*>(
46				arrayType->BaseType());
47		if (baseType == NULL) {
48			modifiedType = dynamic_cast<ModifiedType*>(
49				arrayType->BaseType());
50		}
51	}
52
53	if (baseType == NULL && modifiedType == NULL)
54		return 0.0f;
55	else if (modifiedType != NULL) {
56		baseType = dynamic_cast<PrimitiveType*>(
57			modifiedType->ResolveRawType(false));
58		if (baseType == NULL)
59			return 0.0f;
60	}
61
62	if (baseType->TypeConstant() == B_UINT8_TYPE
63		|| baseType->TypeConstant() == B_INT8_TYPE)
64		return 0.8f;
65
66	return 0.0f;
67}
68
69
70status_t
71CStringTypeHandler::CreateValueNode(ValueNodeChild* nodeChild, Type* type,
72	ValueNode*& _node)
73{
74	if (SupportsType(type) == 0.0f)
75		return B_BAD_VALUE;
76
77	ValueNode* node = new(std::nothrow) CStringValueNode(nodeChild,
78		type);
79
80	if (node == NULL)
81		return B_NO_MEMORY;
82
83	_node = node;
84
85	return B_OK;
86}
87