Comments (5)
Hello, JelleVM,
Good question. You're talking about the DomNodeAdapter's GetAttribute<T>
. If you go to the DomNodeAdapter's DomNode, then you can call the DomNode's IsAttributeDefault(AttributeInfo)
which I think will do what you want.
/// <summary>
/// Gets whether or not the attribute's value (like by calling GetAttribute) is equal to
/// the default value.</summary>
/// <param name="attributeInfo">Attribute metadata</param>
/// <returns>True if the attribute's value is equal to the default value</returns>
public bool IsAttributeDefault(AttributeInfo attributeInfo)
So, your code would change from:
if (myDomNodeAdapter.HasAttribute<float>(attributeInfo))
to:
if (myDomNodeAdapter.DomNode.IsAttributeDefault(attributeInfo))
Right?
We could add another convenience method to DomNodeAdapter, to save the client from having to call (and discover) the DomNode.IsAttributeDefault() method. I don't think I would call this convenience method "HasAttribute" though, because conceptually, a DomNode "has" all the attributes that are defined for it.
--Ron
from atf.
Hi Ron, thanks for your reply. Thanks for pointing me to the IsAttributeDefault
method, I hadn't thought of that. However, I think it doesn't fully solve the problem. (Though I understand what you're saying that "conceptually, a DomNode 'has' all the attributes that are defined.").
What would you do in the following scenario: your DomNode has an optional
variable, say of type int
. If this variable is not there, you want your programme to compute it. If it is there already, you don't do anything, to avoid unnecessary computation. This would be expressed by the following code:
int m_someVariable;
if (HasAttribute(assets.SomeType.myOptionalTypeAttribute))
m_someVariable = GetAttribute(assets.SomeType.myOptionalTypeAttribute);
else
m_someVariable = ComputeValueOfOptionalAttribute(); // heavy computation
Now the int
value of the attribute could be 0. So if we'd use DomNode.IsAttributeDefault()
instead and the variable value happens to be equal to the default value (i.e. 0), we will be doing heave extra work for nothing, I think. Am I right?
Could maybe DomNode
use a new method:
public bool HasAttribute(AttributeInfo attributeInfo) /\* or "IsAttributeNull" or "HasOptionalAttribute" */
{
return GetLocalAttribute(attributeInfo) == null;
}
from atf.
Hi JelleVM,
I see the problem; thank you for carefully explaining it. I think you're right, that this would be a useful method. Client code can call GetLocalAttribute() and check the result, but that's not very readable.
I need to think about the name for a bit, write unit tests, and ask coworkers before I check this in. (I'm in Japan for a conference this week, announcing that LevelEditor is open source!) The problem I see with HasAttribute(AttributeInfo)
is that it might imply that the method is checking if the AttributeInfo has been defined for that DomNode's DomNodeType. How about AttributeWasSet()? Or AttributeHasBeenSet()?
/// <summary>
/// Gets whether or not the attribute has ever been set, even if it was set to its
/// default value.</summary>
/// <param name="attributeInfo">Attribute metadata</param>
/// <returns>True if the attribute has even been set and false otherwise.</returns>
public bool AttributeWasSet(AttributeInfo attributeInfo)
{
return GetLocalAttribute(attributeInfo) != null;
}
--Ron
from atf.
Hi Ron,
Thanks for considering it! It's indeed quite an uncommon user case, but that's why I brought it up.
The name for the function is entirely up to you and your team of course, but I agree that your suggestions are probably clearer for a generic user case.
Have fun in Japan -- more open source ATF stuff, yeay! :-)
from atf.
I'll check this new method on DomNode shortly. Thanks again for the good suggestion!
/// <summary>
/// Gets whether or not the attribute has been set, even if it was set to its default value.</summary>
/// <param name="attributeInfo">Attribute metadata</param>
/// <returns>True iff the attribute has been set to anything other than null</returns>
/// <remarks>Setting an attribute to null is special, and makes the attribute look like it
/// was never set.</remarks>
public bool IsAttributeSet(AttributeInfo attributeInfo)
{
return GetLocalAttribute(attributeInfo) != null;
}
from atf.
Related Issues (20)
- Saving projects with multiple files HOT 3
- Save button does not update in DomTreeEditor HOT 2
- Use other DLL in MEF, throw exception when load the assemblies. HOT 2
- Syntax Editor license question HOT 2
- Analysis of ATF source code by PVS-Studio static code analyzer HOT 2
- GlobalHistoryContext with multiple EditingContext on same DomNode HOT 7
- PropertyGrid does not update when using keyboard to select ListView item HOT 2
- Show/Hide properties on the PropertyGrid HOT 5
- <xs:all> nodes ignored by DomGen.exe HOT 2
- element minOccurs and maxOccurs are ignored by HOT 2
- Panel visiblity status is not reflected in the "Window" menu HOT 4
- Issue with Validatior/TransactionContext system HOT 1
- Redirect PropertyGrid data HOT 3
- Add "GeneratedCode" attribute to Schema class generated by DomGen
- Consider adding a strong name to DLLs. HOT 2
- DomNodeAdapter to TreeControl.Node
- Update SharpDX HOT 1
- isReadOnly parameter cannot by dynamic with defined editor HOT 3
- UnregisterMenu & Readding Menus HOT 2
- VirtualizingWrapPanel Error HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from atf.