Ejemplo n.º 1
0
        public void Load(string fileName, XmlDocument document)
        {
            var rootNode = document.DocumentElement;
            if (rootNode == null)
                return;

            if (!rootNode.Name.Equals(DocumentElementName))
                return;

            // Configuration 읽기
            var stdafxNode = rootNode.SelectSingleNode("configuration/stdafx/text()");
            if (stdafxNode != null)
                Configuration.Stdafx = stdafxNode.Value;

            // Attribute 정의 읽기
            var attributeNodes = rootNode.SelectNodes("attribute");
            if (attributeNodes == null)
                return;

            foreach (XmlNode eachAttrNode in attributeNodes)
            {
                var attributeClass = new AttributeClass
                    {
                        Name = eachAttrNode.GetAttribute<string>("name")
                    };
                if (eachAttrNode["custom"] != null)
                    attributeClass.CustomCode = eachAttrNode["custom"].FirstChild.Value.Trim();

                foreach (var eachFieldNode in eachAttrNode.FindChildren("field"))
                {
                    attributeClass.Fields.Add(new AttributeField
                        {
                            Name = eachFieldNode.GetAttribute<string>("name"),
                            Type = TypeUtil.Parse(eachFieldNode.GetAttribute<string>("type")),
                            Default = eachFieldNode.GetAttribute<string>("default"),
                            Volatile = eachFieldNode.GetAttribute<bool>("volatile"),
                        });
                }
                _attributes.Add(attributeClass);
            }
        }
Ejemplo n.º 2
0
        private void GenerateHeaders(AttributeClass attributeClass)
        {
            var code = new SourceCode();
            code.Append("#pragma once");
            code.NewLine();
            code.Append("#include \"cbes/attribute.h\"");
            code.NewLine();
            code.BracketStart("struct {0} : public attribute_t<{0}>", attributeClass.StructName);
            foreach (var attributeField in attributeClass.Fields)
            {
                code.Append("{2}{0} {1};", TypeUtil.ToDeclareTypeName(attributeField.Type), attributeField.Name, attributeField.Volatile ? "volatile " : "");
            }

            code.NewLine();
            code.Append("// default constructor");
            var constructorArgs = attributeClass.Fields.Select(
                e => string.Format("{0}({1})", e.Name, e.Default ?? TypeUtil.ToDefaultValueInInitializer(e.Type))).ToList();

            if (constructorArgs.Count > 0)
            {
                code.Append("{0}()", attributeClass.StructName);
                code.IndentRight();
                code.Append(": {0} {{}}", string.Join(", ", constructorArgs));
                code.IndentLeft();
            }
            else code.Append("{0}() {{}}", attributeClass.StructName);

            if (attributeClass.NonDefaultFields.Any())
            {
                code.NewLine();
                code.Append("// argumented constructor");
                var paramArgs = new List<string>();
                var initializeArgs = new List<string>();
                // default가 없는 field를 대상으로만 argumented constructor를 만들어준다.
                foreach (var attributeField in attributeClass.Fields)
                {
                    if (attributeField.Default == null)
                    {
                        paramArgs.Add(string.Format("{0} _{1}", TypeUtil.ToArgumentTypeName(attributeField.Type), attributeField.Name));
                        initializeArgs.Add(string.Format("{0}(_{0})", attributeField.Name));
                    }
                    else
                    {
                        initializeArgs.Add(string.Format("{0}({1})", attributeField.Name, attributeField.Default));
                    }
                }

                if (initializeArgs.Count > 0)
                {
                    code.Append("{0}({1})", attributeClass.StructName, string.Join(", ", paramArgs));
                    code.IndentRight();
                    code.Append(": {0} {{}}", string.Join(", ", initializeArgs));
                    code.IndentLeft();
                }
            }

            code.NewLine();
            code.Append(SourceCode.Parse(@"
            virtual void from_bson(bson_iterator*);
            virtual void to_bson(bson*);
            virtual void from_xml(TiXmlElement*);
            virtual void to_xml(std::ostream&);".Trim()));

            if (attributeClass.CustomCode != null)
            {
                code.NewLine();
                code.Append(SourceCode.Parse(attributeClass.CustomCode));
            }

            code.BracketEnd(";");

            code.Append("typedef boost::shared_ptr<{0}> {1};", attributeClass.StructName, attributeClass.ReferenceName);
            code.NewLine();

            var headerPath = Path.Combine(_outputDirectory, attributeClass.HeaderFileName);
            code.WriteToFile(headerPath);
        }